The order of plotting in a grunge diagram in ggplot2

I am trying to create a facet diagram in ggplot2, but I cannot get the faceted border to appear in the order I want. Code for plotting the chart:

g <- ggplot(df2, aes(x=Y, y=label)) + geom_point() g <- g + facet_grid(incentive ~ ., scale="free") g <- g + geom_errorbarh(aes(xmax = Y + se, xmin = Y - se)) g <- g + geom_vline(xintercept=1/6, linetype=2, colour="red") g <- g + opts(title="% Subjects Choosing Non-Focal Image", strip.text.y = theme_text() ) + xlab("%") + ylab("Groups") print(g) 

The problem with the plot is that within the β€œMoney Only” category, 1 cent and 5 cents are in the wrong order. The problem, apparently, is not in the order of the factors themselves, but:

 > levels(df2$label) [1] "0" "1" "1 cent" "5 cent" "6" > 

alt text

Update : ordering the coefficient does not change the display order, i.e. builds with label 3, where:

df2 $ tag3 1 0 1 1 cent 5 cents 6
Levels: 0 <1 <1 cent. 5 cents, 6

 >str(df2$label3) Ord.factor w/ 5 levels "0"<"1"<"1 cent"<..: 1 2 3 4 5 

Actual data frame:

 df2 <- structure(list(Y = c(0.0869565217391304, 0.148148148148148, 0.172413793103448, 0.384615384615385, 0.5625), group = c(0L, 1L, 5L, 3L, 6L), se = c(0.0856368459098186, 0.079039229753282, 0.0762650540661762, 0.0805448741815074, 0.0726021684593052 ), nudged = c(FALSE, TRUE, TRUE, TRUE, TRUE), incentive = structure(c(1L, 2L, 3L, 3L, 4L), .Label = c("Default behavior", "Imbalance only", "Money only", "Money & Imbalance together"), class = "factor"), label = structure(1:5, .Label = c("0", "1", "1 cent", "5 cent", "6"), class = "factor"), plot_order = c(0, 1, 2, 3, 4)), .Names = c("Y", "group", "se", "nudged", "incentive", "label", "plot_order"), row.names = c("as.factor(group)0", "as.factor(group)1", "as.factor(group)5", "as.factor(group)3", "as.factor(group)6"), class = "data.frame") 
+6
r ggplot2
source share
2 answers

Your problem is that facet_grid displays the types of incentives ("Default Behavior", etc.) in an order that contradicts the order of the incentive value (0, 1 cent, 5 cents, etc.), so you do not receive the order you want in the Money Only group. The easiest way to fix this is to reorder your incentive coefficient so that the default is built at the bottom rather than the top:

 df2$incentive <- ordered( df2$incentive, levels = rev(c("Default behavior", "Imbalance only", "Money only", "Money & Imbalance together"))) 

and leave the rest of the code as is. Then you get this plot: alt text

+9
source share

It sounds and looks very strange (since the labels are sorted in order of factor levels, as @Dirk Eddelbuettel also wrote), but you can always use scale_discrete .

I made a small example based on your code, but it looks a little strange, see:

 g <- ggplot(df2, aes(x=Y, y=label)) + geom_point() # add manual scale g <- g+ scale_y_discrete(limits=c("0","1","1 cent","5 cent","6")) g <- g + facet_grid(incentive ~ ., scale="free") g <- g + geom_errorbarh(aes(xmax = Y + se, xmin = Y - se)) g <- g + geom_vline(xintercept=1/6, linetype=2, colour="red") g <- g + opts(title="% Subjects Choosing Non-Focal Image", strip.text.y = theme_text() ) + xlab("%") + ylab("Groups") 

ggplot example plot

0
source share

All Articles