Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何包括;“思考细胞”-类似于ggplot2中生成的瀑布图中的百分比变化_R_Ggplot2_Waterfall - Fatal编程技术网

R 如何包括;“思考细胞”-类似于ggplot2中生成的瀑布图中的百分比变化

R 如何包括;“思考细胞”-类似于ggplot2中生成的瀑布图中的百分比变化,r,ggplot2,waterfall,R,Ggplot2,Waterfall,我试图在我的公司里建立R作为数据可视化工具。我所在部门使用的典型图形类型是瀑布图() 在R中,有一些包和提示让ggplot生成瀑布图(),我已经使用过了 不幸的是,所使用的瀑布图的一个常见特性是带有箭头的注释,用于指示步骤中的百分比变化 请参见下面的示例: 或者在这段视频中() 用于生成此类绘图的软件是think cell(),它是Excel和Powerpoint的附加组件 我的问题是我不知道如何着手解决这个话题。我的第一个想法是朝着这个方向: 使用geom_段生成箭头和方框 使用ggplo

我试图在我的公司里建立R作为数据可视化工具。我所在部门使用的典型图形类型是瀑布图()

在R中,有一些包和提示让ggplot生成瀑布图(),我已经使用过了

不幸的是,所使用的瀑布图的一个常见特性是带有箭头的注释,用于指示步骤中的百分比变化

请参见下面的示例:

或者在这段视频中()

用于生成此类绘图的软件是think cell(),它是Excel和Powerpoint的附加组件

我的问题是我不知道如何着手解决这个话题。我的第一个想法是朝着这个方向:

  • 使用geom_段生成箭头和方框
  • 使用ggplot的注释功能将文本放置在箭头或框中
  • 根据提供给瀑布图的数据自动计算位置
我可以问您,您是否有其他想法/想法来在ggplot中实现这些图形


向马库斯致以最诚挚的问候这里是我将采取的方法的一个例子

第一步。选择应该添加的元素,然后一次添加一个。 假设我们从这个简单的图表开始:

df <- data.frame(x = c(2007, 2008, 2009),
                 y = c(100, 120, 140))
ggplot(df, aes(x, y, label = y)) +
  geom_col() +
  geom_text(vjust = -0.5)

现在,我以增量方式添加层,直到它看起来像我想要的:

# Semi-manual proof of concept
ggplot(df, aes(x, y, label = y)) +
  geom_col() +
  geom_text(vjust = -0.5) +
  scale_y_continuous(expand = expand_scale(add = c(10, 50))) + # Add 50 y padding

  # Line with arrow
  geom_segment(aes(x    = df$x[3], y    = df$y[3] + 50,
                   xend = df$x[3], yend = df$y[3] + 50), 
               arrow = arrow(length = unit(0.02, "npc"), type = "closed")) +

  # Background box
  geom_tile(aes(x = mean(c(df$x[3], df$x[3])),
                y = mean(c(df$y[3], df$y[3])) + 50, width = 1, height = 40), 
            fill = "white", color = "black", size = 0.5) +

  # Text
  geom_text(aes(x = mean(c(df$x[3], df$x[3])),
                y = mean(c(df$y[3], df$y[3])) + 50,
                label = paste0("CAGR\n", 
                               df$x[3], "-", df$x[3], "\n", 
                               scales::percent((df$y[3] / df$y[3]) ^ (1/(df$x[3]-df$x[3])) - 1))))

第二步。把它变成一个函数 现在,我将CAGR相关层移动到一个函数中,用函数参数替换大部分常量

add_CAGR <- function(df, first_val_pos, second_val_pos, 
                     y_offset, box_width = 1, box_height) {
  list(
    # Line with arrow
    geom_segment(aes(x    = df$x[first_val_pos], 
                     xend = df$x[second_val_pos], 
                     y    = df$y[first_val_pos]  + y_offset,
                     yend = df$y[second_val_pos] + y_offset), 
                 arrow = arrow(length = unit(0.02, "npc"), type = "closed")), 

      # Background box
      geom_tile(aes(x = mean(c(df$x[first_val_pos], df$x[second_val_pos])),
                    y = mean(c(df$y[first_val_pos], df$y[second_val_pos])) + y_offset, 
                    width = box_width, height = box_height), 
                fill = "white", color = "black", size = 0.5),

      # Text
      geom_text(aes(x = mean(c(df$x[first_val_pos], df$x[second_val_pos])),
                    y = mean(c(df$y[first_val_pos], df$y[second_val_pos])) + y_offset,
                    label = paste0("CAGR\n", 
                                   df$x[first_val_pos], "-", df$x[second_val_pos], "\n", 
                                   scales::percent((df$y[second_val_pos] / df$y[1]) ^ 
                                                     (1/(df$x[second_val_pos]-df$x[first_val_pos])) - 1))),
                lineheight = 0.8)
  )
}

或者在前两个条之间有相同的东西:

ggplot(df, aes(x, y, label = y)) +
  geom_col() +
  geom_text(vjust = -0.5) +
  scale_y_continuous(expand = expand_scale(add = c(0, 50))) + # Add 50 y padding
  add_CAGR(df, first_val_pos = 1, second_val_pos = 2, 
           y_offset = 50,
           box_width = 0.7, box_height = 40)

我将使用一个函数将必要的图层添加到绘图中,并将源表、开始和结束年份以及指定垂直偏移的某些参数作为参数。它可以有以下几层:1)带箭头的几何图形段;2) 椭圆(可能有一个有效?);3) 带有paste0之类内容的文本框(“CAGR\n”,第一年,“-”,最后一年,“\n”,[变化系数]^[1/年过去]-1%>%[格式代码])亲爱的Jon,非常感谢您的详细回答。顺致敬意,马库斯
ggplot(df, aes(x, y, label = y)) +
  geom_col() +
  geom_text(vjust = -0.5) +
  scale_y_continuous(expand = expand_scale(add = c(0, 50))) + # Add 50 y padding
  add_CAGR(df, first_val_pos = 1, second_val_pos = 3, 
           y_offset = 50,
           box_width = 0.7, box_height = 40)
ggplot(df, aes(x, y, label = y)) +
  geom_col() +
  geom_text(vjust = -0.5) +
  scale_y_continuous(expand = expand_scale(add = c(0, 50))) + # Add 50 y padding
  add_CAGR(df, first_val_pos = 1, second_val_pos = 2, 
           y_offset = 50,
           box_width = 0.7, box_height = 40)