在 ggplot2 中使用 geom_errorbar() 重新格式化数据以绘制校准曲线
Reformat data to plot a calibration curve using geom_errorbar() in ggplot2
简介:
我有一个包含三种不同空气质量测量值的汇总统计数据的数据框。仪器名称为 aa34
、aa35
和 48c
。他们各自以 ppm 为单位测量一氧化碳。我有宽格式的数据,其中每个向量是三种仪器的平均值、标准偏差、标准误差或 95% 置信区间。
我想使用 ggplot()
和 geom_errorbar()
绘制这些汇总统计数据,但我在将数据转换为长格式并为 [= 中的颜色映射提供 ID 变量时遇到了一些问题16=]。我正在关注 this 教程。下面是我想重现的图(当然用有毒烟雾代替了豚鼠的牙齿数据)。我一直在尝试添加一个额外的 y 变量并让它们通过 ID
变量进行颜色协调。我想要的输出将用三个 id
向量中的两个替换示例中的 supp
向量,即包含 aa34
和 aa35
的向量。我相当于 dose
向量的是 ref.co.mean
,我们的 x
变量。我相当于 len
向量的是长格式的向量 aa34.co.mean
和 aa35.co.mean
。
数据:
## Here's what my data frame looks like.
## I know it's ugly, but if you copy and paste it into your console it should work!
df_cal <- structure(list(ref.co.mean = c(1.23638284617457, 1.46466241535712,
2.16020882959014, 2.55054760052641, 3.13141175081258, 3.86968879644661,
6.5914211520901), ref.co.sd = c(0.0196205483139859, 0.0229279198586359,
0.0172965018302434, 0.0164690175286326, 0.00583116470707786,
0.00975072766851073, 0.0388826652553337), ref.co.se = c(0.00346845569085442,
0.00193776290206006, 0.00166435666462165, 0.00127061228762621,
0.000583116470707786, 0.00229826855196908, 0.00614788918523735
), ref.co.ci = c(0.00707396201972773, 0.00383130164529687,
0.00329939297398704,
0.0025085329371034, 0.00115702958592763, 0.00484892279298878,
0.0124352796323718), id = c("48c", "48c", "48c", "48c", "48c",
"48c", "48c"), aa34.co.mean = c(0, 0.248857142857143, 0.823777777777778,
1.256, 1.886, 2.446, 4.54), aa34.co.sd = c(0, 0.0716567783084826,
0.0660714166547489, 0.0777970497665622, 0.0518459255872629, 0,
0.0690217357069497), aa34.co.se = c(0, 0.00605610310675521,
0.0063577250318932, 0.00600217269807407, 0.00518459255872628, 0,
0.0109132946446067), aa34.co.ci = c(0, 0.0119739921598931,
0.0126034483753748, 0.0118499152368743, 0.0102873564420935, 0,
0.0220742219853317), id = c("aa34", "aa34", "aa34", "aa34", "aa34", "aa34",
"aa34"), aa35.co.mean = c(0.2915625, 0.801035714285714, 1.39911111111111,
1.80436904761905, 2.45672, 3.02355555555556, 5.134975), aa35.co.sd =
c(0.0691998633940125, 0.0474980316455754, 0.0846624379229758,
0.0822798331713915, 0.0595577165445419,
0.0178768075145867, 0.0243007072942329), aa35.co.se = c(0.0122329231657723,
0.00401431635364878, 0.00814664688751334, 0.00634802694633388,
0.00595577165445419, 0.00421360393984362, 0.00384227919014218), aa35.co.ci =
c(0.0249492112853266, 0.00793701687349159, 0.0161497773125,
0.0125327252345785, 0.0118175430765459, 0.00888992723110191,
0.00777174323014678), id = c("aa35", "aa35", "aa35", "aa35",
"aa35", "aa35", "aa35")), .Names = c("ref.co.mean", "ref.co.sd",
"ref.co.se", "ref.co.ci", "id", "aa34.co.mean", "aa34.co.sd",
"aa34.co.se", "aa34.co.ci", "id", "aa35.co.mean", "aa35.co.sd",
"aa35.co.se", "aa35.co.ci", "id"), row.names = c(1L, 33L, 173L,
281L, 449L, 549L, 567L), class = "data.frame")
这是我的第一次尝试:
## This code only gets half of the job done...
## 95% Confidence Intervals for Error Bars:
p <- ggplot(df_cal, aes(x=ref.co.mean, y=aa34.co.mean)) +
theme_bw() +
geom_errorbar(aes(ymin=aa34.co.mean-aa34.co.ci,
ymax=aa34.co.mean+aa34.co.ci), width =.05) +
xlab("Reference CO (ppm)") +
ylab("AA34 CO (ppm)") +
geom_smooth(method='lm', formula = y~x, se = FALSE) +
geom_point(size=2, shape = 21, fill="White") +
geom_abline(intercept = 0, slope = 1, color, linetype=2, color = "firebrick") +
ggtitle("CO Calibration @ 0% RH") +
theme(plot.title = element_text(hjust = 0.5)) +
annotate("rect", xmin = 4.80, xmax = 5.70, ymin = 0.70, ymax = 1.70,
fill="white", colour="red") +
annotate("text", x=5.25, y=1.50, label= "R^2 == 0.994", parse=T) +
annotate("text", x=5.25, y=1.20, label= "alpha == -0.9490", parse=T) +
annotate("text", x=5.25, y=0.90, label= "beta == 0.849", parse=T)
p
提前致谢!
这里切换到长格式的问题是你有一个 length 7 的 x 轴变量和 2 个组合的 length 14 为 y 轴。因此,此解决方案绑定行,以便引用(x 轴)数据包含两次。然后就是在 ggplot
美学中使用 colour
和 group
的问题。
library(ggplot2)
df_aa34_2<-df_cal[,c(1:4,6:10)]#select first 'aa' group including reference data (48c)
df_aa35_2<-df_cal[,c(1:4,11:15)]#select second 'aa' group including reference data (48c)
names(df_aa34_2)<-names(df_aa35_2)#colnames must be the same for rbind function
DF<-rbind(df_aa34_2,df_aa35_2)#bind rows
p <- ggplot(DF,aes(x=ref.co.mean,y=aa35.co.mean,colour=id,group=id)) +
geom_errorbar(aes(ymin=aa35.co.mean-aa35.co.ci,
ymax=aa35.co.mean+aa35.co.ci), width =.5) +
xlab("Reference CO (ppm)") +
ylab("AA34 & 35 CO (ppm)") +
geom_smooth(method='lm', formula = y~x, se = FALSE) +
geom_point(size=2, shape = 21, fill="White") +
geom_abline(intercept = 0, slope = 1, color, linetype=2, color = "firebrick") +
ggtitle("CO Calibration @ 0% RH") +
theme(plot.title = element_text(hjust = 0.5)) +
annotate("rect", xmin = 4.80, xmax = 5.70, ymin = 0.70, ymax = 1.70,
fill="white", colour="red") +
annotate("text", x=5.25, y=1.50, label= "R^2 == 0.994", parse=T) +
annotate("text", x=5.25, y=1.20, label= "alpha == -0.9490", parse=T) +
annotate("text", x=5.25, y=0.90, label= "beta == 0.849", parse=T)+
theme_bw()
p
简介:
我有一个包含三种不同空气质量测量值的汇总统计数据的数据框。仪器名称为 aa34
、aa35
和 48c
。他们各自以 ppm 为单位测量一氧化碳。我有宽格式的数据,其中每个向量是三种仪器的平均值、标准偏差、标准误差或 95% 置信区间。
我想使用 ggplot()
和 geom_errorbar()
绘制这些汇总统计数据,但我在将数据转换为长格式并为 [= 中的颜色映射提供 ID 变量时遇到了一些问题16=]。我正在关注 this 教程。下面是我想重现的图(当然用有毒烟雾代替了豚鼠的牙齿数据)。我一直在尝试添加一个额外的 y 变量并让它们通过 ID
变量进行颜色协调。我想要的输出将用三个 id
向量中的两个替换示例中的 supp
向量,即包含 aa34
和 aa35
的向量。我相当于 dose
向量的是 ref.co.mean
,我们的 x
变量。我相当于 len
向量的是长格式的向量 aa34.co.mean
和 aa35.co.mean
。
数据:
## Here's what my data frame looks like.
## I know it's ugly, but if you copy and paste it into your console it should work!
df_cal <- structure(list(ref.co.mean = c(1.23638284617457, 1.46466241535712,
2.16020882959014, 2.55054760052641, 3.13141175081258, 3.86968879644661,
6.5914211520901), ref.co.sd = c(0.0196205483139859, 0.0229279198586359,
0.0172965018302434, 0.0164690175286326, 0.00583116470707786,
0.00975072766851073, 0.0388826652553337), ref.co.se = c(0.00346845569085442,
0.00193776290206006, 0.00166435666462165, 0.00127061228762621,
0.000583116470707786, 0.00229826855196908, 0.00614788918523735
), ref.co.ci = c(0.00707396201972773, 0.00383130164529687,
0.00329939297398704,
0.0025085329371034, 0.00115702958592763, 0.00484892279298878,
0.0124352796323718), id = c("48c", "48c", "48c", "48c", "48c",
"48c", "48c"), aa34.co.mean = c(0, 0.248857142857143, 0.823777777777778,
1.256, 1.886, 2.446, 4.54), aa34.co.sd = c(0, 0.0716567783084826,
0.0660714166547489, 0.0777970497665622, 0.0518459255872629, 0,
0.0690217357069497), aa34.co.se = c(0, 0.00605610310675521,
0.0063577250318932, 0.00600217269807407, 0.00518459255872628, 0,
0.0109132946446067), aa34.co.ci = c(0, 0.0119739921598931,
0.0126034483753748, 0.0118499152368743, 0.0102873564420935, 0,
0.0220742219853317), id = c("aa34", "aa34", "aa34", "aa34", "aa34", "aa34",
"aa34"), aa35.co.mean = c(0.2915625, 0.801035714285714, 1.39911111111111,
1.80436904761905, 2.45672, 3.02355555555556, 5.134975), aa35.co.sd =
c(0.0691998633940125, 0.0474980316455754, 0.0846624379229758,
0.0822798331713915, 0.0595577165445419,
0.0178768075145867, 0.0243007072942329), aa35.co.se = c(0.0122329231657723,
0.00401431635364878, 0.00814664688751334, 0.00634802694633388,
0.00595577165445419, 0.00421360393984362, 0.00384227919014218), aa35.co.ci =
c(0.0249492112853266, 0.00793701687349159, 0.0161497773125,
0.0125327252345785, 0.0118175430765459, 0.00888992723110191,
0.00777174323014678), id = c("aa35", "aa35", "aa35", "aa35",
"aa35", "aa35", "aa35")), .Names = c("ref.co.mean", "ref.co.sd",
"ref.co.se", "ref.co.ci", "id", "aa34.co.mean", "aa34.co.sd",
"aa34.co.se", "aa34.co.ci", "id", "aa35.co.mean", "aa35.co.sd",
"aa35.co.se", "aa35.co.ci", "id"), row.names = c(1L, 33L, 173L,
281L, 449L, 549L, 567L), class = "data.frame")
这是我的第一次尝试:
## This code only gets half of the job done...
## 95% Confidence Intervals for Error Bars:
p <- ggplot(df_cal, aes(x=ref.co.mean, y=aa34.co.mean)) +
theme_bw() +
geom_errorbar(aes(ymin=aa34.co.mean-aa34.co.ci,
ymax=aa34.co.mean+aa34.co.ci), width =.05) +
xlab("Reference CO (ppm)") +
ylab("AA34 CO (ppm)") +
geom_smooth(method='lm', formula = y~x, se = FALSE) +
geom_point(size=2, shape = 21, fill="White") +
geom_abline(intercept = 0, slope = 1, color, linetype=2, color = "firebrick") +
ggtitle("CO Calibration @ 0% RH") +
theme(plot.title = element_text(hjust = 0.5)) +
annotate("rect", xmin = 4.80, xmax = 5.70, ymin = 0.70, ymax = 1.70,
fill="white", colour="red") +
annotate("text", x=5.25, y=1.50, label= "R^2 == 0.994", parse=T) +
annotate("text", x=5.25, y=1.20, label= "alpha == -0.9490", parse=T) +
annotate("text", x=5.25, y=0.90, label= "beta == 0.849", parse=T)
p
提前致谢!
这里切换到长格式的问题是你有一个 length 7 的 x 轴变量和 2 个组合的 length 14 为 y 轴。因此,此解决方案绑定行,以便引用(x 轴)数据包含两次。然后就是在 ggplot
美学中使用 colour
和 group
的问题。
library(ggplot2)
df_aa34_2<-df_cal[,c(1:4,6:10)]#select first 'aa' group including reference data (48c)
df_aa35_2<-df_cal[,c(1:4,11:15)]#select second 'aa' group including reference data (48c)
names(df_aa34_2)<-names(df_aa35_2)#colnames must be the same for rbind function
DF<-rbind(df_aa34_2,df_aa35_2)#bind rows
p <- ggplot(DF,aes(x=ref.co.mean,y=aa35.co.mean,colour=id,group=id)) +
geom_errorbar(aes(ymin=aa35.co.mean-aa35.co.ci,
ymax=aa35.co.mean+aa35.co.ci), width =.5) +
xlab("Reference CO (ppm)") +
ylab("AA34 & 35 CO (ppm)") +
geom_smooth(method='lm', formula = y~x, se = FALSE) +
geom_point(size=2, shape = 21, fill="White") +
geom_abline(intercept = 0, slope = 1, color, linetype=2, color = "firebrick") +
ggtitle("CO Calibration @ 0% RH") +
theme(plot.title = element_text(hjust = 0.5)) +
annotate("rect", xmin = 4.80, xmax = 5.70, ymin = 0.70, ymax = 1.70,
fill="white", colour="red") +
annotate("text", x=5.25, y=1.50, label= "R^2 == 0.994", parse=T) +
annotate("text", x=5.25, y=1.20, label= "alpha == -0.9490", parse=T) +
annotate("text", x=5.25, y=0.90, label= "beta == 0.849", parse=T)+
theme_bw()
p