2016-07-22 89 views
1

我想使用ggplot2繪製兩個每週平均時間序列(來自表示不同儀器的兩個不同數據框)。這應該很簡單,但我必須錯過一些東西。我看着下面的帖子:使用ggplot2具有多個數據框的geom_point和geom_errorbar

using-both-geom-point-and-geom-line-for-multiple-x-in-ggplot2 object-not-found-error-with-ggplot2-when-adding-shape-aesthetic

和良好的老cookbook for r但我一直錯誤之後遇到了錯誤。我使用的dataframes來自使用ddply總結,他們在這裏爲重複性:

mean_TS_Cond_use<- 
structure(list(week_DOY = c(207, 207, 230, 230, 237, 237, 237, 
239, 239, 239, 246, 246, 246, 253, 253, 253, 260, 267, 267, 281, 
281, 281, 288, 288, 288, 295, 295, 316, 316, 323, 323, 330, 330, 
330, 337, 337), Leaf.age.ordered = structure(c(1L, 4L, 1L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 3L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 1L, 2L, 3L, 2L, 3L 
), .Label = c("young", "mature", "old", "old1"), class = "factor"), 
    week_N_Cond = c(7L, 2L, 7L, 2L, 4L, 6L, 3L, 6L, 2L, 10L, 
    3L, 6L, 7L, 2L, 5L, 4L, 1L, 3L, 1L, 3L, 3L, 6L, 4L, 11L, 
    2L, 5L, 4L, 4L, 6L, 2L, 3L, 6L, 20L, 7L, 6L, 2L), week_mean_Cond = c(46.675, 
    28, 38.125, 59.1, 23.5333333333333, 101.5, 58.1333333333333, 
    16.8, 35.5, 62.4, 31.4, 144, 49.3, 49.7, 55.6333333333333, 
    57.65, 7.3, 4.74, NaN, 69.4, 112.3, 80.35, 47.85, 21.6416666666667, 
    6.41, 70.3333333333333, 59.1, 41.6, 24.9666666666667, 64.3, 
    NaN, 39.1, 95.8909090909091, 44.7333333333333, 20.9733333333333, 
    40), week_sd_Cond = c(17.6941374471885, NA, 24.1760728820874, 
    17.1119841047145, 18.1934970067146, 86.4448379025607, 43.4743985965687, 
    NA, NA, NA, NA, 1.4142135623731, 9.61665222413704, NA, 30.8034630087809, 
    28.0721392131059, NA, 1.40007142674936, NA, 31.5912962697006, 
    23.0774781984514, 20.545478010177, 5.30330085889911, 13.7910353732657, 
    NA, 9.97513575513302, 1.69705627484771, 5.23259018078045, 
    6.02522475376092, NA, NA, 9.33380951166242, 59.2789584008602, 
    7.7693843599949, 20.8945957925329, 33.799704140717)), .Names = c("week_DOY", 
"Leaf.age.ordered", "week_N_Cond", "week_mean_Cond", "week_sd_Cond" 
), row.names = c(NA, -36L), class = "data.frame") 

mean_TS_Gs_use<-structure(list(week_DOY = c(232, 232, 239, 239, 246, 246, 246, 
267, 267, 267, 281, 316, 316, 316, 323, 323, 330, 330, 330, 337, 
337), Leaf.age.ordered = structure(c(2L, 3L, 1L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 3L, 1L, 2L, 3L, 2L, 3L, 1L, 2L, 3L, 2L, 3L), .Label = c("young", 
"mature", "old"), class = "factor"), week_N_GS = c(56L, 49L, 
30L, 30L, 55L, 21L, 54L, 7L, 21L, 19L, 6L, 3L, 8L, 4L, 30L, 15L, 
36L, 99L, 70L, 52L, 23L), week_mean_GS = c(73.2017857142857, 
170.422448979592, 88.1133333333333, 66.4866666666667, 125.794545454545, 
103.247619047619, 70.0981481481481, 154.414285714286, 258.757142857143, 
114.073684210526, 254.15, 167.5, 175.8125, 136.25, 87.9866666666667, 
46.46, 112.455555555556, 111.778787878788, 88.4242857142857, 
169.346153846154, 160.895652173913), week_sd_GS = c(27.4044421818562, 
112.736252423718, 30.7610561377961, 26.4143473727146, 98.1052296302704, 
59.4644819959581, 43.7727299045695, 77.6537062556456, 84.1063943551771, 
67.674177268777, 79.52214157076, 47.4155037935906, 45.4656365527071, 
9.46449505608548, 58.2085118395473, 17.0402800111132, 33.7885563420893, 
97.9779549056591, 76.6287028293478, 130.657736481864, 93.5849467220259 
)), .Names = c("week_DOY", "Leaf.age.ordered", "week_N_GS", "week_mean_GS", 
"week_sd_GS"), row.names = c(NA, -21L), class = "data.frame") 

一切都是常規的geom_point和geom_errorbar與第一數據框:

mGts<-ggplot(data=mean_TS_Cond_use, aes(x = week_DOY, y = week_mean_Cond, color=Leaf.age.ordered, ymax = week_mean_Cond + week_sd_Cond, ymin=week_mean_Cond - week_sd_Cond))+ 
    geom_point(size=4) + 
    geom_errorbar() 
mGts 

我嘗試添加新時間序列在新數據幀是這樣的:

mGts_situ<-mGts + 
    geom_point(aes(x = week_DOY, y = week_mean_GS, color=Leaf.age.ordered), data=mean_TS_Gs_use, size=4, shape=18) + 
    geom_errorbar(aes(ymax = week_mean_GS + week_sd_GS, ymin=week_mean_GS - week_sd_GS), data=mean_TS_Gs_use) 
mGts_situ 

但我得到一個錯誤,「對象「week_mean_Cond找不到。」由於ggplot從第一個數據框中「尋找」一個對象,我試圖在aes調用之前擺脫繼承的aes並移動'data ='的定義。 (我還定義了ggplot調用之外的錯誤欄限制和其他小的更改)。這裏是新的嘗試:

Gs_upper<-mean_TS_Gs_use$week_mean_GS + mean_TS_Gs_use$week_sd_GS 
Gs_lower<-mean_TS_Gs_use$week_mean_GS - mean_TS_Gs_use$week_sd_GS 

mGts_situ<-mGts + 
    geom_point(data=mean_TS_Gs_use, inherit.aes = FALSE, aes(x = week_DOY, y = week_mean_GS, color=Leaf.age.ordered, ymax = Gs_upper, ymin = Gs_lower), size=4, shape=18) + 
    geom_errorbar()+ 
    scale_x_continuous("DOY", limits = c(200, 350)) + 
    scale_y_continuous("Weekly Mean", limits = c(0, 345))+ 
    theme_bw() 
mGts_situ 

哪個不給錯誤有關的任何物品,但它仍然不會顯示在新的數據集(「mean_TS_Gs_use」)的錯誤吧。你可以看到,第一繪圖數據框(圓圈)有errorbars,但第二繪製數據框(三角形)不: Two time series: one missing errorbars

回答

2

你不能讓你的蛋糕,並與inherit.aes吃太多,你要麼繼承一切或指定一切。

在你的情況,你的新數據爲yminymax不同的列名,因此,我們確實需要設置inherit.aes = Fgeom_errorbar層,但後來我們就需要指定所有的美學。

我們可以拯救自己,如果在原來的情節yminymax只設定在geom_errorbar級別,而不是最高級別了一點小麻煩:

mGts <- 
    ggplot(
     data = mean_TS_Cond_use, 
     aes(
      x = week_DOY, 
      y = week_mean_Cond, 
      color = Leaf.age.ordered 
     ) 
    ) + 
    geom_point(size = 4) + 
    geom_errorbar(
     # move these down here 
     aes(ymax = week_mean_Cond + week_sd_Cond, 
      ymin = week_mean_Cond - week_sd_Cond) 
    ) 

隨着這種變化,新geom_point層是罰款,但我們會設置inherit.aes = F和respecify美學爲geom_errorbar

mGts_situ <- mGts + 
    geom_point(
     mapping = aes(
      x = week_DOY, 
      y = week_mean_GS, 
      color = Leaf.age.ordered 
     ), 
     data = mean_TS_Gs_use, 
     size = 4, 
     shape = 18 
    ) + 
    geom_errorbar(
     mapping = aes(
      ymax = week_mean_GS + week_sd_GS, 
      ymin = week_mean_GS - week_sd_GS, 
      x = week_DOY, 
      color = Leaf.age.ordered 
     ), 
     data = mean_TS_Gs_use, 
     inherit.aes = FALSE 
    ) 
mGts_situ 
+0

啊哈!謝謝@Gregor,這是有效的(在第一個aes中,在Leaf.age.ordered之後拋棄逗號之後)。 – user2860703

1

我覺得這個情節會更容易地創建,如果我們結合兩個數據幀s:

library(dplyr) 
library(ggplot2) 

重命名列以便我們在兩個數據框中具有通用名稱。添加新列以區分源數據來自哪個數據幀。然後將二者結合起來的數據幀:

mean_TS_Cond_use = mean_TS_Cond_use %>% 
    rename(week_mean=week_mean_Cond, week_sd=week_sd_Cond) %>% 
    mutate(Source="Cond") 

mean_TS_Gs_use = mean_TS_Gs_use %>% 
    rename(week_mean=week_mean_GS, week_sd=week_sd_GS) %>% 
    mutate(Source="Gs") 

df = bind_rows(list(mean_TS_Cond_use, mean_TS_Gs_use)) 

Leaf.age.ordered復位命令:

df$Leaf.age.ordered = factor(df$Leaf.age.ordered, levels=c("young","mature","old","old1")) 

轉換week_DOY因素(這樣躲着將正常工作):

df$week_DOY_f = factor(df$week_DOY, levels=min(df$week_DOY):max(df$week_DOY)) 

情節與躲着避免交疊。該group審美是沒有得到躲着權:

pd = position_dodge(0.5) 

ggplot(df, aes(x=week_DOY_f, 
       y=week_mean, colour=Source, fill=Source, 
       ymax=week_mean + week_sd, ymin=week_mean - week_sd)) + 
    geom_errorbar(position=pd, aes(group=interaction(Leaf.age.ordered, Source)), 
       width=0.1, alpha=0.5) + 
    geom_point(position=pd, aes(group=interaction(Leaf.age.ordered, Source), 
           size=Leaf.age.ordered), 
      pch=21, color="black", stroke=0.2) + 
    theme_bw() + 
    scale_size_discrete(range=c(1,3)) + 
    guides(size=guide_legend(override.aes=list(fill="grey30"))) 

情節還是很忙碌,但希望更易於閱讀:

enter image description here

也許文字標籤將更好地爲區分年齡:

ggplot(df, aes(x=week_DOY_f, 
       y=week_mean, colour=Source, 
       ymax=week_mean + week_sd, ymin=week_mean - week_sd)) + 
    geom_errorbar(position=pd, aes(group=interaction(Leaf.age.ordered, Source)), 
           width=0.1, alpha=0.5) + 
    geom_label(position=pd, aes(label=toupper(substr(Leaf.age.ordered,1,1)), 
           group=interaction(Leaf.age.ordered, Source)), 
      fontface="bold", fill="white", label.size=0, size=2.5, 
      label.padding=unit(0.05,"lines"), show.legend=FALSE) + 
    theme_bw() + 
    guides(colour=guide_legend(override.aes=list(alpha=1,lwd=1))) 

enter image description here

+0

很好的全方位服務答案在這裏! – Gregor

+0

整潔的解決方案!很高興看到你如何使用'閃避'和文本標籤。 – user2860703

相關問題