简体   繁体   English

ggplot,堆积的条形图,x值的顺序混合

[英]ggplot, stacked bar chart, order of x values get mixed up

I've a data frame (mmt.ranking.sum_2) with a list of 25 questions (1st column 'questions'). 我有一个数据框(mmt.ranking.sum_2),其中包含25个问题(第一列“问题”)的列表。

The question-strings are consecutively preceded by 9a-h, 10a-j, 11a-g; 问题字符串之前依次是9a-h​​,10a-j,11a-g; ie numbered 即编号

For each question there are counts of answers in classes from r0-r5 (2.-7. column) 对于每个问题,在r0-r5(2.-7。列)的类中都有大量答案。

questions              r0 r1 r2 r3 r4 r5
9a 'question text'     1 1 0 8 3 8
9b 'question text'     1 0 2 7 7 4
...
9h 'question text'     1 6 4 7 3 0
10a 'question text'    ...
...
10j 'question text'    ...
...

11g 'question text'    ...

This is melted & values are drawn into a stacked bar chart 将其融化并将值绘制到堆积的条形图中

df.melt<-melt(mmt.ranking.sum_2[,1:7], id.vars="questions") df.melt <-melt(mmt.ranking.sum_2 [,1:7],id.vars =“ questions”)

ggplot(df.melt, aes(questions, value, fill=variable)) + geom_bar()+ coord_flip() + theme_bw()+ scale_fill_brewer() ggplot(df.melt,aes(问题,值,填充=变量))+ geom_bar()+ coord_flip()+ theme_bw()+ scale_fill_brewer()

In the original data frame (see above) & in the melted one 在原始数据框中(请参见上文)和已融化的数据框中

questions variable value 问题变量值

9a'question text' r0 1 9a'问题文本'r0 1

9b'question text' r0 1 9b'问题文本'r0 1

... ...

9a'question text' r1 2 9a'问题文本'r1 2

... ...

11g'question text' r5 2 11g“问题文字” r5 2

the order of the questions is correct: 9a-h, 10a-j, 11a-g 问题的顺序是正确的:9a-h,10a-j,11a-g

But the order changes & reverses strangely in the final chart ('coord_flip' causes horizontal bars). 但是顺序在最终图表中发生了奇怪的变化和反转(“ coord_flip”会引起水平条形)。

top > down: 9h-9a, 11g- 11a, 10j-10a 顶部>向下:9h-9a,11g-11a,10j-10a

Any ideas why and how I can keep the original order? 有什么想法为什么以及如何保持原始订单?

Any help appreciated 任何帮助表示赞赏

Thanks, Georg 谢谢,乔治

You should reorder your x axis using reorder , Try this for example , Replace 您应该使用reorder对x轴reorder ,例如,尝试使用Replace

aes(questions,..)

by 通过

aes(reorder(questions,as.numeric(gsub('([0-9]+).*','\\1', 
             df.melt$questions)),...)

You don't give a reproducible example So I can't be sure from the given solution. 没有给出可复制的示例,因此无法从给定的解决方案中确定。 Here some code to generate your questionnaire data ( I spent 30 minutes to generate your data and less than one minute to find a solution, so please try to reproduce data next time). 这里有一些代码可以生成您的问卷数据(我花了30分钟来生成您的数据,而花了不到一分钟的时间来找到解决方案,因此请下次尝试重现数据)。

## 6 questions
N <- 6  
set.seed(123)
let <- sample(1:5,N,rep=TRUE)
qs <- lapply(seq(N),
       function(x){
         nn <- paste0(x,letters[1:20][seq(let[x])])
         somtext <- replicate(3,paste(sample(c(0:9, letters, LETTERS),
                      5, replace=TRUE),
               collapse=""))
         paste(nn,'question text',paste0(somtext,collapse=' '))

       })

questions <- unlist(qs)

dat <- matrix(sample(0:8,length(questions)*6,rep=TRUE),ncol=6)

colnames(dat) <- paste0('r',0:5)
dat <- data.frame(questions,dat)

library(reshape2)
df.melt <- melt(dat, id.vars="questions")

Then when I plot it using : 然后当我使用以下方式绘制它时:

ggplot(df.melt, aes(reorder(questions,as.numeric(gsub('([0-9]+).*','\\1', df.melt$questions)))
                    ,value, fill=variable)) +
  geom_bar(stat='identity')+ 
  theme_bw()+ 
  coord_flip() +
  scale_fill_brewer()

` ` 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM