【问题标题】:Drawing elements (arrows & circle) in ggplot (R) to show the difference between two bars在 ggplot (R) 中绘制元素(箭头和圆圈)以显示两个条形之间的差异
【发布时间】:2019-05-02 09:34:51
【问题描述】:

我正在尝试使用 ggplot 在 R 中创建一个绘图,以一种很好的方式显示我的两个条形之间的差异。

我找到了一个示例,它完成了我想要的部分功能,但我有两个主要问题:

  1. 它基于比较几组条形图,但我只有两个,所以我添加了一个包含它们的组。
  2. 我想把箭头画成更好的形状。我附上了一张图片。

代码:

transactions <- c(5000000, 1000000)
time <- c("Q1","Q2")
group <- c("A", "A")

data <- data.frame(transactions, time, group)

library(ggplot2)

fun.data <- function(x){
  print(x)
  return(data.frame(y = max(x) + 1,
                    label = paste0(round(diff(x), 2), "cm")))
}

ylab <- c(2.5, 5.0, 7.5, 10)

gg <- ggplot(data, aes(x = time, y = transactions, fill = colors_hc[1], label = round(transactions, 0))) +
  geom_bar(stat = "identity", show.legend = FALSE) +
  geom_text(position = position_dodge(width = 0.9),
            vjust = 1.1) +
  geom_line(aes(group = group), position = position_nudge(0.1),
            arrow = arrow()) +
  stat_summary(aes(x = group, y = transactions),
               geom = "label",
               fun.data = fun.data,
               fontface = "bold", fill = "lightgrey",
               inherit.aes = FALSE) +
  expand_limits(x = c(0, NA), y = c(0, NA)) +
  scale_y_continuous(labels = paste0(ylab, "M"),
                     breaks = 10 ^ 6 * ylab) 

gg

我瞄准的箭头:

我在哪里(忽略丑陋,还没有样式):

【问题讨论】:

标签: r ggplot2


【解决方案1】:

这行得通,但你仍然需要玩一下轴(或者更确切地说是美化它们)

library(dplyr)
library(ggplot2)    
transactions <- c(5000000, 1000000)
time <- c("Q1","Q2")
group <- c("A", "A")

my_data <- data.frame(transactions, time, group)

fun.data <- function(x){
  return(data.frame(y = max(x) + 1,
                    label = as.integer(diff(x))))
}

my_data %>% 
ggplot(aes(x = group, y = transactions, fill = time)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  geom_text(aes(label = as.integer(transactions)), 
            position = position_dodge(width = 0.9),
            vjust = 1.5) +
  geom_line(aes(group = group), position = position_nudge(0.1),
            arrow = arrow()) +
  stat_summary(aes(x = group, y = transactions), 
               geom = "label",
               size = 5,
               position = position_nudge(0.05),
               fun.data = fun.data,
               fontface = "bold", fill = "lightgrey",
               inherit.aes = FALSE)

编辑2:

y_limit <- 6000000
my_data %>% 
  ggplot(aes(x = time, y = transactions)) + 
  geom_bar(stat = 'identity',
           fill = 'steelblue') +
  geom_text(aes(label = as.integer(transactions)),
            vjust = 2) +
  coord_cartesian(ylim = c(0, y_limit)) + 
  geom_segment(aes(x = 'Q1', y = max(my_data$transactions), 
                   xend = 'Q1', yend = y_limit)) + 
  geom_segment(aes(x = 'Q2', y = y_limit, 
                   xend = 'Q2', yend = min(my_data$transactions)), 
               arrow = arrow()) + 
  geom_segment(aes(x = 'Q1', y = y_limit, 
                   xend = 'Q2', yend = y_limit)) +
  geom_label(aes(x = 'Q2', 
                 y = y_limit, 
                 label = as.integer(min(my_data$transactions)- max(my_data$transactions))),
             size = 10,
             position = position_nudge(-0.5),
             fontface = "bold", fill = "lightgrey")

【讨论】:

  • 谢谢,但我试图把分组的东西拿走,因为在我的情况下不需要它,因为我只有两个酒吧。基本上时间是 x,事务是 y。你能指出我如何使箭头更接近我的“理想”结果吗?我很不确定如何绘制几块并将它们连接起来。
  • 当然你也可以破解它,但是代码不会很漂亮
  • 添加一条线(或者更确切地说是箭头)非常简单,特别是如果您使用geom_segment,您基本上必须告诉该段从哪里开始以及从哪里开始。如果您需要经常这样做,您可能需要定义新的函数数据框,但是如果这是一次性的事情,您可以硬编码该段所需的美学,事实上我将再次使用 plot 来编辑我的代码编码段
  • 这很酷,我想我明白了。 arrow_df 长什么样子?
  • 对不起,这是我试错后留下的,不需要它就删除它。我也从答案中得到了它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 2016-11-14
相关资源
最近更新 更多