Basic scatter plots in R

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(file="~/Downloads/Iris (2).csv", header=T)

Now create a scatterplot with 3 colors:

head(x=dat, n=10)
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1           5.1         3.5          1.4         0.2  setosa
## 2           4.9         3.0          1.4         0.2  setosa
## 3           4.7         3.2          1.3         0.2  setosa
## 4           4.6         3.1          1.5         0.2  setosa
## 5           5.0         3.6          1.4         0.2  setosa
## 6           5.4         3.9          1.7         0.4  setosa
## 7           4.6         3.4          1.4         0.3  setosa
## 8           5.0         3.4          1.5         0.2  setosa
## 9           4.4         2.9          1.4         0.2  setosa
## 10          4.9         3.1          1.5         0.1  setosa
plot(y=dat$Sepal.Length, x=dat$Petal.Length)

myColorVector <- ifelse(dat$Species == "setosa", "red", ifelse(dat$Species=="versicolor", "blue", "green"))

plot(dat$Sepal.Length, dat$Petal.Length, col= myColorVector)

plot(dat$Sepal.Length, dat$Petal.Length, col= c(rep("red", 50), rep("blue", 50), rep("green", 50)))#monique's solution

#third solution
plot(x=dat$Sepal.Length, y=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")

Let’s play with the “type” argument:

## Playing with the "type" argument
plot(dat$Petal.Length, dat$Sepal.Length, type="n", xlab="petal length", bty="l") ## Here we're creating an initial blank plot. Look at the help section for the function plot for details about the argument "type"
points(dat$Petal.Length[dat$Species=="setosa"], dat$Sepal.Length[dat$Species=="setosa"], type="o")
points(dat$Petal.Length[dat$Species=="virginica"], dat$Sepal.Length[dat$Species=="virginica"], type="h")

subsetting tables and vectors using the square brackets [ ]

Why did we just use square brackets? This is how you subset data from a vector or dataframe. In other words, you want to extract only certain elements from your dataset. Note that you would use [ , ] if you are specifying a row and column in a dataframe. For example the value in the 4th row and 1st column can be subsetted using:

dat[dat$Species=="setosa",2]
##  [1] 3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 3.7 3.4 3.0 3.0 4.0 4.4 3.9
## [18] 3.5 3.8 3.8 3.4 3.7 3.6 3.3 3.4 3.0 3.4 3.5 3.4 3.2 3.1 3.4 4.1 4.2
## [35] 3.1 3.2 3.5 3.6 3.0 3.4 3.5 2.3 3.2 3.5 3.8 3.0 3.8 3.2 3.7 3.3
dat[dat$Species=="setosa",]
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1           5.1         3.5          1.4         0.2  setosa
## 2           4.9         3.0          1.4         0.2  setosa
## 3           4.7         3.2          1.3         0.2  setosa
## 4           4.6         3.1          1.5         0.2  setosa
## 5           5.0         3.6          1.4         0.2  setosa
## 6           5.4         3.9          1.7         0.4  setosa
## 7           4.6         3.4          1.4         0.3  setosa
## 8           5.0         3.4          1.5         0.2  setosa
## 9           4.4         2.9          1.4         0.2  setosa
## 10          4.9         3.1          1.5         0.1  setosa
## 11          5.4         3.7          1.5         0.2  setosa
## 12          4.8         3.4          1.6         0.2  setosa
## 13          4.8         3.0          1.4         0.1  setosa
## 14          4.3         3.0          1.1         0.1  setosa
## 15          5.8         4.0          1.2         0.2  setosa
## 16          5.7         4.4          1.5         0.4  setosa
## 17          5.4         3.9          1.3         0.4  setosa
## 18          5.1         3.5          1.4         0.3  setosa
## 19          5.7         3.8          1.7         0.3  setosa
## 20          5.1         3.8          1.5         0.3  setosa
## 21          5.4         3.4          1.7         0.2  setosa
## 22          5.1         3.7          1.5         0.4  setosa
## 23          4.6         3.6          1.0         0.2  setosa
## 24          5.1         3.3          1.7         0.5  setosa
## 25          4.8         3.4          1.9         0.2  setosa
## 26          5.0         3.0          1.6         0.2  setosa
## 27          5.0         3.4          1.6         0.4  setosa
## 28          5.2         3.5          1.5         0.2  setosa
## 29          5.2         3.4          1.4         0.2  setosa
## 30          4.7         3.2          1.6         0.2  setosa
## 31          4.8         3.1          1.6         0.2  setosa
## 32          5.4         3.4          1.5         0.4  setosa
## 33          5.2         4.1          1.5         0.1  setosa
## 34          5.5         4.2          1.4         0.2  setosa
## 35          4.9         3.1          1.5         0.2  setosa
## 36          5.0         3.2          1.2         0.2  setosa
## 37          5.5         3.5          1.3         0.2  setosa
## 38          4.9         3.6          1.4         0.1  setosa
## 39          4.4         3.0          1.3         0.2  setosa
## 40          5.1         3.4          1.5         0.2  setosa
## 41          5.0         3.5          1.3         0.3  setosa
## 42          4.5         2.3          1.3         0.3  setosa
## 43          4.4         3.2          1.3         0.2  setosa
## 44          5.0         3.5          1.6         0.6  setosa
## 45          5.1         3.8          1.9         0.4  setosa
## 46          4.8         3.0          1.4         0.3  setosa
## 47          5.1         3.8          1.6         0.2  setosa
## 48          4.6         3.2          1.4         0.2  setosa
## 49          5.3         3.7          1.5         0.2  setosa
## 50          5.0         3.3          1.4         0.2  setosa
dat[dat$Species=="setosa",c(2,3,4,5)]
##    Sepal.Width Petal.Length Petal.Width Species
## 1          3.5          1.4         0.2  setosa
## 2          3.0          1.4         0.2  setosa
## 3          3.2          1.3         0.2  setosa
## 4          3.1          1.5         0.2  setosa
## 5          3.6          1.4         0.2  setosa
## 6          3.9          1.7         0.4  setosa
## 7          3.4          1.4         0.3  setosa
## 8          3.4          1.5         0.2  setosa
## 9          2.9          1.4         0.2  setosa
## 10         3.1          1.5         0.1  setosa
## 11         3.7          1.5         0.2  setosa
## 12         3.4          1.6         0.2  setosa
## 13         3.0          1.4         0.1  setosa
## 14         3.0          1.1         0.1  setosa
## 15         4.0          1.2         0.2  setosa
## 16         4.4          1.5         0.4  setosa
## 17         3.9          1.3         0.4  setosa
## 18         3.5          1.4         0.3  setosa
## 19         3.8          1.7         0.3  setosa
## 20         3.8          1.5         0.3  setosa
## 21         3.4          1.7         0.2  setosa
## 22         3.7          1.5         0.4  setosa
## 23         3.6          1.0         0.2  setosa
## 24         3.3          1.7         0.5  setosa
## 25         3.4          1.9         0.2  setosa
## 26         3.0          1.6         0.2  setosa
## 27         3.4          1.6         0.4  setosa
## 28         3.5          1.5         0.2  setosa
## 29         3.4          1.4         0.2  setosa
## 30         3.2          1.6         0.2  setosa
## 31         3.1          1.6         0.2  setosa
## 32         3.4          1.5         0.4  setosa
## 33         4.1          1.5         0.1  setosa
## 34         4.2          1.4         0.2  setosa
## 35         3.1          1.5         0.2  setosa
## 36         3.2          1.2         0.2  setosa
## 37         3.5          1.3         0.2  setosa
## 38         3.6          1.4         0.1  setosa
## 39         3.0          1.3         0.2  setosa
## 40         3.4          1.5         0.2  setosa
## 41         3.5          1.3         0.3  setosa
## 42         2.3          1.3         0.3  setosa
## 43         3.2          1.3         0.2  setosa
## 44         3.5          1.6         0.6  setosa
## 45         3.8          1.9         0.4  setosa
## 46         3.0          1.4         0.3  setosa
## 47         3.8          1.6         0.2  setosa
## 48         3.2          1.4         0.2  setosa
## 49         3.7          1.5         0.2  setosa
## 50         3.3          1.4         0.2  setosa
##equivalent to:
dat[dat$Species=="setosa",-1]
##    Sepal.Width Petal.Length Petal.Width Species
## 1          3.5          1.4         0.2  setosa
## 2          3.0          1.4         0.2  setosa
## 3          3.2          1.3         0.2  setosa
## 4          3.1          1.5         0.2  setosa
## 5          3.6          1.4         0.2  setosa
## 6          3.9          1.7         0.4  setosa
## 7          3.4          1.4         0.3  setosa
## 8          3.4          1.5         0.2  setosa
## 9          2.9          1.4         0.2  setosa
## 10         3.1          1.5         0.1  setosa
## 11         3.7          1.5         0.2  setosa
## 12         3.4          1.6         0.2  setosa
## 13         3.0          1.4         0.1  setosa
## 14         3.0          1.1         0.1  setosa
## 15         4.0          1.2         0.2  setosa
## 16         4.4          1.5         0.4  setosa
## 17         3.9          1.3         0.4  setosa
## 18         3.5          1.4         0.3  setosa
## 19         3.8          1.7         0.3  setosa
## 20         3.8          1.5         0.3  setosa
## 21         3.4          1.7         0.2  setosa
## 22         3.7          1.5         0.4  setosa
## 23         3.6          1.0         0.2  setosa
## 24         3.3          1.7         0.5  setosa
## 25         3.4          1.9         0.2  setosa
## 26         3.0          1.6         0.2  setosa
## 27         3.4          1.6         0.4  setosa
## 28         3.5          1.5         0.2  setosa
## 29         3.4          1.4         0.2  setosa
## 30         3.2          1.6         0.2  setosa
## 31         3.1          1.6         0.2  setosa
## 32         3.4          1.5         0.4  setosa
## 33         4.1          1.5         0.1  setosa
## 34         4.2          1.4         0.2  setosa
## 35         3.1          1.5         0.2  setosa
## 36         3.2          1.2         0.2  setosa
## 37         3.5          1.3         0.2  setosa
## 38         3.6          1.4         0.1  setosa
## 39         3.0          1.3         0.2  setosa
## 40         3.4          1.5         0.2  setosa
## 41         3.5          1.3         0.3  setosa
## 42         2.3          1.3         0.3  setosa
## 43         3.2          1.3         0.2  setosa
## 44         3.5          1.6         0.6  setosa
## 45         3.8          1.9         0.4  setosa
## 46         3.0          1.4         0.3  setosa
## 47         3.8          1.6         0.2  setosa
## 48         3.2          1.4         0.2  setosa
## 49         3.7          1.5         0.2  setosa
## 50         3.3          1.4         0.2  setosa
##also equivalent to:
setosaCol2through5 <- dat[dat$Species=="setosa",c(2:5)]
write.csv(setosaCol2through5, file = "~/Downloads/myFirstFile.csv", row.names = FALSE)


dat[4,1]
## [1] 4.6
#if you want to subset columns 3 and 4:
dat[,c(3,4)]
##     Petal.Length Petal.Width
## 1            1.4         0.2
## 2            1.4         0.2
## 3            1.3         0.2
## 4            1.5         0.2
## 5            1.4         0.2
## 6            1.7         0.4
## 7            1.4         0.3
## 8            1.5         0.2
## 9            1.4         0.2
## 10           1.5         0.1
## 11           1.5         0.2
## 12           1.6         0.2
## 13           1.4         0.1
## 14           1.1         0.1
## 15           1.2         0.2
## 16           1.5         0.4
## 17           1.3         0.4
## 18           1.4         0.3
## 19           1.7         0.3
## 20           1.5         0.3
## 21           1.7         0.2
## 22           1.5         0.4
## 23           1.0         0.2
## 24           1.7         0.5
## 25           1.9         0.2
## 26           1.6         0.2
## 27           1.6         0.4
## 28           1.5         0.2
## 29           1.4         0.2
## 30           1.6         0.2
## 31           1.6         0.2
## 32           1.5         0.4
## 33           1.5         0.1
## 34           1.4         0.2
## 35           1.5         0.2
## 36           1.2         0.2
## 37           1.3         0.2
## 38           1.4         0.1
## 39           1.3         0.2
## 40           1.5         0.2
## 41           1.3         0.3
## 42           1.3         0.3
## 43           1.3         0.2
## 44           1.6         0.6
## 45           1.9         0.4
## 46           1.4         0.3
## 47           1.6         0.2
## 48           1.4         0.2
## 49           1.5         0.2
## 50           1.4         0.2
## 51           4.7         1.4
## 52           4.5         1.5
## 53           4.9         1.5
## 54           4.0         1.3
## 55           4.6         1.5
## 56           4.5         1.3
## 57           4.7         1.6
## 58           3.3         1.0
## 59           4.6         1.3
## 60           3.9         1.4
## 61           3.5         1.0
## 62           4.2         1.5
## 63           4.0         1.0
## 64           4.7         1.4
## 65           3.6         1.3
## 66           4.4         1.4
## 67           4.5         1.5
## 68           4.1         1.0
## 69           4.5         1.5
## 70           3.9         1.1
## 71           4.8         1.8
## 72           4.0         1.3
## 73           4.9         1.5
## 74           4.7         1.2
## 75           4.3         1.3
## 76           4.4         1.4
## 77           4.8         1.4
## 78           5.0         1.7
## 79           4.5         1.5
## 80           3.5         1.0
## 81           3.8         1.1
## 82           3.7         1.0
## 83           3.9         1.2
## 84           5.1         1.6
## 85           4.5         1.5
## 86           4.5         1.6
## 87           4.7         1.5
## 88           4.4         1.3
## 89           4.1         1.3
## 90           4.0         1.3
## 91           4.4         1.2
## 92           4.6         1.4
## 93           4.0         1.2
## 94           3.3         1.0
## 95           4.2         1.3
## 96           4.2         1.2
## 97           4.2         1.3
## 98           4.3         1.3
## 99           3.0         1.1
## 100          4.1         1.3
## 101          6.0         2.5
## 102          5.1         1.9
## 103          5.9         2.1
## 104          5.6         1.8
## 105          5.8         2.2
## 106          6.6         2.1
## 107          4.5         1.7
## 108          6.3         1.8
## 109          5.8         1.8
## 110          6.1         2.5
## 111          5.1         2.0
## 112          5.3         1.9
## 113          5.5         2.1
## 114          5.0         2.0
## 115          5.1         2.4
## 116          5.3         2.3
## 117          5.5         1.8
## 118          6.7         2.2
## 119          6.9         2.3
## 120          5.0         1.5
## 121          5.7         2.3
## 122          4.9         2.0
## 123          6.7         2.0
## 124          4.9         1.8
## 125          5.7         2.1
## 126          6.0         1.8
## 127          4.8         1.8
## 128          4.9         1.8
## 129          5.6         2.1
## 130          5.8         1.6
## 131          6.1         1.9
## 132          6.4         2.0
## 133          5.6         2.2
## 134          5.1         1.5
## 135          5.6         1.4
## 136          6.1         2.3
## 137          5.6         2.4
## 138          5.5         1.8
## 139          4.8         1.8
## 140          5.4         2.1
## 141          5.6         2.4
## 142          5.1         2.3
## 143          5.1         1.9
## 144          5.9         2.3
## 145          5.7         2.5
## 146          5.2         2.3
## 147          5.0         1.9
## 148          5.2         2.0
## 149          5.4         2.3
## 150          5.1         1.8
#if you want to subset raws 4 and 6:
dat[c(4,6),]
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 4          4.6         3.1          1.5         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
## When you are subsetting a vector, which is a 1-D object, you only need the index of the item you are subsetting. No comma because you do not need row and column indices. For example:
myVector <- LETTERS[1:10] ## LETTERS and letters are base constants in R for the 26 letters of the alphabet in either upper- or lower-case. 
myVector
##  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
myVector[3] ## subset the 3rd element in this vector
## [1] "C"
## Let's subset just the rows that correspond to setosa in the dat dataset:
dat$Petal.Length[dat$Species=="setosa"]##here we use a conditional statement (dat$Species=="setosa" returns a vector of TRUE's and FALSE's) to tell R which element of the Petal.Length vector we want to subset
##  [1] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 1.5 1.6 1.4 1.1 1.2 1.5 1.3
## [18] 1.4 1.7 1.5 1.7 1.5 1.0 1.7 1.9 1.6 1.6 1.5 1.4 1.6 1.6 1.5 1.5 1.4
## [35] 1.5 1.2 1.3 1.4 1.3 1.5 1.3 1.3 1.3 1.6 1.9 1.4 1.6 1.4 1.5 1.4
#Just type this if you're not sure you understand
dat$Species=="setosa"
##   [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [12]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [23]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [34]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [45]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
##  [56] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [67] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [78] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [89] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [100] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [111] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [122] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [144] FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Graphical options: a few examples

Let’s try to plot a basic graph again. Pay attention to the arguments used below.

plot(dat$Petal.Length, dat$Sepal.Length, 
     main="my nice title", ## main plot title
     sub="blah", ## a subtitle for the plot
     bty="l", ## box type for the plot
     xlab="Iris petal length", ## x-axis label
     ylab="Iris sepal length", ## y-axis label
     xlim=c(0,7), ylim=c(0,8), ## limits for x and y axes
     pch=6, ## character type for points
     cex=0.5, ## size of plot points
     ## col=dat$Species ## this will set colors 1:3 for each species
     col=ifelse(dat$Species=="setosa", "red", ifelse(dat$Species=="versicolor", "blue", "green")) ## you can also use numbers for basic colors such as 1,2,3, etc. 
     ## the nested ifelse function above let's you set the color for points based on a series statements
)

## Note about creating a numeric vector:
c(1,2,3,4, 5, 6, 7, 8, 9, 10, 11)
##  [1]  1  2  3  4  5  6  7  8  9 10 11
## is equivalent to
1:11 ## this is useful if you have a long list of consecutive numbers
##  [1]  1  2  3  4  5  6  7  8  9 10 11
## Take a quick look at what all the plotting characters look like. 
plot(1:20, 1:20, pch=1:20, cex=1.2, main="Different plotting characters in R", xlab=NA, ylab=NA) ## if you want to remove the axis titles, you can assign the axis titles as NA

##look at this one too:
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")

## histogram:
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=20, las=1)

Interactive graphs

There are many packages available to create interactive graphs. Plotly is a good start.

##interactive graph
install.packages("plotly")##only the first time you use package "plotly" with yout computer
library(plotly)
p <- plot_ly(x = dat$Sepal.Length, y = dat$Petal.Length)
p