The order of legend entries in ggplot2 barplots with coordinate ()

I am trying to get the correct order of variables in a graph, which I did with ggplot2 in R.

Suppose I have a dataframe, for example:

set.seed(1234) my_df<- data.frame(matrix(0,8,4)) names(my_df) <- c("year", "variable", "value", "vartype") my_df$year <- rep(2006:2007) my_df$variable <- c(rep("VX",2),rep("VB",2),rep("VZ",2),rep("VD",2)) my_df$value <- runif(8, 5,10) my_df$vartype<- c(rep("TA",4), rep("TB",4)) 

which gives the following table:

  year variable value vartype 1 2006 VX 5.568517 TA 2 2007 VX 8.111497 TA 3 2006 VB 8.046374 TA 4 2007 VB 8.116897 TA 5 2006 VZ 9.304577 TB 6 2007 VZ 8.201553 TB 7 2006 VD 5.047479 TB 8 2007 VD 6.162753 TB 

There are four variables (VX, VB, VZ, and VD) that belong to two groups of variable types (TA and TB).

I would like to build the values ​​in the form of horizontal stripes on the y axis, vertically ordered first by groups of variables, and then by variable names , faces by year, with values ​​along the x axis and the fill color of the corresponding variable group. (i.e., in this simplified example, the order should be from top to bottom, VB, VX, VD, VZ)

1) My first attempt was to try the following:

 ggplot(my_df, aes(x=variable, y=value, fill=vartype, order=vartype)) + # adding or removing the aesthetic "order=vartype" doesn't change anything geom_bar() + facet_grid(. ~ year) + coord_flip() 

However, the variables are listed in reverse alphabetical order, but not vartype : the aesthetics order=vartype ignored.

enter image description here

2) After answering a similar question that I posted yesterday, I tried the following, based on the message Order bars in the ggplot2 histogram :

 my_df$variable <- factor( my_df$variable, levels=rev(sort(unique(my_df$variable))), ordered=TRUE ) 

This approach does get the variables in vertical alphabetical order on the graph, but ignores the fact that the variables must be ordered first by goups variables (with TA variables on top and TB variables below).

enter image description here

3) The following gives the same as 2 (above):

 my_df$vartype <- factor( my_df$vartype, levels=sort(unique(my_df$vartype)), ordered=TRUE ) 

... which has the same problems as the first approach (variables listed in reverse alphabetical order, groups are ignored)

4) another approach, based on the original answer to Order Bars in the ggplot2 histogram , also gives the same plan as 2, above

 my_df <- within(my_df, vartype <- factor(vartype, levels=names(sort(table(vartype), decreasing=TRUE))) ) 

I am puzzled by the fact that, despite several approaches, the aesthetic order=vartype ignored. However, this seems to work in an unrelated issue: http://learnr.wordpress.com/2010/03/23/ggplot2-changing-the-default-order-of-legend-labels-and-stacking-of -data /

I hope the problem is clear and welcome any suggestions.

Matteo

Yesterday I posted a similar question, but unfortunately I made a few mistakes when describing the problem and providing a reproducible example. I have listened to a few sentences since then, and carefully searched for stakoverflow for a similar question and applied, as far as I know, every proposed combination of solutions to no avail. I ask the question again, hoping that I can solve my problem and hopefully be useful to others.

+10
r order ggplot2
Sep 04 '11 at 13:15
source share
1 answer

This has little to do with ggplot , but instead the question arises of creating an ordering of variables that you need to use to reorder the levels of the factor. Here is your data implemented using various functions for the best effect:

 set.seed(1234) df2 <- data.frame(year = rep(2006:2007), variable = rep(c("VX","VB","VZ","VD"), each = 2), value = runif(8, 5,10), vartype = rep(c("TA","TB"), each = 4)) 

Note that in this way variable and vartype are factors. If they are not factors, ggplot() will force them, and then you will leave them in alphabetical order. I have said this before and will no doubt repeat it; Before you start printing / performing data analysis, first enter your data in the correct format first .

You need the following order:

 > with(df2, order(vartype, variable)) [1] 3 4 1 2 7 8 5 6 

where you should notice that we first get the ordering on vartype , and then on variable within the vartype levels. If we use this to change the order of variable , we get:

 > with(df2, reorder(variable, order(vartype, variable))) [1] VX VX VB VB VZ VZ VD VD attr(,"scores") VB VD VX VZ 1.5 5.5 3.5 7.5 Levels: VB VX VD VZ 

(ignore the attr(,"scores") bit attr(,"scores") and focus on the Levels). This has the correct order, but ggplot() will draw them from bottom to top, and you would like to top to bottom. I am not familiar with ggplot() to know if this can be controlled, so we need to also cancel the order using decreasing = TRUE in the call to order() .

Assuming this all together, we have:

 ## reorder `variable` on `variable` within `vartype` df3 <- transform(df2, variable = reorder(variable, order(vartype, variable, decreasing = TRUE))) 

What when used with your build code:

 ggplot(df3, aes(x=variable, y=value, fill=vartype)) + geom_bar() + facet_grid(. ~ year) + coord_flip() 

produces the following:

reordered barplot

+10
Sep 04 2018-11-11T00:
source share



All Articles