-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggplot2_Pres.R
143 lines (110 loc) · 4.89 KB
/
ggplot2_Pres.R
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# ggplot2 package presentation
# Alex Burnham
# Preliminaries:
# Clear memory of varaiables and objects:
rm(list=ls())
# load packages:
library(ggplot2)
library(plyr)
# Create a Data set:
# create sample IDs
ID <- c(1:200)
# create a factor called origin
Origin <- c(rep("local", 100),
rep("California", 100))
# create a factor called flower type
FlowerType <- rep(c(rep("clover",25),
rep("goldenrod",25),
rep("treefoil",25),
rep("mixed",25)),2)
# create a variable called mass
Mass <- c(rnorm(n = 100,
mean=32,
sd = 8), rnorm(n = 100,
mean=21,
sd=4))
# create a variable called Nosema Load
NosemaLoad <-c(rnorm(n = 100,
mean=100000,
sd = 80000), rnorm(n = 100,
mean=500000,
sd=40000))
# create a variable called varroa load
VarroaLoad <- c(rnorm(n = 100,
mean=5,
sd = 2), rnorm(n = 100,
mean=9,
sd=3))
# create a variable called time
Time <- rep(c(rep("Time1", 50), rep("Time2", 50)),2)
# create data frame
DF <- data.frame(ID, Origin, FlowerType, Mass, NosemaLoad, VarroaLoad, Time)
# Basic Graphics in ggplot:
# Summary stats using ddply:
# using ddply to get summary stats for mass:
DF1 <- ddply(DF, c("FlowerType"), summarise,
n = length(Mass),
mean = mean(Mass, na.rm=TRUE),
sd = sd(Mass, na.rm=TRUE),
se = sd / sqrt(n))
# Bar Plot
plot1 <- ggplot(DF1, aes(x=FlowerType,
y=mean)) + geom_bar(stat = "identity")
plot1 + theme_minimal(base_size = 17)+geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.2, position=position_dodge(.9)) + coord_cartesian(ylim=c(0,40))
#Histogram
plot2 <- ggplot(DF, aes(Mass))
plot2 + geom_histogram() + stat_bin(bins = 30) + theme_minimal(base_size = 17)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# Scatterplot
plot3 <- ggplot(DF, aes(x=Mass,
y=VarroaLoad))
plot3 + geom_point() + theme_minimal(base_size = 17)
#Boxplot
plot4 <- ggplot(DF, aes(x=FlowerType,
y=Mass))
plot4 + geom_boxplot() + theme_minimal(base_size = 17)
#More Complicated Graphics in ggplot
#Summary stats using ddply:
# using ddply to get summary stats for mass:
DF2 <- ddply(DF, c("FlowerType", "Origin"), summarise,
n = length(Mass),
mean = mean(Mass, na.rm=TRUE),
sd = sd(Mass, na.rm=TRUE),
se = sd / sqrt(n))
# Bar Plot
#choosing color pallet
colors <- c("slategray3", "dodgerblue4")
#Create a bar graph for with CI and SE bars
plot5 <- ggplot(DF2, aes(x=FlowerType,
y=mean,
fill=Origin)) +
geom_bar(stat="identity",
position=position_dodge()) + labs(x="Flower Type", y = "Mass (lbs)") + geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.2, position=position_dodge(.9))
plot5 + theme_minimal(base_size = 17) + scale_fill_manual(values=colors, name="Origin:") + coord_cartesian(ylim = c(0, 40))
# Histogram
colors <- c("gray", "blue")
plot6 <- ggplot(DF, aes(x=Mass, fill=Origin)) +
geom_histogram(binwidth=1, alpha=.7, position="identity")
plot6 + theme_minimal(base_size = 17) + scale_fill_manual(values=colors)
#Scatterplot
plot7 <- ggplot(DF, aes(x=Mass,
y=VarroaLoad))
plot7 + geom_point(aes(color = Mass)) + theme_minimal(base_size = 17) + geom_smooth(method="lm",se=F)
# Boxplot
colors1 <- c("slategray3", "dodgerblue4", "blue", "lightblue")
plot8 <- ggplot(DF, aes(x=FlowerType,
y=Mass,
fill=FlowerType))
plot8 + geom_boxplot() + scale_fill_manual(values=colors1, guide_legend(NULL)) + guides(fill=FALSE) + theme_minimal(base_size = 17)
#Clean graphics for publication:
DF3 <- ddply(DF, c("Time", "Origin"), summarise,
n = length(NosemaLoad),
mean = mean(NosemaLoad, na.rm=TRUE),
sd = sd(NosemaLoad, na.rm=TRUE),
se = sd / sqrt(n))
plot9 <- ggplot(data = DF3,
aes(x = Time,
y = mean,
group = Origin)
) + geom_point(size=3) + scale_colour_manual(values = c("black", "black")) + labs(x = "Time", y = "Nosema Load (spores/bee)") + geom_line(aes(linetype=Origin), size=1) + scale_fill_brewer(palette = "Paired") + theme_classic(base_size = 17) + coord_cartesian(ylim = c(0, 690000), xlim = c(1,2)) + geom_errorbar(aes(ymin = mean - se, ymax = mean + se, width = 0.1))
plot9 + theme(legend.position=c(.15, .85)) + labs(linetype="Queen Origin")