如何从 ggplot 中的箱线图中删除图例? [复制]

Posted

技术标签:

【中文标题】如何从 ggplot 中的箱线图中删除图例? [复制]【英文标题】:How can I remove the legend from this boxplot in ggplot? [duplicate] 【发布时间】:2020-01-14 14:59:46 【问题描述】:

请在下方找到My Data

如何从箱形图中删除红色的圆圈图例?

我希望保持相同的颜色和设计。我尝试了许多不同的解决方案,但不幸的是,这并没有解决问题。

这可能有点基本,但根本不知道如何解决这个问题。我希望你能提供帮助 - 在此先感谢!

我的脚本是:

df <- data.frame(x = as.factor(c(p$WHO.Grade)),
                 y = c(p$ki67pro),
                 f = rep(c("Ki67pro"), c(nrow(p))))



ggplot(df) +
  geom_boxplot(aes(x, y, fill = f, colour = f), outlier.alpha = 0, position = position_dodge(width = 0.78)) +
  scale_x_discrete(name = "", label=c("WHO-I\nn=108","WHO-II\nn=34","WHO-III\nn=1")) +
  scale_y_continuous(name="Ki-67 proliferative index", breaks=seq(0,30,5), limits=c(0,30)) +
  stat_boxplot(aes(x, y, colour = f), geom = "errorbar", width = 0.3,position = position_dodge(0.7753)) +
  geom_point(aes(x, y, fill = f, colour = f), size = 3, shape = 21, position = position_jitterdodge()) +
  scale_fill_manual(values = c("#52C1C76D"), name = "",
                    labels = c("\nTotal cohort\nn=159\n ")) +
  scale_colour_manual(values = c("#51BFC4"), name = "",
                      labels = c("\nTotal cohort\nn=159\n "))

还有My Data

p <- structure(list(WHO.Grade = c(1L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L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L, 3L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
38L, 39L, 40L, 41L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 
53L, 54L, 55L, 57L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 
81L, 82L, 83L, 84L, 85L, 87L, 89L, 90L, 91L, 92L, 93L, 94L, 96L, 
97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 
109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 
120L, 121L, 123L, 124L, 125L, 126L, 127L, 128L, 130L, 131L, 132L, 
133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 
144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 
155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 
166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L))

【问题讨论】:

【参考方案1】:

您可以按如下方式使用theme()

... + theme(legend.position = "none")

这应该消除传说

参考:https://www.datanovia.com/en/blog/ggplot-legend-title-position-and-labels/

【讨论】:

以上是关于如何从 ggplot 中的箱线图中删除图例? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

ggplot2 函数中 geom_boxplot 的平均值和中值箱线图图例

如何在ggplot的箱线图中按组绘制平均值

使用ggplot2画基因表达样本的箱线图

如何在r中的箱线图之间创建单独的线图

格式化在 ggplot 的箱线图中显示为文本的数字。解释箱线图外均值的定位

为ggplot2线图创建图例