2017-06-20 18 views
1

我想繪製兩個數據框,使用中的facet_wrap彼此相鄰。然而其中一塊地塊有誤差線,另一塊沒有。我可以用錯誤條來繪製一個錯誤條,如果我不包含錯誤條,我可以將兩個數據框繪製在一起。我一直無法在一張圖上用誤差線來繪製兩個彼此相鄰的圖。ggplot2:如何將2個繪圖合併爲一個,當一個繪圖有誤差條,而另一個沒有時

我的數據的一個子集:

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

不需要做任何數據操作,你只需要在你的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

的幾個注意事項是什麼改變了我:我添加df1ggplot()呼叫使facet_wrap知道在哪裏可以得到從farm對象。我還在ggplot()呼叫中添加了aes(x=yrmonth),以便geom_errorbar可以繼承它。這節省了一些額外的輸入。

+0

謝謝!效果很好。隨後我想添加'color = index,group = index',以便通過索引爲每行添加顏色。我嘗試將它添加到'ggplot'中的'aes()'或兩個'geom_lines',但它不起作用。 – phaser

+0

@phaser,我想你需要在第一個'geom_line' b/c中指定那些第二個'geom_line'沒有的和'index'列 –

1

另一種方法是,如果將兩個數據幀(df1,df2)合併在一起,則可以執行此操作。

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

相關問題