【问题标题】:How to make a double cumulative graph in R如何在R中制作双累积图
【发布时间】:2019-03-19 04:22:06
【问题描述】:

如何在r中制作这种类型的图表?

Product Revenue Quantity    Rev Cum Quat Cum
Product A   50% 40% 50% 40%
Product B   25% 30% 75% 70%
Product C   15% 20% 90% 90%
Product D   10% 10% 100%    100%

【问题讨论】:

  • 两件事:1)这看起来像是一个家庭作业问题,你要求人们为你做。到目前为止,您自己尝试过什么? 2) 你标记了这个ggplot2plotlyggvis...你实际使用的是哪个?

标签: r ggplot2 plotly ggvis


【解决方案1】:

geom_rect 采用 4 个坐标并生成一个矩形。 mutate_at(-1funs(...) 作为第二个参数为除第 1 列之外的每一列创建新变量,函数在 funs 中指定。 mutate_at(vars(matches('cum')) 也一样,但在这种情况下,只会为名称包含 'cum' 的变量创建新列。

library(tidyverse)

df <- read_table("
Product Revenue Quantity    Rev Cum Quat Cum
Product_A   50% 40% 50% 40%
Product_B   25% 30% 75% 70%
Product_C   15% 20% 90% 90%
Product_D   10% 10% 100%    100%
")[1:3]

df %>% 
  mutate_at(-1, funs(cum = cumsum(parse_number(.)))) %>% 
  mutate_at(vars(matches('cum')), funs(lag = lag(., default = 0))) %>% 
  ggplot() +
    geom_rect(aes(xmin = Revenue_cum_lag, 
                  ymin = Quantity_cum_lag, 
                  xmax = Revenue_cum, 
                  ymax = Quantity_cum))

【讨论】:

猜你喜欢
  • 2016-11-24
  • 2021-12-19
  • 2020-11-17
  • 2014-06-21
  • 2021-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
相关资源
最近更新 更多