r - Order in ggplot2 grouped barchart -


this question has answer here:

this code:

col1<-c(rep("first",2), rep("second",2), rep("third",2), rep("fourth",2), rep("fifth",2), rep("sixth",2), rep("seventh",2), rep("eighth",2)) col2<-gl(2,1,8, labels=c("one","two")) col3<-values d1 <- data.frame(column1=col1, column2=col2, column3=col3) ggplot(d, aes(x=column1, y=column3, fill=column2)) + geom_bar(position=position_dodge()) 

the bars on plot in alphabetical order, need them in order in col1. how can this?

substitute:

d1 <- data.frame(column1=col1, column2=col2, column3=col3) 

for:

d1 <- data.frame(column1=factor(col1, levels=unique(col1)), column2=col2, column3=col3) 

the factor(col1, levels=unique(col1)) akes factor of col1 , levels= allows pick order of levels subsequently used ggplot define order of bars.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -