forked from CamEJ/Simple-plots-in-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple line plot with error bars
More file actions
76 lines (47 loc) · 2.09 KB
/
Simple line plot with error bars
File metadata and controls
76 lines (47 loc) · 2.09 KB
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
## Simple line plot with error bars
## for error bars used:
## http://docs.ggplot2.org/0.9.3.1/geom_errorbar.html
# Load ggplot
library(ggplot2)
# read in data change sep as appropriate ('\t' = tab separated)
pH <-read.table("LinePlot with error bars_data.txt", header = TRUE, sep='\t')
# here is what data looks like
pH
Week pH Treatment variance
1 0 5.5800 Zero 0.08366600
2 2 5.2450 Zero 0.02661453
3 4 5.0000 Zero 0.15785014
4 5 4.9025 Zero 0.07824907
5 6 4.9000 Zero 0.06164414
6 7 4.8500 Zero 0.15774452
7 0 5.6275 High 0.05121442
8 2 5.2875 High 0.05359960
9 4 5.1675 High 0.07203876
10 5 4.8900 High 0.04020779
11 6 5.1425 High 0.03750000
12 7 5.1725 High 0.07846177
# Define the top and bottom of the errorbars using + & - variance variable
limits <- aes(ymax = pH + variance, ymin=pH - variance)
# now make plot (called j) of pH against week with colour of points changing according to Treatment
j <- ggplot(data=pH, aes(x=Week,y=pH, group=Treatment, colour=Treatment)) +
geom_line() +
geom_point(size=4, shape=21) +
## add error bars
geom_errorbar(limits, width=0.25)
j # print plot
## change from default colours:
## For colour plot using scale_color_brewer using code below instead of scale_colour_grey()
## change palette from "Set1" if you want to change colours
## See http://docs.ggplot2.org/current/scale_brewer.html
## rename plot as j2
j2 <- j + scale_color_brewer(palette="Set1") +
theme_bw() + # remove grey background of plot
theme(text = element_text(size=20, color = "black"))
j2 # print plot and have a look
## now add axis labels and title
j3 <- j2 + labs(title = "pH change with trial time in High & Zero Plots")
## Editing title to make space beneath it and to change size and font face
j3 + theme(plot.title = element_text(size=20, face="bold", vjust=2))
j3 # print plot
## if you are interested in the variance of the samples (eg differences in replicates at each x axis point)
## then maybe scatterplot could be better way to represent data