gpt4 book ai didi

r - ggplot : Order bars in faceted bar chart per facet

转载 作者:行者123 更新时间:2023-12-03 21:29:19 27 4
gpt4 key购买 nike

我在 R 中有一个数据框,我想在分面 ggplot 条形图中绘制它。
我在 ggplot 中使用此代码:

ggplot(data_long, aes(x = partei, y = wert, fill = kat, width=0.75)) + 
labs(y = "Wähleranteil [ % ]", x = NULL, fill = NULL) +
geom_bar(stat = "identity") +
facet_wrap(~kat) +
coord_flip() +
guides(fill=FALSE) +
theme_bw() + theme( strip.background = element_blank(),
panel.grid.major = element_line(colour = "grey80"),
panel.border = element_blank(),
axis.ticks = element_line(size = 0),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank() ) +
theme(legend.position="bottom") +
scale_fill_brewer(palette="Set2")
这产生了这个图表:
enter image description here
您可以看到只有最后一个方面按所需的降序排列。我希望所有方面都按降序排列,这意味着标签顺序会发生变化。因此,我还需要所有方面都有自己的 y 轴标签。
这是我正在使用的数据:
data_long = data.frame(
partei = c("SP", "Grüne", "AL", "BDP", "glp",
"CVP", "EVP", "FDP", "SVP", "EDU", "SP", "Grüne", "AL", "BDP",
"glp", "CVP", "EVP", "FDP", "SVP", "EDU", "SP", "Grüne", "AL",
"BDP", "glp", "CVP", "EVP", "FDP", "SVP", "EDU", "SP", "Grüne",
"AL", "BDP", "glp", "CVP", "EVP", "FDP", "SVP", "EDU", "SP",
"Grüne", "AL", "BDP", "glp", "CVP", "EVP", "FDP", "SVP", "EDU",
"SP", "Grüne", "AL", "BDP", "glp", "CVP", "EVP", "FDP", "SVP",
"EDU", "SP", "Grüne", "AL", "BDP", "glp", "CVP", "EVP", "FDP",
"SVP", "EDU"),
kat = c("kand1", "kand1", "kand1", "kand1", "kand1",
"kand1", "kand1", "kand1", "kand1", "kand1", "kand2", "kand2",
"kand2", "kand2", "kand2", "kand2", "kand2", "kand2", "kand2",
"kand2", "kand3", "kand3", "kand3", "kand3", "kand3", "kand3",
"kand3", "kand3", "kand3", "kand3", "kand4", "kand4", "kand4",
"kand4", "kand4", "kand4", "kand4", "kand4", "kand4", "kand4",
"kand5", "kand5", "kand5", "kand5", "kand5", "kand5", "kand5",
"kand5", "kand5", "kand5", "kand6", "kand6", "kand6", "kand6",
"kand6", "kand6", "kand6", "kand6", "kand6", "kand6", "kand7",
"kand7", "kand7", "kand7", "kand7", "kand7", "kand7", "kand7",
"kand7", "kand7"),
wert = c(95.41, 80.6, 75.77, 54.02, 47.91,
39.01, 36.2, 32.01, 5.71, 1.1, 18.05, 7.15, 9.02, 62.3, 39.18,
42.41, 23.14, 94.66, 29.93, 34.97, 0.51, 0.27, 3.92, 9.21, 2.53,
2.7, 3.52, 23.19, 92.49, 60.64, 52.98, 81.28, 56.42, 7.52, 13.65,
4.06, 9.96, 1.46, 0.94, 0, 7.51, 9.19, 9.94, 25.3, 69.58, 10.59,
9.23, 17.61, 3.6, 3.43, 4.29, 2.37, 7.73, 13.14, 11.67, 75.43,
19.34, 6.52, 2.43, 6.4, 1.87, 2.98, 5.87, 6.7, 1.29, 2.73, 80.91,
1.1, 1.58, 45.47)
)

最佳答案

因为有时更容易查看所有代码的运行情况,所以这里有一个解决方案,可以在一次 lapply 调用中生成所有绘图。还有一些其他问题需要解决(订购,正确选择颜色),我喜欢拼图。

#create list of plots
myplots <- lapply(split(dat,dat$kat), function(x){
#relevel factor partei by wert inside this subset
x$partei <- factor(x$partei, levels=x$partei[order(x$wert,decreasing=F)])

#make the plot
p <- ggplot(x, aes(x = partei, y = wert, fill = kat, width=0.75)) +
geom_bar(stat = "identity") +
scale_fill_discrete(drop=F)+ #to force all levels to be considered, and thus different colors
theme_bw()+
theme(legend.position="none")+
labs(y="Wähleranteil (%)", x="", title=unique(x$kat))+
coord_flip()
})

library(gridExtra)

do.call(grid.arrange,(c(myplots, ncol=3)))

enter image description here

关于r - ggplot : Order bars in faceted bar chart per facet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34001024/

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