【问题标题】:Stacked bar plot in R with the positive and negative valuesR中带有正值和负值的堆积条形图
【发布时间】:2019-01-09 07:04:07
【问题描述】:

我想在 R 中绘制一个堆积条形图,我的数据如下所示:

此表是针对日期的值,可以看出,有不同边的重复日期。我想使用这些数据绘制条形图。

combined = rbind(x,y)
combined = combined[order(combined$Group.1),]
barplot(combined$x,main=paste("x vs y Breakdown",Sys.time()),names.arg = combined$Group.1,horiz = TRUE,las=2,xlim=c(-30,30),col = 'blue',beside = True)

想要一个堆积图,我可以在其中查看日期值。如何更改我的代码?

【问题讨论】:

  • 请编辑您的问题以使用dput 而非图像添加数据。附上的图也是当前的还是预期的输出?
  • 提供示例数据。当日期重复时,您希望如何绘制它们?
  • 这可能会有所帮助:stackoverflow.com/questions/35324892/…
  • 不能叠加正负值。

标签: r


【解决方案1】:

您可以使用ggplot2 轻松创建此图。 这里有一段代码为您使用类似于您所拥有的数据框:

library(ggplot2)

my_data <- data.frame(
  date = factor(c(1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8)),
  x = c(-2, 14, -8, -13, 3, -4, 9, 8, 3, -4, 8, -1)
)

g <- ggplot(my_data, aes(x = date, y = x)) +
  geom_bar(
    stat = "identity", position = position_stack(),
    color = "white", fill = "lightblue"
  ) +
  coord_flip()

这是输出:

显然,the official documentation 是开始更好地了解如何改进它的好方法。

【讨论】:

    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2021-07-18
    • 2013-08-13
    相关资源
    最近更新 更多