【问题标题】:bar chart starting over line chart从折线图开始的条形图
【发布时间】:2022-01-14 11:28:27
【问题描述】:

我想重叠一条线和一个条形图。棘手的部分是条形不应从 0 开始,而应从行开始。到目前为止我试过了

valuation <- c(100, 105, 120)
deposit <- c(0, 10, -15)
date <- as.Date(c('2010-11-1','2010-11-2','2010-11-3'))
df <- data.frame( valuation, deposit, date)ggplot(df) + 
        geom_line(aes(x=date, y=valuation, group=1)) + 
        geom_col(aes(x=date, y=deposit, fill=sign)) + 
        scale_fill_manual(values = c("positive"="green","negative"="red"))

这会产生以下输出:https://i.ibb.co/pyT3bk2/1.png(抱歉,我需要 10 个声望才能发布图片)

我需要正值从直线的同一点开始向上,负值也从直线开始但向下:https://i.ibb.co/gZBsXnp/2.png

你知道这是否可能吗?非常感谢!!

【问题讨论】:

  • 什么是 group=1?

标签: r ggplot2


【解决方案1】:

我建议在这里使用geom_rect 而不是geom_col

ggplot(df) + 
  geom_rect(aes(xmin=date-.4, xmax=date+.4, ymin=valuation, ymax=valuation+deposit, 
                fill=ifelse(deposit>0, "positive", "negative"))) + 
  geom_line(aes(x=date, y=valuation)) + 
  ylim(0, 120) +
  scale_fill_manual(values = c("positive"="green","negative"="red"), name = "sign")

创建以下图表:

【讨论】:

  • yesssssssssssssss 完美运行。非常感谢维策!!!
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 2013-06-29
相关资源
最近更新 更多