【问题标题】:ggplot2: how to change the order of a legendggplot2:如何更改图例的顺序
【发布时间】:2018-12-18 17:45:22
【问题描述】:
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"))

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

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

我尝试重新排序values 和labels,但随后条形图的颜色被打乱了。我希望保持颜色不变,即深紫色对应于 30 岁的情况,浅紫色对应于 50 岁的情况。深绿色对应 30 岁对照,浅绿色对应 50 岁对照。

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

【问题讨论】:

  • geom_barh函数从何而来?我无法让您的示例正常运行。
  • @bouncyball 来自library(ggstance)。感谢您指出这一点。

标签: r ggplot2 visualization


【解决方案1】:

在您的情况下,您想设置休息时间。 values 和 labels 不会更改顺序。它们应该按照休息的顺序给出。 interaction() 通过在两个类别之间放置一个句点来为您创建级别。您可以使用with(dat, levels(interaction(Status, Type))) 进行检查。然后您可以通过breaks= 订购五个。例如

scale_fill_manual("", 
                breaks = c("Case.Age30", "Case.Age50",
                           "Control.Age30", "Control.Age50"),
                values = c("#756bb1", "#2ca25f", 
                           "#bcbddc", "#99d8c9"), 
                labels = c("Age 30 Case", "Age 50 Case",
                           "Age 30 Control", "Age 50 Control"))

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 2011-10-15
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2020-04-10
    • 2016-06-02
    • 2020-12-02
    • 2021-10-04
    相关资源
    最近更新 更多