This is your personal markdown file. Modify the title of this .Rmd file and change the author name. You can also delete this and any other useless sentence.
Download the iris.csv file from canvas and load it in R. To do so, modify the line of code below (hint: remember the “import dataset” menu?):
dat <- read.csv("~/Downloads/Iris (2).csv")
Create a nice scatterplot, and change the color of the dots. Here is the R command for that:
head(dat)
plot(dat$Sepal.Length, dat$Petal.Length, col= ifelse(dat$Species=="setosa", "red", ifelse(dat$Species=="versicolor", "blue", "green")))
plot(dat$Sepal.Length, dat$Petal.Length, col= c(rep("red", 50), rep("blue", 50), rep("green", 50)))##monique's solution
##third solution
plot(dat$Sepal.Length, dat$Petal.Length, type="n")
points(dat$Sepal.Length[dat$Species=="setosa"], dat$Petal.Length[dat$Species=="setosa"], col="red")
points(dat$Sepal.Length[dat$Species=="virginica"], dat$Petal.Length[dat$Species=="virginica"], col="blue")
points(dat$Sepal.Length[dat$Species=="versicolor"], dat$Petal.Length[dat$Species=="versicolor"], col="green")
Now be creative and try to: 1. Use two different colors for Iris setosa and for the 2 other species. Hint: col= ifelse(dat$Species=="setosa", "red", "green") 2. More tricky: use three different colors for Iris setosa, Iris virginica and Iris versicolor. 3. Create a histogram showing the distribution of variable “Petal.Length”. How many modes do you see? Change the number of bars on your histogram. Add a color and edit the x and y axis labels.
myHist <- hist(dat$Petal.Length, col="darkmagenta", main="histogram of something", xlab="Petal length", ylab="counts", nclass=50)
barplot(myHist$counts, horiz=TRUE)
myHist <- hist(dat$Petal.Length, col="darkmagenta", main="histogram of something", xlab="Petal length", ylab="counts", nclass=50, las=1)
More graphical options are available. You can find them in the help file of function “par”:
?par
#most of the arguments of function "par" can also be used for most other graphical functions like "plot", "barplot", "hist", etc...
#for example:
plot(x = 1:10, y=10:1, bty="l", cex=2, cex.axis=1.5, main="lame graph", cex.main=.5, fg="red", col.lab="blue", col.main="yellow", col.axis="purple", family="Georgia")
Feel free to test other possible graphical arguments…
Install package “plotly”. You only need to do it once. Once it’s in your computer, it’ll stay there. All you’ll need to after that is load the package using the library() function.
install.packages("plotly")
Package plotly allows you to create interactive graphs (in html only! It would not makes sense on a pdf or word doc):
library(plotly)##also works if you use quotes: library("plotly")
p <- plot_ly(x = dat$Sepal.Length, y = dat$Petal.Length)
p
Note: The above tag (<br>) inserts an empty line in the R markdown document.
As you will see, there is a menu bar above the plotly graph. Play with it and see what the different options are.
Try to improve your interactive graph using tips provided in the plotly cheat sheet.
Here is how to add some colors and change the information displayed when hovering…
p <- plot_ly(x = dat$Sepal.Length, y = dat$Petal.Length, color = ~dat$Species, hoverinfo = 'text', text = paste("Species: ", dat$Species, "<br>Sepal length: ", dat$Sepal.Length, "cm<br>Petal length: ", dat$Petal.Length, "cm"))
p
p <- plot_ly(x = dat$Sepal.Length, y = dat$Petal.Length, color = ~dat$Species, hoverinfo = 'text', text = paste("Species: ", dat$Species))
p
Click the publish button and export your webpage to the internet! What’s the http link to your webpage?
Does it look good?