问题
I have this boxplot and I want to reorder the columns. Instead of MAR, MCAR, MNAR, I want MCAR, MAR, MNAR. And the legend is wrong too, orange is ymiss and blue is yobs but I can't change it. Can you help me? thanks!
library(ggplot2)
logistic <- function(x) exp(x) / (1 + exp(x))
set.seed(80122)
n <- 300
dt <- MASS::mvrnorm(n = n, mu = c(0, 0),
Sigma = matrix(c(1, 0.5, 0.5, 1), nrow = 2));dt
r2.mcar <- 1 - rbinom(n, 1, 0.5);r2.mcar
r2.mar <- 1 - rbinom(n, 1, logistic(dt[, 1]));r2.mar
r2.mnar <- 1 - rbinom(n, 1, logistic(dt[, 2]));r2.mnar
yobs1<-dt
yobs1[r2.mcar==0,2]<-NA;yobs1
yobs2<-dt
yobs2[r2.mar==0,2]<-NA;yobs2
yobs3<-dt
yobs3[r2.mnar==0,2]<-NA;yobs3
dados<-matrix(cbind(yobs1[,1],yobs2[,1],yobs3[,1],yobs1[,2],yobs2[,2],yobs3[,2]),ncol=1)
v<-c(rep("yobs",900),rep("ymiss",900))
m<-c(rep("MCAR",300),rep("MAR",300),rep("MNAR",300))
dados<-data.frame(v,m,dados)
names(dados)<-c("y", "mecanismo","valores")
p <- ggplot(dados, aes(x =valores, y = mecanismo)) +
geom_boxplot(aes(fill = y), position = position_dodge(0.9)) +
scale_fill_manual(values = c("lightskyblue","lightsalmon"))
p + facet_grid(~mecanismo)

回答1:
Your one question is actually two questions:
Changing the Order of the facets.
ggplot2first changes your data indados$mecanismoto a factor. The order of the facets are related to the order of the levels of that factor, which by default will often be the order in which they appear in your dataset. To change the order, you need to definedados$mecanismoas a factor in your data frame, then also define the levels in the specific order.Assigning a specific color in a legend to a factor. In your
scale_fill_manualcommand, you are assigningvalues=. By default, the order of the values are applied according to the order of the levels of the factor (which is in this casedados$y). You can just re-specify the levels of that factor, but to assign colors specifically, you don't need to. All you need to do is supply alist(..or a named vectorc('name'='color', 'name2'='color2',..tovalues=in place of a vector of colors. Incidentally... you can change the order in whichymissandyobsappears in the legend by setting the factor as indicated in #1 above...
Here's the code and plot:
# relevel factor for right order of facets
dados$mecanismo <- factor(dados$mecanismo, levels=c('MCAR','MAR','MNAR'))
p <- ggplot(dados, aes(x =valores, y = mecanismo)) +
geom_boxplot(aes(fill = y), position = position_dodge(0.9)) +
# set colors with named vector
scale_fill_manual(values = c('yobs'="lightskyblue",'ymiss'="lightsalmon"))
p + facet_grid(~mecanismo)
EDIT: Change order of legend keys
After some comments, it's clear that the OP was intending not just to recolor the data according to specific values for ymiss and yobs, but also change the positioning of the boxes in the legend to match that of the plot. By this, I mean that in the plot above, the boxplot for yobs is on top with ymiss below, yet in the legend on the right, the order is reversed. The fix for this is to specify the ordering of labels explicitly through the breaks= argument of scale_fill_manual. This addresses and fixes that issue:
dados$mecanismo <- factor(dados$mecanismo, levels=c('MCAR','MAR','MNAR'))
ggplot(dados, aes(x =valores, y = mecanismo)) +
geom_boxplot(aes(fill = y), position = position_dodge(0.9)) +
# set colors with named vector
scale_fill_manual(
values = c('yobs'="lightskyblue",'ymiss'="lightsalmon"),
breaks = c('yobs', 'ymiss')
)
回答2:
Add this code and it will work:
#Reorder var
dados$mecanismo <- factor(dados$mecanismo,levels = c("MCAR","MAR","MNAR"),ordered = T)
p <- ggplot(dados, aes(x =valores, y = mecanismo)) +
geom_boxplot(aes(fill = y), position = position_dodge(0.9)) +
scale_fill_manual(values = c("lightsalmon","lightskyblue"))
p + facet_grid(~mecanismo)
来源:https://stackoverflow.com/questions/62776858/reorder-columns-in-boxplots-ggplot2