![](/img/trans.png)
[英]geom_bar ggplot2 stacked, grouped bar plot with positive and negative values - pyramid plot
[英]Stacked bar plot in R with the positive and negative values
我想在 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)
想要一個堆疊圖,我可以在其中查看日期的值。 如何更改我的代碼?
您可以使用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()
這是輸出:
顯然,官方文檔是開始更好地了解如何改進它的好方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.