forked from glochi/HW1_gloria_chi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggplot2_slide27.Rmd
39 lines (34 loc) · 1.05 KB
/
ggplot2_slide27.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
---
title: "HW1 - Gloria Chi"
output: html_document
---
```{r}
library(ggplot2)
library(reshape2)
data(iris)
head(iris)
# Add a column for flower ID
iris$flower_id <- rownames(iris)
# Default options
iris_melted <- melt(iris)
head(iris_melted)
# Split iris_melted to get the columns we need
split_variable <- strsplit(as.character(iris_melted$variable), split = "\\.")
# Create two new variables
iris_melted$flower_part <- sapply(split_variable, "[", 1)
iris_melted$measurement_type <- sapply(split_variable, "[", 2)
# Remove the one we don't need anymore
iris_melted$variable <- NULL
head(iris_melted)
```
Reproduce Slide 27 Graphs
```{r}
iris_cast <- dcast(iris_melted, formula = flower_id + Species + flower_part ~ measurement_type)
head(iris_cast)
ggplot(data=iris_cast, aes(x=Width, y=Length))+ # Add points and use free scales in the facet
geom_point()+facet_grid(Species~flower_part, scale="free")+
# Add a regression line
geom_smooth(method="lm")+
# Use the black/white theme and increase the font size
theme_bw(base_size=24)
```