Reorder columns in boxplots ggplot2

北城以北 提交于 2021-02-07 10:30:59

问题


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)

boxplot


回答1:


Your one question is actually two questions:

  1. Changing the Order of the facets. ggplot2 first changes your data in dados$mecanismo to 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 define dados$mecanismo as a factor in your data frame, then also define the levels in the specific order.

  2. Assigning a specific color in a legend to a factor. In your scale_fill_manual command, you are assigning values=. By default, the order of the values are applied according to the order of the levels of the factor (which is in this case dados$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 a list(.. or a named vector c('name'='color', 'name2'='color2',.. to values= in place of a vector of colors. Incidentally... you can change the order in which ymiss and yobs appears 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!