ggplot2:如何更改图例的顺序

时间:2018-07-10 19:23:15

标签: r ggplot2 visualization

library(tidyverse)
library(ggplot2)
library(ggstance)
dat <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                           1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Female", "Male"
                                           ), class = "factor"), Status = c("Case", "Case", "Case", "Case", 
                                                                            "Control", "Control", "Control", "Control", "Case", "Case", "Case", 
                                                                            "Case", "Control", "Control", "Control", "Control"), Type = c("Age30", 
                                                                                                                                          "Age30", "Age30", "Age30", "Age30", "Age30", "Age30", "Age30", 
                                                                                                                                          "Age50", "Age50", "Age50", "Age50", "Age50", "Age50", "Age50", 
                                                                                                                                          "Age50"), Risk = c(21.59862, 3.27479, 1.10073, 1.70754, 8.85253, 
                                                                                                                                                             1.66318, 0.23228, 0.44844, 18.01182, 3.80135, 1.40662, 2.75944, 
                                                                                                                                                             4.81212, 1.93184, 0.29695, 0.72521), Disease = c("D1", "D2", 
                                                                                                                                                                                                              "D3", "D4", "D1", "D2", "D3", "D4", "D1", "D2", "D3", "D4", "D1", 
                                                                                                                                                                                                              "D2", "D3", "D4")), .Names = c("Gender", "Status", "Type", "Risk", 
                                                                                                                                                                                                                                             "Disease"), row.names = c(NA, -16L), class = "data.frame")

ggplot(dat, aes(x = Risk, y = forcats::fct_reorder(Status, Risk), 
                           group = Status,
                           fill = Type)) +
  facet_grid(Disease ~ ., switch = 'y') +
  geom_barh(aes(fill = interaction(Status, Type)), 
            stat = 'identity', position = 'stackv', color = "darkgrey",
            size = 0.3) +
  scale_x_continuous(limits = c(0, 100)) +
  labs( y = "Disease", x = "Risk",
       plot.title = element_text(hjust = 0.5)) + 
  theme_minimal() + 
  theme(
    text = element_text(size=10),
    strip.text.y = element_text(angle = 0),
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank(),
    plot.title = element_text(size = 11, hjust = 0.5),
    legend.position = "bottom",
    legend.key.size = unit(0.2, "cm")) +
  scale_fill_manual("", 
                    values = c("#756bb1", "#2ca25f", "#bcbddc",
                                "#99d8c9"),
                    labels = c("Age 30 Case", "Age 30 Control", 
                               "Age 50 Case", "Age 50 Control"))

enter image description here

我想将图例标签的顺序更改为以下(从左到右):

[ ] Age 30 Case  [ ] Age 50 Case  [ ] Age 30 Control  [ ] Age 50 Control

我尝试重新排列valueslabels的顺序,但是随后这些条的颜色被重新排列。我希望保持颜色不变,即深紫色对应30岁的情况,浅紫色对应50岁的情况。深绿色对应年龄30控制,浅绿色对应年龄50控制。

我知道颜色对应于变量的级别,但是在我的绘图中,颜色取决于2个变量的组合。因此,我想不出一种使用relevel并更改图例标签顺序的方法。

1 个答案:

答案 0 :(得分:4)

您要设置中断。 <ion-grid no-padding> <ion-row> <ion-col col-10> <h4 > <strong>{{ antecedentAlert.antPac.get_tipo_antecedente.nombre }}</strong> <span *ngIf="antecedentAlert.antPac.get_subtipo_antecedente != null"> : {{ antecedentAlert.antPac.get_subtipo_antecedente.nombre }} </span> . . . </ion-col> <ion-col col-auto> <span class="contenedor-badge-icon"> <ion-badge class="badge-estado-ant" color="azul-2" *ngIf="antecedentAlert.antPac.validado == 'validado' ">Validado</ion-badge> <ion-badge class="badge-estado-ant" color="danger" *ngIf="antecedentAlert.antPac.validado == 'no validado' ">No validado</ion-badge> <ion-badge class="badge-estado-ant" color="naranja-2" *ngIf="antecedentAlert.antPac.validado == 'pendiente' "><ion-icon class="candado-antecedentes" [name]="antecedentAlert.antPac.privado=='1'?'lock':''" [color]="white"></ion-icon> Por validar</ion-badge> </span> </ion-col> </ion-row> </ion-grid> values不会更改顺序。应该按照休息的顺序给他们。 labels通过在两个类别之间放置一个句点来为您创建级别。您可以使用interaction()进行检查。然后,您可以通过with(dat, levels(interaction(Status, Type)))给五个订单。例如

breaks=

enter image description here