本文介绍了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()+ (fill = FALSE)+ theme_bw()+ theme(strip.background = element_blank(), panel.grid.major = element_line(color =grey80),面板。 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) 生成此图表: p> 您可以看到只有最后一个方面按照所需的降序排列。我希望所有方面都按降序排列,这意味着标签顺序会发生变化。因此,我还需要所有方面都有自己的y轴标签。 这是我使用的数据: partei kat wert 1 SP kand1 95.41 2Grünekand1 80.60 3 AL kand1 75.77 4 BDP kand1 54.02 5 glp kand1 47.91 6 CVP kand1 39.01 7 EVP kand1 36.20 8 FDP kand1 32.01 9 SVP kand1 5.71 10 EDU kand1 1.10 11 SP kand2 18.05 12Grünekand2 7.15 13 AL kand2 9.02 14 BDP kand2 62.30 15 glp kand2 39.18 16 CVP kand2 42.41 17 EVP kand2 23.14 18 FDP kand2 94.66 19 SVP kand2 29.93 20 EDU kand2 34.97 21 SP kand3 0.51 22Grünekand3 0.27 23 AL kand3 3.92 24 BDP kand3 9.21 25 glp kand3 2.53 26 CVP kand3 2.70 27 EVP kand3 3.52 28 FDP kand3 23.19 29 SVP kand3 92.49 30 EDU kand3 60.64 31 SP kand4 5 2.98 32Grünekand4 81.28 33 AL kand4 56.42 34 BDP kand4 7.52 35 glp kand4 13.65 36 CVP kand4 4.06 37 EVP kand4 9.96 38 FDP kand4 1.46 SVP kand4 0.94 40 EDU kand4 0.00 41 SP kand5 7.51 42Grünekand5 9.19 43 AL kand5 9.94 44 BDP kand5 25.30 45 glp kand5 69.58 46 CVP kand5 10.59 47 EVP kand5 9.23 48 FDP kand5 17.61 49 SVP kand5 3.60 50 EDU kand5 3.43 51 SP kand6 4.29 52Grünekand6 2.37 53 AL kand6 7.73 54 BDP kand6 13.14 55 glp kand6 11.67 56 CVP kand6 75.43 57 EVP kand6 19.34 58 FDP kand6 6.52 59 SVP kand6 2.43 60 EDU kand6 6.40 61 SP kand7 1.87 62Grünekand7 2.98 63 AL kand7 5.87 64 BDP kand7 6.70 65 glp kand7 1.29 66 CVP kand7 2.73 67 EVP kand7 80.91 68 FDP kand 7 1.10 69 SVP kand7 1.58 EDU kand7 45.47 解决方案因为有时看到所有的代码都很容易实现,所以这里有一个解决方案,在一次调用lapply的内部生成所有的图。还有其他一些问题需要弄清楚(订购,弄清楚颜色),我喜欢拼图。 #create list of绘制 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,decrease = F)]) #绘制图p geom_bar(stat =identity)+ scale_fill_discrete(drop = F)+#强制所有级别被考虑,颜色 theme_bw()+ 主题(legend.position =none)+ labs(y =Wähleranteil(%),x =,title = unique kat))+ coord_flip()}) library(gridExtra) do.call(grid.arrange,(c(myplots, ncol = 3))) I have a dataframe in R that I want to plot in a faceted ggplot bar chart.I use this code in 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")This produces this chart:You can see that only the last facet is in the desired descending order. I would like that all the facets are ordered in descending order, meaning that the label order changes. Therefore I also need that all facets have their own y-axis labels.This is the data I'm using: partei kat wert1 SP kand1 95.412 Grüne kand1 80.603 AL kand1 75.774 BDP kand1 54.025 glp kand1 47.916 CVP kand1 39.017 EVP kand1 36.208 FDP kand1 32.019 SVP kand1 5.7110 EDU kand1 1.1011 SP kand2 18.0512 Grüne kand2 7.1513 AL kand2 9.0214 BDP kand2 62.3015 glp kand2 39.1816 CVP kand2 42.4117 EVP kand2 23.1418 FDP kand2 94.6619 SVP kand2 29.9320 EDU kand2 34.9721 SP kand3 0.5122 Grüne kand3 0.2723 AL kand3 3.9224 BDP kand3 9.2125 glp kand3 2.5326 CVP kand3 2.7027 EVP kand3 3.5228 FDP kand3 23.1929 SVP kand3 92.4930 EDU kand3 60.6431 SP kand4 52.9832 Grüne kand4 81.2833 AL kand4 56.4234 BDP kand4 7.5235 glp kand4 13.6536 CVP kand4 4.0637 EVP kand4 9.9638 FDP kand4 1.4639 SVP kand4 0.9440 EDU kand4 0.0041 SP kand5 7.5142 Grüne kand5 9.1943 AL kand5 9.9444 BDP kand5 25.3045 glp kand5 69.5846 CVP kand5 10.5947 EVP kand5 9.2348 FDP kand5 17.6149 SVP kand5 3.6050 EDU kand5 3.4351 SP kand6 4.2952 Grüne kand6 2.3753 AL kand6 7.7354 BDP kand6 13.1455 glp kand6 11.6756 CVP kand6 75.4357 EVP kand6 19.3458 FDP kand6 6.5259 SVP kand6 2.4360 EDU kand6 6.4061 SP kand7 1.8762 Grüne kand7 2.9863 AL kand7 5.8764 BDP kand7 6.7065 glp kand7 1.2966 CVP kand7 2.7367 EVP kand7 80.9168 FDP kand7 1.1069 SVP kand7 1.5870 EDU kand7 45.47 解决方案 Because it's sometimes easier to see all code in action, here's a solution for you that generates all plots inside one call to lapply. There were some other issues to figure out (ordering, getting the colors right) and I like a puzzle.#create list of plotsmyplots <- 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))) 这篇关于ggplot:在每个面的分面条形图中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 03:12