如何将两个填充区域拼凑起来?

时间:2016-10-22 10:53:41

标签: r plot ggplot2 plotly

我有两个数据框:

dput(df1)

structure(list(month = structure(1:12, .Label = c("Jan", "Feb", 
"Mar", "Apr", "May", "Jun", "Jul", "Ago", "Sep", 
"Oct", "Nov", "Dec"), class = c("ordered", "factor"
)), max_wt_per_month = c(145.433333333333, 103, 111.916666666667, 
190.85, 154.683333333333, 136.066666666667, 168.533333333333, 
94.2666666666667, 180, 146.233333333333, 157.4, 179.933333333333
), min_wt_per_month = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.05
), avg_wt_per_month = c(8.62182932965783, 8.6232935986297, 10.0719732082026, 
10.0015567184692, 14.7826000421388, 12.4903419254567, 15.2629113635624, 
15.2463245173283, 14.1524047780697, 7.89863122852994, 6.61470640814081, 
8.39703918722787)), .Names = c("month", "max_wt_per_month", "min_wt_per_month", 
"avg_wt_per_month"), row.names = c(5L, 4L, 9L, 1L, 8L, 7L, 6L, 
2L, 12L, 11L, 10L, 3L), class = "data.frame")

dput(df2)

structure(list(month = structure(1:12, .Label = c("Jan", "Feb", 
"Mar", "Apr", "May", "Jun", "Jul", "Ago", "Sep", 
"Oct", "Nov", "Dec"), class = c("ordered", "factor"
)), max_wt_per_month = c(113.2, 96.3833333333333, 109.433333333333, 
176.933333333333, 151.333333333333, 122.233333333333, 174.583333333333, 
96.65, 192.05, 164.483333333333, 170.966666666667, 192.883333333333
), min_wt_per_month = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    avg_wt_per_month = c(17.6627541466025, 16.6349269588313, 
    21.1785870516185, 20.4826873385013, 26.466982862202, 21.9996887643946, 
    28.8988648947951, 31.9911411411411, 30.0452008346375, 17.4505074160812, 
    17.3421645263354, 21.4158029053789)), .Names = c("month", 
"max_wt_per_month", "min_wt_per_month", "avg_wt_per_month"), row.names = c(5L, 
4L, 9L, 1L, 8L, 7L, 6L, 2L, 12L, 11L, 10L, 3L), class = "data.frame")

我想创建一个填充区域图,如here所示(一个叫做"区域图的内部填充")。我可以单独绘制df1df2,但我不能将它们放在一个单独的地块上。怎么做?

这是我目前的代码:

library(plotly)

plot_ly(df1, x = ~month, y = ~max_wt_per_month, type = 'scatter', mode = 'lines',
        line = list(color = 'rgba(0,100,80,1)'),
        showlegend = FALSE, name = 'Max, 1') %>%
  add_trace(y = ~min_wt_per_month, type = 'scatter', mode = 'lines',
            fill = 'tonexty', fillcolor='rgba(0,100,80,0.2)', line = list(color = 'rgba(0,100,80,1)'),
            showlegend = FALSE, name = 'Min, 1') %>%

  plot_ly(df2, x = ~month, y = ~max_wt_per_month, type = 'scatter', mode = 'lines',
          line = list(color = 'rgba(168, 216, 234, 1)'),
          showlegend = FALSE, name = 'Max, 2') %>%
  add_trace(y = ~min_wt_per_month, type = 'scatter', mode = 'lines',
            fill = 'tonexty', fillcolor='rgba(168, 216, 234, 1)', line = list(color = 'rgba(168, 216, 234, 1)'),
            showlegend = FALSE, name = 'Min, 2') %>%

  layout(title = "...",
         paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
         xaxis = list(title = "months",
                      gridcolor = 'rgb(255,255,255)',
                      showgrid = TRUE,
                      showline = FALSE,
                      showticklabels = TRUE,
                      tickcolor = 'rgb(127,127,127)',
                      ticks = 'outside',
                      zeroline = FALSE),
         yaxis = list(title = "wt",
                      gridcolor = 'rgb(255,255,255)',
                      showgrid = TRUE,
                      showline = FALSE,
                      showticklabels = TRUE,
                      tickcolor = 'rgb(127,127,127)',
                      ticks = 'outside',
                      zeroline = FALSE))

它给了我一个错误:

Error: First argument,的数据, must be a data frame.

我检查了class(df1)class(df2)返回data.frame。所以,不要理解这个消息。

预期结果类似于this one

更新1:

我尝试在数据结构中使用ggplot2进行以下修改(但在这种情况下,图表看起来像条形图(非常细条),而不是填充区域图表):

df1$type = "1"
df2$type = "2"

data_merged = rbind(df1,df2)

library(ggplot2)

ggplot(data_merged, aes(x=month, y=max_wt_per_month, fill=as.factor(type))) +
+ geom_area(colour="black", size=.2, alpha=.4) +
+     scale_fill_brewer(palette="Greens", breaks=rev(levels(as.factor(data_merged$type))))

更新2:

我刚刚意识到ggplot2如果我用数字替换月份名称,但我怎么能保留月份名称?

1 个答案:

答案 0 :(得分:0)

你试图将你的第一个情节“管道”到你的第二个(所以基本上试图传递一个类plotly_hash而不是data.frame)。只需按plot_ly()替换您的第二次add_trace()来电,然后指定data = df2。您还需要在x = ~month个电话中指定add_trace()。 这是一个更简单的表示:

plot_ly(df1, 
        x = ~month, 
        y = ~max_wt_per_month, 
        type = 'scatter', 
        mode = 'lines',
        line = list(color = 'rgba(0,100,80,1)'),
        showlegend = FALSE) %>%
  add_trace(x = ~month,
            y = ~min_wt_per_month, 
            type = 'scatter', 
            mode = 'lines',
            fill = 'tonexty',
            showlegend = FALSE) %>%
  add_trace(x = ~month, 
            y = ~max_wt_per_month, 
            type = 'scatter', 
            mode = 'lines',
            showlegend = FALSE, 
            data = df2) %>%
  add_trace(x = ~month,
            y = ~min_wt_per_month, 
            type = 'scatter', 
            mode = 'lines',
            fill = 'tonexty', 
            showlegend = FALSE, 
            data = df2)

给出了:

enter image description here