简体   繁体   English

重新排序箱线图中的列 ggplot2

[英]Reorder columns in boxplots ggplot2

I have this boxplot and I want to reorder the columns.我有这个箱线图,我想重新排序列。 Instead of MAR, MCAR, MNAR, I want MCAR, MAR, MNAR.我想要 MCAR、MAR、MNAR 而不是 MAR、MCAR、MNAR。 And the legend is wrong too, orange is ymiss and blue is yobs but I can't change it.传说也是错误的,橙色是ymiss,蓝色是yobs,但我无法改变它。 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)

箱形图

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. ggplot2首先将dados$mecanismo中的数据更改为一个因子。 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.要更改顺序,您需要将dados$mecanismo定义为数据框中的一个因素,然后还按特定顺序定义级别。

  2. Assigning a specific color in a legend to a factor .将图例中的特定颜色分配给因子 In your scale_fill_manual command, you are assigning values= .在您的scale_fill_manual命令中,您正在分配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 ).默认情况下,值的顺序是根据因子级别的顺序应用的(在本例dados$y )。 You can just re-specify the levels of that factor, but to assign colors specifically, you don't need to.您可以重新指定该因子的级别,但要专门分配 colors,您不需要。 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...您需要做的就是提供一个list(..或一个命名向量c('name'='color', 'name2'='color2',.. to values=来代替 colors 的向量。顺便说一句.. . 您可以通过设置上述#1 中所示的因子来更改ymissyobs在图例中出现的顺序...

Here's the code and plot:这是代码和 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.经过一些评论,很明显 OP 不仅打算根据ymissyobs的特定值重新着色数据,而且还更改图例中框的位置以匹配 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.我的意思是,在上面的 plot 中, yobs的箱线图在上面, ymiss在下面,但在右边的图例中,顺序是相反的。 The fix for this is to specify the ordering of labels explicitly through the breaks= argument of scale_fill_manual .对此的解决方法是通过scale_fill_manualbreaks=参数显式指定标签的顺序。 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')
  )

在此处输入图像描述

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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM