Reputation: 35
I'd like to draw hline with facet_wrap.
How can I add the 'reference value' of ref to each plot?
I want to add reference lines for 'RdRp', 'E', 'N', 'S'.
Both '4℃' and '22℃' use the same lines. For example, I want to draw a hline with yintercept=1.74E+05 for RdRp. And these lines in '4℃' and '22℃'are the same.
Here is my code without geom_hline.
library(reshape2)#melt,dcast
library(ggplot2)
library(ggh4x)#facet_nested
fig4ab=read.csv("fig4ab.csv")
ref=read.csv("ref.csv")
ref.melt=melt(ref, id.vars = c("Gene"))
#fig4ab
fig4ab.melt=na.omit(melt(fig4ab, id.vars = c("Sample", "Day", "Temp", "Vial")))
fig4ab.mean=dcast(fig4ab.melt, Day+Temp+Vial~variable, mean)
fig4ab.sd=dcast(fig4ab.melt, Day+Temp+Vial~variable, sd)
fig4ab.mean.melt=melt(fig4ab.mean, id.vars = c("Day", "Temp", "Vial"))
fig4ab.sd.melt=melt(fig4ab.sd, id.vars = c("Day", "Temp", "Vial"))
fig4ab.sd.melt[is.na(fig4ab.sd.melt)] = 0
fig4ab.mean.total=dcast(fig4ab.mean.melt, Day+Temp~variable, mean)
fig4ab.sd.total=dcast(fig4ab.mean.melt, Day+Temp~variable, sd)
fig4ab.mean.total.melt=melt(fig4ab.mean.total, id.vars = c("Day", "Temp"))
fig4ab.sd.total.melt=melt(fig4ab.sd.total, id.vars = c("Day", "Temp"))
#STS
ggplot(data = fig4ab.mean.total.melt, aes(x=Day, y=value))+
geom_point()+
geom_errorbar(aes(ymin = value - fig4ab.sd.total.melt$value, ymax = value + fig4ab.sd.total.melt$value), alpha=0.5, width=0.4)+
facet_grid(variable~Temp)+
labs(title = "Short-term stability")+
xlab("Day") + ylab("copies/mL") +
scale_x_continuous(breaks = c(0,1,2,5,7))+
scale_y_continuous(limit = c(0,300000), breaks = c(0, 200000))+
scale_colour_grey()+
theme_bw()+
theme(text=element_text(size= 11), legend.text=element_text(size=11), axis.text.x=element_text(size=11), axis.text.y=element_text(size=11), strip.text = element_text(
size = 11))
dput of fig4ab
structure(list(Sample = 1:84, RdRp = c(192000, 188571.4, 180000,
180000, 185142.9, 173142.9, 192000, 188571.4, 180000, 180000,
185142.9, 173142.9, 166285.7, 176571.4, 161142.9, 197142.9, 198857.1,
180000, 192000, 173142.9, 185142.9, 168000, 195428.6, 198857.1,
190285.7, 183428.6, 195428.6, 186857.1, 178285.7, 178285.7, 164571.4,
161142.9, 162857.1, 185142.9, 193714.3, 178285.7, 188571.4, 162857.1,
169714.3, 190285.7, 173142.9, 181714.3, 226285.7, 209142.9, 216000,
174857.1, 174857.1, 171428.6, 180000, 171428.6, 181714.3, 192000,
181714.3, 181714.3, 181714.3, 181714.3, 178285.7, 117600, 125142.9,
123085.7, 135428.6, NA, 144000, 125314.3, 135428.6, 128228.6,
186857.1, 178285.7, 192000, 178285.7, 168000, 186857.1, 190285.7,
181714.3, 173142.9, 149142.9, 145714.3, 144000, 169714.3, 159428.6,
169714.3, 152571.4, 159428.6, 154285.7), E = c(159428.6, 168000,
157714.3, 161142.9, 156000, 161142.9, 159428.6, 168000, 157714.3,
161142.9, 156000, 161142.9, 154285.7, 157714.3, 148971.4, 186857.1,
176571.4, 168000, 164571.4, 166285.7, 164571.4, 193714.3, 178285.7,
185142.9, 178285.7, 161142.9, 168000, 171428.6, 168514.3, 176571.4,
149828.6, 150857.1, 152571.4, 168000, 181714.3, 176571.4, 161142.9,
161142.9, 156000, 162857.1, 171428.6, 168000, 195428.6, 188571.4,
198857.1, 157714.3, 166285.7, 162857.1, 169714.3, 171428.6, 166285.7,
178285.7, 180000, 178285.7, 166285.7, 162857.1, 162857.1, 111085.7,
127885.7, 117257.1, 127714.3, 132000, 132000, 122400, 120857.1,
122571.4, 176571.4, 168000, 161142.9, 152742.9, 159428.6, 161142.9,
162857.1, 173142.9, 149314.3, 156000, 142285.7, 150514.3, 159428.6,
160285.7, 168000, 152571.4, 156000, 140571.4), N = c(219428.6,
214285.7, 195428.6, 214285.7, 214285.7, 205714.3, 219428.6, 214285.7,
195428.6, 214285.7, 214285.7, 205714.3, 190285.7, 190285.7, 185142.9,
207428.6, 212571.4, 207428.6, 202285.7, 200571.4, 197142.9, 243428.6,
240000, 241714.3, 226285.7, 224571.4, 233142.9, 214285.7, 226285.7,
216000, 190285.7, 192000, 198857.1, 210857.1, 202285.7, 224571.4,
200571.4, 204000, 197142.9, 216000, 228000, 233142.9, 246857.1,
264000, 258857.1, 221142.9, 210857.1, 222857.1, 226285.7, 222857.1,
221142.9, 221142.9, 226285.7, 236571.4, 217714.3, 210857.1, 205714.3,
142285.7, 135428.6, 133200, 168000, 166285.7, 164571.4, 145714.3,
149314.3, 149142.9, 216000, 198857.1, 217714.3, 185142.9, 197142.9,
200571.4, 212571.4, 221142.9, 209142.9, 183428.6, 173142.9, 176571.4,
190285.7, 210857.1, 209142.9, 195428.6, 188571.4, 188571.4),
S = c(200571.4, 198857.1, 202285.7, 200571.4, 181714.3, 183428.6,
200571.4, 198857.1, 202285.7, 200571.4, 181714.3, 183428.6,
169714.3, 174857.1, 174857.1, 193714.3, 200571.4, 190285.7,
190285.7, NA, NA, 198857.1, 214285.7, 192000, 214285.7, 202285.7,
209142.9, 197142.9, 197142.9, 205714.3, 178285.7, 173142.9,
168000, 193714.3, 205714.3, 195428.6, 185142.9, 174857.1,
181714.3, 200571.4, 195428.6, 198857.1, 238285.7, 224571.4,
240000, 202285.7, 197142.9, 190285.7, 195428.6, 212571.4,
198857.1, 222857.1, 204000, 200571.4, 202285.7, 195428.6,
205714.3, 130285.7, 134742.9, 133371.4, 142285.7, NA, 152571.4,
140571.4, 138857.1, 138857.1, 195428.6, 188571.4, 192000,
195428.6, 185142.9, 186857.1, 205714.3, 200571.4, 200571.4,
164571.4, 166285.7, 156000, 195428.6, 186857.1, 183428.6,
176571.4, 168000, 162857.1), Day = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L), Temp = c("4℃", "4℃", "4℃", "4℃", "4℃",
"4℃", "22℃", "22℃", "22℃", "22℃", "22℃", "22℃", "4℃",
"4℃", "4℃", "4℃", "4℃", "4℃", "4℃", "4℃", "4℃", "22℃",
"22℃", "22℃", "22℃", "22℃", "22℃", "22℃", "22℃", "22℃",
"4℃", "4℃", "4℃", "4℃", "4℃", "4℃", "4℃", "4℃", "4℃",
"22℃", "22℃", "22℃", "22℃", "22℃", "22℃", "22℃", "22℃",
"22℃", "4℃", "4℃", "4℃", "4℃", "4℃", "4℃", "4℃",
"4℃", "4℃", "22℃", "22℃", "22℃", "22℃", "22℃", "22℃",
"22℃", "22℃", "22℃", "4℃", "4℃", "4℃", "4℃", "4℃",
"4℃", "4℃", "4℃", "4℃", "22℃", "22℃", "22℃", "22℃",
"22℃", "22℃", "22℃", "22℃", "22℃"), Vial = c(1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L,
6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L,
11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 15L,
15L, 15L, 16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L,
19L, 19L, 20L, 20L, 20L, 21L, 21L, 21L, 22L, 22L, 22L, 23L,
23L, 23L, 24L, 24L, 24L, 25L, 25L, 25L, 26L, 26L, 26L, 27L,
27L, 27L, 28L, 28L, 28L)), class = "data.frame", row.names = c(NA,
-84L))
dput of ref
structure(list(Gene = c("N", "S", "E", "RdRp"), reference.value = c(200913.5238,
223204.6667, 154144.5238, 173597.1905), uncertainty = c(59085.3527,
55078.11069, 43052.0553, 41084.84359)), class = "data.frame", row.names = c(NA,
-4L))
Upvotes: 1
Views: 202
Reputation: 125607
You could achieve your desired result by passing your ref
dataframe to the data argument of geom_hline
. To make this work with your data I had to do some data wrangling to convert your reference values to numerics and to rename the Gene
variable to make the faceting work.
library(reshape2) # melt,dcast
library(ggplot2)
library(ggh4x) # facet_nested
ref$reference.value <- as.numeric(gsub("\\.E", "e", ref$reference.value))
ref$uncertainty <- as.numeric(gsub("\\.E", "e", ref$uncertainty))
names(ref)[1] <- "variable"
ref$variable <- trimws(ref$variable)
ref$variable <- factor(ref$variable, levels = levels(fig4ab.mean.total.melt$variable))
# STS
ggplot(data = fig4ab.mean.total.melt, aes(x = Day, y = value)) +
geom_hline(data = ref, aes(yintercept = reference.value)) +
geom_point() +
geom_errorbar(aes(ymin = value - fig4ab.sd.total.melt$value, ymax = value + fig4ab.sd.total.melt$value), alpha = 0.5, width = 0.4) +
facet_grid(variable ~ Temp) +
labs(title = "Short-term stability") +
xlab("Day") +
ylab("copies/mL") +
scale_x_continuous(breaks = c(0, 1, 2, 5, 7)) +
scale_y_continuous(limit = c(0, 300000), breaks = c(0, 200000)) +
scale_colour_grey() +
theme_bw() +
theme(text = element_text(size = 11), legend.text = element_text(size = 11), axis.text.x = element_text(size = 11), axis.text.y = element_text(size = 11), strip.text = element_text(
size = 11
))
Upvotes: 2