本文介绍了更改ggplot2 barplot中闪避条的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个数据框 df.all ,我用下面的代码用ggplot2绘制条形图。我想这样做是为了让躲闪的酒吧的顺序翻转。也就是说,标签为Singular的酒吧出现在标有Plural的酒吧之前。 ggplot(df.all, aes(gram,V1,fill = number))+ geom_bar(stat =identity,position =dodge)+ scale_x_discrete(labels = c(Grammatical,Ungrammatical)) + scale_y_continuous(formatter =percent,limits = c(0,1))+ facet_grid(。〜experiment)+ scale_fill_hue(Attractor,breaks = c(S ,P),labels = c(Singular,Plural)) I我试过做水平(df.all $ number)= c(S,P),认为ggplot可以使用水平的顺序来决定绘图顺序,但那并不奏效。我不知道还有什么可以尝试的。任何想法? df.all 的内容,以防万一它有用: > df.all 数字克实验V1 1 SG BERIMBAU_AGR_A 0.8133333 2 SG BERIMBAU_AGR_B 0.8658537 3 SU BERIMBAU_AGR_A 0.5436242 4 SU BERIMBAU_AGR_B 0.4597701 5 PG BERIMBAU_AGR_A 0.8580645 6 PG BERIMBAU_AGR_B 0.8536585 7 PU BERIMBAU_AGR_A 0.3087248 8 PU BERIMBAU_AGR_B 0.3975904 > str(df.all)'data.frame':8 obs。 4个变量: $ number:因子w / 2级别S,P:2 2 2 1 1 1 1 ..- attr(*,scores)= num [ 1:2(1d)] 0 -1 .. ..- attr(*,dimnames)= 1 .. .. $ $:chrPS $ gram:因子w / 2等级G,U:1 1 2 2 1 1 2 2 $实验:因子w / 4等级BERIMBAU_AGR_A,..:1 4 1 4 1 4 1 4 $ V1:num 0.813 0.866 0.544 0.46 0.858 ... 解决方案哈德利提供了一个解决方案。这里是问题和解决方案的复制。 目标是让标有S的条在标有P的条之前。这种情况在默认情况下不会发生,因为R订单按字母顺序排列。 df alt text http://pealco.net/code/ggplot_dodge/wrongorder.png Hadley在另一个答案中评论道,你需要基于x变量重新排序,而不是y变量。虽然我不确定这是为什么会起作用。 要翻转此示例中的因子顺序,可以将该因子转换为数字并乘以-1。 / p> df 再次绘图显示他的作品。 alt text http://pealco.net/code/ggplot_dodge/rightorder.png (df,df [order(gram,-as.numeric(number)),我还是希望有更多解释为什么 df 有效。 I have a dataframe df.all and I'm plotting it in a bar plot with ggplot2 using the code below. I'd like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "Singular" come before the bars labeled "Plural".ggplot(df.all, aes(gram, V1, fill=number)) + geom_bar(stat="identity", position="dodge") + scale_x_discrete(labels=c("Grammatical","Ungrammatical")) + scale_y_continuous(formatter="percent", limits=c(0,1)) + facet_grid(. ~ experiment) + scale_fill_hue("Attractor", breaks=c("S","P"), labels=c("Singular","Plural"))I've tried doing levels(df.all$number) = c("S", "P") thinking that maybe ggplot uses the order of the levels to decide plotting order, but that didn't work. I'm not sure what else to try. Any ideas?The contents of df.all, in case it's useful:> df.all number gram experiment V11 S G BERIMBAU_AGR_A 0.81333332 S G BERIMBAU_AGR_B 0.86585373 S U BERIMBAU_AGR_A 0.54362424 S U BERIMBAU_AGR_B 0.45977015 P G BERIMBAU_AGR_A 0.85806456 P G BERIMBAU_AGR_B 0.85365857 P U BERIMBAU_AGR_A 0.30872488 P U BERIMBAU_AGR_B 0.3975904> str(df.all)'data.frame': 8 obs. of 4 variables: $ number : Factor w/ 2 levels "S","P": 2 2 2 2 1 1 1 1 ..- attr(*, "scores")= num [1:2(1d)] 0 -1 .. ..- attr(*, "dimnames")=List of 1 .. .. ..$ : chr "P" "S" $ gram : Factor w/ 2 levels "G","U": 1 1 2 2 1 1 2 2 $ experiment: Factor w/ 4 levels "BERIMBAU_AGR_A",..: 1 4 1 4 1 4 1 4 $ V1 : num 0.813 0.866 0.544 0.46 0.858 ... 解决方案 Hadley has provided a solution. Here's a replication of the problem and the solution.The goal is to get the bars labeled "S" to come before the bars labeled "P". This doesn't happen by default because R orders levels alphabetically.df <- read.csv("http://pealco.net/code/ggplot_dodge/df.txt")ggplot(df, aes(gram, V1, fill=number)) + geom_bar(stat="identity", position="dodge")alt text http://pealco.net/code/ggplot_dodge/wrongorder.pngAs Hadley commented in another answer, "you need to reorder based on the x variables, not the y variable". Though I'm not sure why this works.To flip the order of the factors in this example, you can convert the factor to numeric and multiply by -1.df <- with(df, df[order(gram, -as.numeric(number)), ])Plotting again shows that his works.alt text http://pealco.net/code/ggplot_dodge/rightorder.pngI'd still like so more explanation about why df <- with(df, df[order(gram, -as.numeric(number)), ]) works. 这篇关于更改ggplot2 barplot中闪避条的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 21:04