gpt4 book ai didi

r - R中的100%堆积条形图

转载 作者:行者123 更新时间:2023-12-04 10:52:37 25 4
gpt4 key购买 nike

早上好,

我正在尝试在 R 中的 100% 堆积条形图中获取这些数据

        Federal Non Federal
2006 46753094 74740716
2007 43397314 74834857
2008 43962330 71051132
2009 42238038 72987898
2010 49546221 75232382
2011 48730233 76333479
2012 49316564 74669993
2013 48198329 75644892
2014 46630540 74783207
2015 46214781 75004771
2016 47625256 73744148

使它看起来像这样:
enter image description here

我将是第一个承认它当然不喜欢令人兴奋的 map 但仍然需要它的人。

我已尝试按照 here 的说明执行代码但它没有用。

这就是我所做的:
>     g <- ggplot(FedNonFed, aes(FedNonFed))
> g + geom_bar(aes(fill = FedNonFed), position = "fill")

不是我需要的图表。
g <- ggplot(FedNonFed, aes(FY))
g + geom_bar(aes(fill = FedNonFed), position = "fill")
g + geom_bar(aes(fill = TotalExpense), position = "fill")

任何帮助,将不胜感激。

最佳答案

你需要融化你的数据。我稍微更改了您的数据以使其更易于加载,但这应该很容易替换。

library(reshape2)
library(scales)
df = data.frame("Year" = seq(2006,2016,by = 1), "Federal" = seq(1,11,by = 1), "Non Federal" = seq(11,1,by = -1))
dfm = melt(df, id.vars = "Year")
ggplot(dfm,aes(x = Year, y = value,fill = variable)) +
geom_bar(position = "fill",stat = "identity") +
scale_y_continuous(labels = percent_format())

我对情节进行了一些更改,使其更接近 excel 情节。唯一不同的是颜色。
ggplot(dfm,aes(x = Year, y = value,fill = variable)) + 
geom_bar(position = "fill",stat = "identity") +
scale_y_continuous(labels = percent_format())+ scale_x_continuous(breaks = 2006:2016,labels= as.character(seq(2006,2016,by = 1)))+
theme(plot.subtitle = element_text(vjust = 1),
plot.caption = element_text(vjust = 1),
legend.title = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position = "bottom", legend.direction = "horizontal")

关于r - R中的100%堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42277055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com