【问题标题】:Outlines bars in bar graph ggplot条形图ggplot中的轮廓条
【发布时间】:2021-09-13 19:27:55
【问题描述】:

我想知道是否有办法在我的条形图中勾勒出条形。我想勾勒出一组酒吧的左侧、顶部和右侧,然后勾勒出一组聚集在一起的酒吧。我对此进行了一些尝试,但运气不佳。我不完全确定这是可能的。我将为我的数据、图表和一些我希望图表看起来像的图像附上一些代码

library(ggplot2)
ggplot(df, aes(bar,num, fill = group)) +
  geom_bar(stat = 'identity', width = 5) +
  scale_fill_manual(values = alpha(c("blue"), .3))

这是数据

df <- structure(list(bar = c(17.5, 27.5, 32.5, 37.5, 42.5, 47.5, 52.5, 
57.5, 62.5, 67.5, 72.5, 77.5, 82.5), num = c(1L, 7L, 8L, 11L, 
22L, 46L, 43L, 60L, 59L, 40L, 17L, 11L, 1L), group = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "A", class = "factor")), class = "data.frame", row.names = c(NA, 
-13L))

这就是图表当前的样子

这就是我希望做的

【问题讨论】:

  • 我认为你必须在 geom_bar 中使用 col = red
  • @mhovd 在整个栏周围画了一个轮廓

标签: r ggplot2


【解决方案1】:

您可以通过添加零长度条来破解 geom_step 来执行此操作。

zeroes <- setdiff(seq(min(df$bar), max(df$bar) + 5, by = 5), df$bar)
df2 <- rbind(df, data.frame(bar = min(df$bar), num = 0, group = "A"))
df2 <- rbind(df2, data.frame(bar = zeroes, num = 0, group = "A"))
df2 <- df2 %>% arrange(bar, num)

library(ggplot2)
ggplot(df2, aes(bar,num, fill = group)) +
  geom_bar(stat = 'identity', , width = 5) +
  geom_step(col = 'red', aes(x = bar - 2.5)) +
  scale_fill_manual(values = alpha(c("blue"), .3))

【讨论】:

  • 完美!!我希望没有那个空的红线,但你只能问这么多。我可以轻松地编辑它。谢谢!!
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
相关资源
最近更新 更多