r - ggplot2:如何更改图例的顺序

标签 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 的方法并更改我的图例标签的顺序。

最佳答案

在您的情况下,您想设置休息时间。 valueslabels不会改变顺序。他们应该按照休息的顺序给出。 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"))

enter image description here

关于r - ggplot2:如何更改图例的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51272689/

相关文章:

r - 计算 R 中矩阵中每个唯一列出现次数的最快方法

r - 尝试创建一个确保列表长度为 2 的函数

r - ggplot 图例 : geom_abline interference

r - ggplot中的两个填充变量

string - 可视化算法的工具

r - R中具有 map 和图层的ggplot2 stat_density2d的透明度和Alpha级别

python - 使用 python 和 networkx 进行大图可视化

r - 如何将用户定义的图例添加到 R 中的 ggplot?

r - 如何延长facet gridded ggplot中的特定刻度线?

r - 在 R 中聚合