Skip to content

Instantly share code, notes, and snippets.

@pspitler3
Created February 27, 2015 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pspitler3/0754363bb37baa8c3cb4 to your computer and use it in GitHub Desktop.
Save pspitler3/0754363bb37baa8c3cb4 to your computer and use it in GitHub Desktop.
Start-R Part 2 Code
# Load the required libraries
library(datasets)
library(dplyr)
library(ggplot2)
# Load the two datasets to be consumed in this example
CO2 <- CO2
PlantGrowth <- PlantGrowth
# Look at the column names and the first few values for CO2
colnames(CO2)
head(CO2)
# Look at the column names and the first few values for PlantGrowth
colnames(PlantGrowth)
head(PlantGrowth)
# Use dplyr to summarize the data
CO2_summary <- CO2 %>% filter(Type == 'Quebec') %>% select(-Type) %>%
group_by(Plant, Treatment) %>%
summarize(mean_uptake = mean(uptake), med_uptake = median(uptake))
View(CO2_summary)
# Use dplyr to create a quick look at Quebec and Mississippi plants
CO2_Quebec <- CO2 %>% filter(Type == 'Quebec')
CO2_Mississippi <- CO2 %>% filter(Type == 'Mississippi')
# Use ggplot to explore the CO2 data
# Use boxplots to look at how treatments effect plants for quebec and mississippi
ggplot(CO2_Quebec, aes(x = Plant, y = uptake, fill = Plant)) + geom_boxplot()
ggplot(CO2_Mississippi, aes(x = Plant, y = uptake, fill = Plant)) + geom_boxplot()
# Use violin plots to look at how treatments effect plants for quebec and mississippi
ggplot(CO2_Quebec, aes(x = Plant, y = uptake, fill = Plant)) + geom_violin()
ggplot(CO2_Mississippi, aes(x = Plant, y = uptake, fill = Plant)) + geom_violin()
# Use density plots to look at how treatments effect plants for quebec and mississippi
ggplot(CO2_Quebec, aes(x = uptake, fill = Plant)) + geom_density(alpha = .2)
ggplot(CO2_Mississippi, aes(x = uptake, fill = Plant)) + geom_density(alpha = .2)
# Use boxplots to look at plant weights by treatments versus controls
ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) + geom_boxplot()
# Use violin plots to look at plant weights by treatments versus controls
ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) + geom_violin()
# Use density plots to look at plant weights by treatments versus controls
ggplot(PlantGrowth, aes(x = weight, fill = group)) + geom_density(alpha = .2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment