【发布时间】:2019-05-02 09:34:51
【问题描述】:
我正在尝试使用 ggplot 在 R 中创建一个绘图,以一种很好的方式显示我的两个条形之间的差异。
我找到了一个示例,它完成了我想要的部分功能,但我有两个主要问题:
- 它基于比较几组条形图,但我只有两个,所以我添加了一个包含它们的组。
- 我想把箭头画成更好的形状。我附上了一张图片。
代码:
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
我瞄准的箭头:
【问题讨论】:
-
这实际上是我使用的示例,现在我不确定它是否是正确的方向。