R 如何组合两个填充面积图?
我有两个数据帧:R 如何组合两个填充面积图?,r,plot,ggplot2,plotly,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.91666666666
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")
及
我想创建一个填充面积图,如图所示(该图称为“面积图的内部填充”)。我可以分别绘制df1
和df2
,但是我不能将它们放在一个绘图上。怎么做
这是我当前的代码:
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))
这给了我一个错误:
错误:第一个参数
data,必须是数据帧。
我检查了class(df1)
和class(df2)
返回data.frame
。所以,我真的不明白这个信息
预期结果类似于
更新1:
我尝试使用ggplot2
,并对数据结构进行了以下修改(但在本例中,图表看起来像条形图(带有非常细的条形),而不是填充区域图表):
更新2:
我刚刚意识到,如果我用数字代替月份名称,那么
ggplot2
是可行的,但是我如何才能保存月份名称呢???您正在尝试将第一个绘图“管道”到第二个绘图(因此基本上是尝试传递一个类plotly\u hash
,而不是data.frame
)。只需将第二个plot\u ly()
调用替换为add\u trace()
并指定data=df2
。您还需要在add_trace()
s调用中指定x=~month
。
下面是一个更简单的表示:
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)
其中:
您正在尝试将第一个绘图“管道化”到第二个绘图(因此基本上是尝试传递类
plotly\u hash
,而不是data.frame
)。只需将第二个plot\u ly()
调用替换为add\u trace()
并指定data=df2
。您还需要在add_trace()
s调用中指定x=~month
。
下面是一个更简单的表示:
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)
其中:
我认为这里的错误是不言自明的,你试图将第一个情节“管道化”到第二个情节中。您正在将一个类“plotly\u hash”传递给第二个plot。@StevenBeaupré:您知道这个问题的解决方案吗?只需将第二个
plot\u ly()
调用替换为add\u trace()
并指定。。。data=df2
(与上一个添加跟踪()相同
)@StevenBeaupré:好的,现在可以了,但我只看到一个填充区域图。也许ggplot2
更适合这种情况?我对ggplot2
的唯一问题是月份必须是数字才能显示填充面积图。但我想让它们成为月份名称。这对我来说很好。我使用的是plotly v4.5.2
我认为这里的错误是不言自明的,您试图将第一个绘图“管道化”到第二个绘图中。您正在将一个类“plotly\u hash”传递给第二个plot。@StevenBeaupré:您知道这个问题的解决方案吗?只需将第二个plot\u ly()
调用替换为add\u trace()
并指定。。。data=df2
(与上一个添加跟踪()相同
)@StevenBeaupré:好的,现在可以了,但我只看到一个填充区域图。也许ggplot2
更适合这种情况?我对ggplot2
的唯一问题是月份必须是数字才能显示填充面积图。但我想让它们成为月份名称。这对我来说很好。我正在使用plotly v4.5.2谢谢,但是否可以为填充区域添加一些透明度,以便可以看到绿色图表的右侧。现在它和蓝色的重叠了。谢谢,但是有没有可能给填充的区域增加一些透明度,这样绿色图表的右侧就可以看到了。现在它与蓝色的重叠。
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)