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)) +
However, the variables are listed in reverse alphabetical order, but not vartype : the aesthetics order=vartype ignored.

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).

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.