ggplot2:当一个绘图有错误条而一个没有错误时,如何将2个图组合成一个

时间:2017-06-20 02:04:41

标签: r ggplot2

我正在尝试使用facet_wrap中的ggplot2绘制彼此相邻的两个数据框。然而,其中一个图有误差条,另一个没有。我可以绘制错误条很好的那个,如果我不包含错误条,我可以将两个数据框彼此相邻绘制。我无法在一张图上用错误条将两者相邻。

我的数据的一个子集:

df1 <- structure(list(farm = c("F1", "F1", 
"F1", "F1"), index = structure(c(1L, 
1L, 4L, 4L), .Label = c("HT", "Mid-T", "Outside Mid-T", "Outside South"
), class = "factor"), sensorheight = c(1L, 1L, 1L, 1L), yrmonth = structure(c(1491004800, 
1496275200, 1491004800, 1496275200), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), monthindex = structure(c(2L, 2L, 2L, 2L), .Label = c("Spring", 
"Winter"), class = "factor"), N = c(2, 2, 1, 1), TempC = c(2.06446759259259, 
6.68402777777778, 1.32268518518518, 5.63194444444445), sd = c(1.17081824208967, 
0.034373246307681, NA, NA), se = c(0.827893518518518, 0.0243055555555567, 
NA, NA), ci = c(10.5193845460483, 0.308831365115372, NA, NA)), .Names = c("farm", 
"index", "sensorheight", "yrmonth", "monthindex", "N", "TempC", 
"sd", "se", "ci"), row.names = c(7L, 9L, 20L, 22L), class = "data.frame")

df2 <- structure(list(farm = c("F2", "F2", "F2", 
"F2", "F2", "F2"), location = c("Outside", 
"Outside", "Outside", "Permanent", "Permanent", "Permanent"), 
    sensorheight = c(1L, 1L, 1L, 1L, 1L, 1L), yrmonth = structure(c(1459468800, 
    1462060800, 1464739200, 1459468800, 1462060800, 1464739200
    ), class = c("POSIXct", "POSIXt"), tzone = "UTC"), TempC = c(3.43055555555556, 
    5.34520609318996, 10.6064814814815, 3.21701388888889, 5.30264336917563, 
    9.8587962962963)), .Names = c("farm", "location", "sensorheight", 
"yrmonth", "TempC"), row.names = c(1L, 2L, 3L, 7L, 8L, 9L), class = "data.frame")

这是我可以绘制的内容:

ggplot(df1, 
       aes(x=yrmonth,y=TempC, colour=index)) + 
  geom_line() +
  geom_errorbar(aes(ymin=TempC-sd, ymax=TempC+sd), size = .5) 

ggplot() + 
  geom_line(data = df1, aes(x=yrmonth,y=TempC)) +
  geom_line(data = df2, aes(x=yrmonth,y=TempC)) +
  facet_wrap( ~ farm, scales = "free_x")

当我试图将它们一起绘制时:

ggplot() + 
  geom_line(data = df1, aes(x=yrmonth,y=TempC)) +
  geom_errorbar(aes(ymin=TempC-sd, ymax=TempC+sd), size = .5)  +
  geom_line(data = df2, aes(x=yrmonth,y=TempC)) +
  facet_wrap( ~ farm, scales = "free_x")

我收到以下错误:

Error in if (empty(data)) { : missing value where TRUE/FALSE needed

2 个答案:

答案 0 :(得分:2)

无需进行任何数据操作,您只需稍微更改ggplot电话。这应该有效:

ggplot(df1, aes(x=yrmonth)) + 
  geom_line(aes(y=TempC)) +
  geom_errorbar(aes(ymin=TempC-sd, ymax=TempC+sd), size = .5)  +
  geom_line(data = df2, aes(x=yrmonth,y=TempC)) +
  facet_wrap( ~ farm, scales = "free_x")

enter image description here

关于我更改内容的一些注意事项:我在df1调用中添加了ggplot(),以便facet_wrap知道从哪里获取farm对象。我还在aes(x=yrmonth)调用中添加了ggplot(),以便geom_errorbar可以继承它。这节省了一些额外的打字。

答案 1 :(得分:1)

另一种方法是,如果将两个data.frames(df1df2)合并在一起,则可以执行此操作。

library(tidyverse)
#Merge the two data.frames together
df1 %>% dplyr::full_join(., df2) %>% 
  #plot
  ggplot(., aes(yrmonth, TempC))+
  geom_line()+
  geom_errorbar(aes(ymin=TempC-sd, ymax=TempC+sd), size = .5)+
  facet_wrap( ~ farm, scales = "free_x")

enter image description here