Created
February 27, 2015 06:25
-
-
Save pspitler3/0754363bb37baa8c3cb4 to your computer and use it in GitHub Desktop.
Start-R Part 2 Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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