ggplot 的顺序忽略图例的顺序

问题描述 投票:0回答:1

使用以下数据,我尝试创建如图所示的图,但图例的顺序为:mgCST 组、CST 组、治疗状态。然而,ggplot 忽略了所有对图例进行排序的尝试

data <- structure(list(ID = c(138L, 139L, 140L, 71L, 72L, 73L), UID = c("UAB005_W10D5", 
"UAB005_W10D6", "UAB005_W10D7", "UAB005_W1D1", "UAB005_W1D2", 
"UAB005_W1D3"), SID = c("UAB005 - CST", "UAB005 - CST", "UAB005 - CST", 
"UAB005 - mgCST", "UAB005 - mgCST", "UAB005 - mgCST"), SERIAL = c(68, 
69, 70, 1, 2, 3), WEEK = c(10, 10, 10, 1, 1, 1), DAY = c(5, 6, 
7, 1, 2, 3), FULL.STATUS = structure(c(5L, 5L, 5L, 2L, 2L, 2L
), levels = c("Prior to SBV", "Prior to MET", "BV DX", "During MET", 
"After MET"), class = "factor"), NUGENT_SCORE = c(7, 7, 7, 0, 
3, 5), NUGENT_CLASS = c("BV", "BV", "BV", "NO_BV", "NO_BV", "INTER_BV"
), mgCST_CST = structure(c(28L, 28L, 28L, NA, NA, NA), levels = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"I", "III", "IV-A", "IV-B", "IV-C"), class = "factor"), subCST = c("IV-B", 
"IV-B", "IV-B", "III-B", "III-B", "III-B")), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list(
    SID = c("UAB005 - CST", "UAB005 - mgCST"), .rows = structure(list(
        1:3, 4:6), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))
MET <- ggplot(mapping = aes(x = SERIAL, y = SID,  shape = FULL.STATUS)) +
              geom_point(data = data.CST, aes(color = CST), size = 2) +
              scale_color_manual(values = CST.colors,
                                 name = "CST Group") +
              ggnewscale::new_scale_color() +
              geom_point(data = data.mgCST, aes(color = mgCST), size = 2) +
              scale_color_manual(values = mgCST.colors,
                                 name = "mgCST Group") +
              scale_shape_manual(values = status.shapes,
                                 name = "Treatment Status") +
              guides(color = guide_legend(order = 1)) +
              xlim(c(0, 70)) +
              xlab("Study Day") +
              ylab("SID") +
              theme_bw() + 
              theme(text = element_text(size = 12)) + 
              ggtitle("BV Dynamics MET Study")

我已经尝试了引导功能的所有迭代:

MET <- ggplot(mapping = aes(x = SERIAL, y = SID,  shape = FULL.STATUS)) +
              geom_point(data = data.CST, aes(color = CST), size = 2) +
              scale_color_manual(values = CST.colors,
                                 name = "CST Group") +
              ggnewscale::new_scale_color() +
              geom_point(data = data.mgCST, aes(color = mgCST), size = 2) +
              scale_color_manual(values = mgCST.colors,
                                 name = "mgCST Group") +
              scale_shape_manual(values = status.shapes,
                                 name = "Treatment Status") +
              guides(color = guide_legend(order = 1),
                     color = guide_legend(order = 2),
                     shape = guide_legend(order = 3)) +
              xlim(c(0, 70)) +
              xlab("Study Day") +
              ylab("SID") +
              theme_bw() + 
              theme(text = element_text(size = 12)) + 
              ggtitle("BV Dynamics MET Study")
MET <- ggplot(mapping = aes(x = SERIAL, y = SID,  shape = FULL.STATUS)) +
              geom_point(data = data.CST, aes(color = CST), size = 2) +
              scale_color_manual(values = CST.colors,
                                 name = "CST Group") +
              ggnewscale::new_scale_color() +
              geom_point(data = data.mgCST, aes(color = mgCST), size = 2) +
              scale_color_manual(values = mgCST.colors,
                                 name = "mgCST Group") +
              scale_shape_manual(values = status.shapes,
                                 name = "Treatment Status") +
              xlim(c(0, 70)) +
              xlab("Study Day") +
              ylab("SID") +
              theme_bw() + 
              theme(text = element_text(size = 12)) + 
              ggtitle("BV Dynamics MET Study")

MET + guides(color = guide_legend(order = 1),
             shape = guide_legend(order = 3))

以及以下内容:

guides_merge <- function(gdefs) {
  gdefs <- lapply(gdefs, function(g) { g$hash <- paste(g$order, g$hash, sep = "z"); g})
  tapply(gdefs, sapply(gdefs, function(g)g$hash), function(gs)Reduce(guide_merge, gs))
}
environment(guides_merge) <- environment(ggplot)
assignInNamespace("guides_merge", guides_merge, pos = "package:ggplot2")

MET <- ggplot(mapping = aes(x = SERIAL, y = SID,  shape = FULL.STATUS)) +
              geom_point(data = data.CST, aes(color = CST), size = 2) +
              scale_color_manual(values = CST.colors,
                                 name = "CST Group") +
              ggnewscale::new_scale_color() +
              geom_point(data = data.mgCST, aes(color = mgCST), size = 2) +
              scale_color_manual(values = mgCST.colors,
                                 name = "mgCST Group") +
              scale_shape_manual(values = status.shapes,
                                 name = "Treatment Status") +
              guides(color = guide_legend(order = 1),
                     color = guide_legend(order = 2),
                     shape = guide_legend(order = 3)) +
              xlim(c(0, 70)) +
              xlab("Study Day") +
              ylab("SID") +
              theme_bw() + 
              theme(text = element_text(size = 12)) + 
              ggtitle("BV Dynamics MET Study")

我已经更新了 ggplot2,希望得到任何帮助!

r ggplot2 legend
1个回答
0
投票

Vanilla

ggplot
不处理相同美学的多个图例。当您有
guides(color = ..., color = ...)
时,它无法知道哪个颜色图例是哪个,并且由于它只期望一种颜色图例,因此可能无法正常运行。

我在

ggnewscale
github 存储库的问题中搜索了“order”并发现了这个,这表明将
guide = guide_legend(order = foo)
放入
scale
调用中将会起作用。

MET <- ggplot(mapping = aes(x = SERIAL, y = SID,  shape = FULL.STATUS)) +
              geom_point(data = data.CST, aes(color = CST), size = 2) +
              scale_color_manual(
                values = CST.colors,
                name = "CST Group",
                guide = guide_legend(order = 1)
              ) +
              ggnewscale::new_scale_color() +
              geom_point(data = data.mgCST, aes(color = mgCST), size = 2) +
              scale_color_manual(
                values = mgCST.colors,
                name = "mgCST Group",
                guide = guide_legend(order = 2)
              ) +
              scale_shape_manual(
                values = status.shapes,
                name = "Treatment Status",
                guide = guide_legend(order = 3)
              ) +
              xlim(c(0, 70)) +
              xlab("Study Day") +
              ylab("SID") +
              theme_bw() + 
              theme(text = element_text(size = 12)) + 
              ggtitle("BV Dynamics MET Study")
© www.soinside.com 2019 - 2024. All rights reserved.