Exercises

Create the following figures for the R CO2 dataset using ggplot2. When applicable, use only one line of code to create each figure by piping commands together.

NOTE: For these solutions, the following command was run before generating any figures. This sets the ggplot theme to “classic” for all plots created in the R session:


theme_set(theme_classic())



  1. Histogram of CO2 uptake values for all plants

ggplot(CO2, aes(x = uptake)) + geom_histogram()

figure


  1. Histogram of CO2 uptake values for all plants from Mississippi

CO2 %>% 
	filter(Type == "Mississippi") %>%
	ggplot(aes(x = uptake)) + geom_histogram()

figure

  1. Histogram of CO2 uptake values for all plants from Mississippi. This time, provide the argument bins=50 to geom_histogram(), as in: geom_histogram(bins=50). Then, provide bins=5 and finally bins=100. Think about how these plots compare and therefore what this argument is doing.

CO2 %>% 
	filter(Type == "Mississippi") %>%
	ggplot(aes(x = uptake)) + geom_histogram(bins=50)

figure


CO2 %>% 
	filter(Type == "Mississippi") %>%
	ggplot(aes(x = uptake)) + geom_histogram(bins=5)

figure


CO2 %>% 
	filter(Type == "Mississippi") %>%
	ggplot(aes(x = uptake)) + geom_histogram(bins=100)

figure

Fewer bins means less resolution in the histogram. More bins means thinner, finer bars.


  1. Faceted histogram of CO2 uptake values across plant Type

ggplot(CO2, aes(x = uptake)) + geom_histogram() + facet_grid(~Type)

figure

  1. Scatterplot of CO2 uptake against concentration (meaning uptake is the response variable) for all nonchilled plants, where points are colored by plant Type

CO2 %>%
	filter(Treatment == "nonchilled") %>%
	ggplot(aes(x = conc, y = uptake)) + geom_point(aes(color = Type))

figure

  1. Faceted scatterplot of CO2 uptake against concentration across plant Type, where points are colored by Treatment

CO2 %>%
	ggplot(aes(x = conc, y = uptake)) + geom_point(aes(color = Treatment)) + facet_grid(~Type)

figure

  1. Faceted scatterplot of CO2 uptake against concentration across plant Type and Treatment, i.e. the final figure should have 2 rows and 2 columns.

CO2 %>%
	ggplot(aes(x = conc, y = uptake)) + geom_point() + facet_grid(Treatment~Type)

figure