ggplot2中多geom图层颜色统一及comparison图例移除问题
问题解决:统一ggplot2多图层颜色并移除指定图例
需求说明
已通过scale_fill_manual为stat_halfeye和geom_boxplot设置了Answerer为lemonchiffon、NotAnswerer为darkorange的填充色,需完成以下调整:
- 将
geom_point和geom_path中原偏蓝色的点/线条(对应comparison=0)改为lemonchiffon - 将原偏灰色的点/线条(对应
comparison=1)改为darkorange - 移除
comparison对应的颜色图例
修改要点
- 新增
scale_color_manual,把comparison的两个取值映射到目标颜色 - 在
guides参数中添加color = "none",移除comparison的图例
修改后的完整代码
library(ggplot2) library(tidyquant) library(ggdist) library(ggthemes) box_width <- .12 jitter_width <- .025 pj <- position_jitter(seed = 1, width = jitter_width, height = 0) ggplot(data = df, aes(x = name, y = value, fill = name)) + # 半小提琴图 stat_halfeye( aes( justification = ifelse(name == "Answerer", 1.2, -.2), side = ifelse(name == "Answerer", "left", "right") ), adjust = 0.5, .width = 0, point_colour = NA ) + # 箱线图 geom_boxplot( width = box_width, alpha = 0.5 ) + scale_fill_manual(values = c("Answerer" = "lemonchiffon", "NotAnswerer" = "darkorange"))+ # 散点 geom_point( aes(x = name_num, color = comparison), alpha = 0.5, position = pj ) + # 连接线 geom_path(aes(x = name_num, group = IPU_id, color = comparison), alpha = 0.5, position = pj, ) + # 新增颜色映射并移除comparison图例 scale_color_manual(values = c("0" = "lemonchiffon", "1" = "darkorange")) + guides(fill = "none", color = "none")
验证数据
df <- structure(list(IPU_id = c(2L, 2L, 116L, 116L, 352L, 352L, 481L, 481L, 583L, 583L, 1077L, 1077L, 1394L, 1394L, 1447L, 1447L, 1538L, 1538L, 1982L, 1982L, 2459L, 2459L, 2729L, 2729L, 2763L, 2763L, 2809L, 2809L, 3162L, 3162L, 3165L, 3165L, 3188L, 3188L, 3249L, 3249L, 3454L, 3454L, 3618L, 3618L, 3780L, 3780L, 3796L, 3796L, 3972L, 3972L, 4407L, 4407L, 4779L, 4779L), comparison = c(0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L), name = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), levels = c("Answerer", "NotAnswerer" ), class = "factor"), value = c(-0.0431558441558438, 1.70782692307692, 0.684046511627906, 0.423953124999999, 0.302268456375839, -0.309683333333333, 0.792993421052627, -0.871427152317877, 2.55586178861788, -0.85030081300813, -0.241509900990097, 0.026694999999999, 0.880453020134229, -0.298657718120805, -0.0876920529801324, -0.250344370860927, -6.02329885057471, -8.22881976744186, -6.48995, 1.035525, 9.35135416666668, -2.60511643835616, 0.643900662251654, 0.167112582781379, 0.572819078947369, -2.0765165562914, 0.462728395061728, -0.141359756097561, 0.76575, -1.60808333333333, -0.894208609271523, -0.786271523178809, 0.754248201438848, -0.160610714285716, -0.338933884297521, -1.21221008403362, 6.022116, -0.524599999999991, 0.425596774193548, 0.163237012987012, 0.189037634408601, -2.02574175824177, 0.1978, 1.16077962962963, -1.45908116883117, 0.964378289473683, 0.421305555555556, 0.540716783216783, 0.16981683168317, -0.798830000000001), name_num = c(1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675, 1.1325, 1.8675)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"))
内容的提问来源于stack exchange,提问作者Chris Ruehlemann
相关产品推荐
相关产品推荐

