温馨提示:本文翻译自stackoverflow.com,查看原文请点击:r - Creating a grouped boxplot with separately coloured dots in ggplot
ggplot2 r

r - 使用ggplot创建带有单独彩色点的分组箱线图

发布于 2020-11-09 00:27:32

我想制作一个分组的箱线图(通过“ majorCluster”和“治疗”变量),其上覆盖点由不同的变量(“ aid”变量)着色。我尝试过的所有方法都改变了点的颜色,但是位置与箱形图不匹配。在此先感谢您的协助!

这是我的代码:

  ggplot(data = boxplot.data, aes(x=majorCluster, y=expa)) +
  geom_boxplot(aes(color = fct_rev(factor(treatment))), width = 0.5, size = 0.4, position = position_dodge(0.8)) +
  geom_dotplot(binaxis = "y", stackdir = "center", trim = FALSE, dotsize = 0.5,position = position_dodge(0.8), aes(color = fct_rev(factor(treatment)), fill = fct_rev(factor(treatment)))) +
  theme_classic()

这是输出(期望的输出=根据“ aid”变量,点具有不同的颜色) 在此处输入图片说明

这是数据(dput):

boxplot.data <- structure(list(aid = c("Anti-PD-L1 A", "Anti-PD-L1 A", "Anti-PD-L1 A", 
"Anti-PD-L1 A", "Anti-PD-L1 B", "Anti-PD-L1 B", "Anti-PD-L1 B", 
"Anti-PD-L1 B", "Anti-PD-L1 C", "Anti-PD-L1 C", "Anti-PD-L1 C", 
"Anti-PD-L1 C", "Anti-PD-L1 D", "Anti-PD-L1 D", "Anti-PD-L1 D", 
"Anti-PD-L1 D", "Untreated A", "Untreated A", "Untreated A", 
"Untreated A", "Untreated B", "Untreated B", "Untreated B", "Untreated B"
), majorCluster = c("1", "2", "3", "4", "1", "2", "3", "4", "1", 
"2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4", "1", "2", 
"3", "4"), expa = c(0.00229641856789997, 0.00449718430290869, 
0.0079476155988667, 0.0323137314979365, 0.125655399176487, 0.0737804421330638, 
0.0552815368690545, 0.0467782242054685, 0.00143345514424931, 
0.0055910433498606, 0.00647918094834399, 0.0150090372519559, 
0.0167864512842503, 0.0243240630288888, 0.0404105646363485, 0.0267541240801361, 
0.00925515666671728, 0.0115509845370231, 0.0276374627000041, 
0.0438400723313962, 0.0403818878060491, 0.0481121640688924, 0.0545189407651033, 
0.0764620370634215), migr = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), tran = c(0.335015348105744, 
0.376269513326694, 0.449228611310031, 0.446682573678422, 0.842635301541922, 
0.765588148505148, 0.683233418019828, 0.610651263240787, 0.18994141256059, 
0.232057278886713, 0.281840445619523, 0.351757256110902, 0.472126089570835, 
0.599354493054942, 0.707113638164233, 0.810238120454871, 0.483691176581404, 
0.609864164601998, 0.685779002499795, 0.716753100388738, 0.743285518118052, 
0.708765108329113, 0.74453010103349, 0.912753289862157), treatment = c("Atezo", 
"Atezo", "Atezo", "Atezo", "Atezo", "Atezo", "Atezo", "Atezo", 
"Atezo", "Atezo", "Atezo", "Atezo", "Atezo", "Atezo", "Atezo", 
"Atezo", "None", "None", "None", "None", "None", "None", "None", 
"None")), row.names = c("17", "21", "31", "41", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "111", "211", 
"311", "411", "51", "61", "71", "81"), class = "data.frame")

查看更多

提问者
Gordon Beattie
被浏览
0
Tjebo 2020-06-09 19:27

添加group到您的aes呼叫中geom_point(我使用而不是geom_dot,因为我想这就是您想要的...)

使用可填充形状进行填充,因此您可以将颜色和填充美感用于不同的图例

library(tidyverse)

ggplot(data = boxplot.data, aes(x=majorCluster, y=expa)) +
  geom_boxplot(aes(color = fct_rev(factor(treatment))), width = 0.5, size = 0.4, position = position_dodge(0.8)) +
  geom_point(position = position_dodge(0.8), 
             aes(group = fct_rev(factor(treatment)), 
                 fill = aid), shape = 21) +
  theme_classic()

reprex软件包(v0.3.0)创建于2020-06-09