Back to main workshop page

Sorry, this is going to be a bit rough. All of the code is below but I won’t have time to annotate it before the lecture. Please check back later!


library(tidyr)
library(ggplot2)
data(iris)
head(iris)
dim(iris)


iris$indiv = 1:nrow(iris)
# We want to do a ggplot: Boxplot with:
# "measurement" on the x axis (sepal length, sepal width),
# colored by "Species":

ggplot(iris, aes(x = measurement_type, y = measurement, color = Species))+geom_point()

#### Taking multiple columns and combining them like this is a tidyr function called "Gather". we are gatheringi nividiual columns into just 2.
# either specify all columns or
iris_gathered <- gather(iris, measurement, value, Sepal.Length:Petal.Width)
# or which to exclude
iris_gathered <- gather(iris, measurement, value, -Species, -indiv)
iris_gathered
dim(iris_gathered)

ggplot(iris_gathered, aes(x = measurement, y = value, color = Species))+geom_boxplot()


# Now, we want to go back to the original. We want to plot sepal length vs. Sepal width for each sp. That means we need these as 2 columns.

iris_2 <- spread(iris_gathered, measurement, value)

myplot <- spread(iris_gathered, measurement, value) %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species))+geom_point()
myplot


### Going back to iris_gathered:

head(iris_gathered)
# columns for part, length_or_width
separate(iris_gathered, measurement, into = c('part', 'length_or_width'), sep = '.')
#https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html
separate_iris = separate(iris_gathered, measurement, into = c('part', 'length_or_width'), sep = '\\.')

ggplot(separate_iris, aes(x = part, y = value, color = length_or_width))+geom_boxplot()+facet_wrap(~Species)


####
#"unite" puts things back together:
iris_3 <- unite(separate_iris, new_measurement,part, length_or_width, sep = '_')
head(iris_3)