Plot.ly 和 R 中的堆积面积图 [英] Stacked Area Chart in Plot.ly and R

查看:28
本文介绍了Plot.ly 和 R 中的堆积面积图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Plot.ly 有一个 教程 对于 Python:

Plot.ly has a tutorial on this for Python:

# Add original data
x=['Winter', 'Spring', 'Summer', 'Fall']

y0_org=[40, 80, 30, 10]
y1_org=[20, 10, 10, 10]
y2_org=[40, 10, 60, 80]

# Add data to create cumulative stacked values
y0_stck=y0_org
y1_stck=[y0+y1 for y0, y1 in zip(y0_org, y1_org)]
y2_stck=[y0+y1+y2 for y0, y1, y2 in zip(y0_org, y1_org, y2_org)]

R 似乎没有任何类似的教程.我尝试使用 R 的填充区域绘图教程,但未能构建堆叠图.

R doesn't seem to have any similar tutorial. I tried to play with the filled area plot tutorial for R, but failed to build a stacked plot.

library(plotly)
p <- plot_ly(x = c(1, 2, 3, 4), y = c(0, 2, 3, 5), fill = "tozeroy")
add_trace(p, x = c(1, 2, 3, 4), y = c(3, 5, 1, 7), fill = "tonexty")

如何在 R/Plot.ly 中复制上述代码来创建堆积面积图?非常感谢!

How can I replicate the code above in R/ Plot.ly to create a stacked area chart? Thanks a lot!

推荐答案

我是这样搞定的:

library(plotly)

x <- c('Winter', 'Spring', 'Summer', 'Fall')
y0 <- c(40, 80, 30, 10)
y1 <- c(20, 10, 10, 10)
y2 <- c(40, 10, 60, 80)

y1 <- y0 + y1
y2 <- y1 + y2

p <- ggplot() + 
  geom_area(aes(x, y2, fill = 'green')) +
  geom_area(aes(x, y1, fill = 'blue')) +
  geom_area(aes(x, y0, fill = 'red'))

ggplotly(p)

这篇关于Plot.ly 和 R 中的堆积面积图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆