[英]Stacked bar plot in R with the positive and negative values
I would like to plot a stacked bar plot in R and my data looks as such:我想在 R 中绘制一个堆积条形图,我的数据如下所示:
This table is the values against date and as it can be seen, there are repetitive dates with different sides.该表是针对日期的值,可以看出,存在不同边的重复日期。 I would like to plot a bar plot using this data.
我想使用此数据绘制条形图。
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)
Want a stacked plot where I can see the values against dates.想要一个堆叠图,我可以在其中查看日期的值。 How do change my code?
如何更改我的代码?
You can easily create this figure with ggplot2
.您可以使用
ggplot2
轻松创建此图。 Here a piece of code for you using a data frame similar to what you have:这里有一段代码供您使用类似于您所拥有的数据框:
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()
This is the output:这是输出:
Obviously, the official documentation is a good way to start to understand a bit better how to improve it.显然,官方文档是开始更好地了解如何改进它的好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.