r - 在 ggplot2 中使用 geom_pointrange 显示图例的问题
问题描述
我在 R 中使用带有 geom_pointrange 的 ggplot2 来绘制六种场景下五组的灵敏度(95% CI)(总共 30 个灵敏度图)。这六个场景源自一组 3x2 的场景。我可以成功绘制图表,但我遇到了图例问题。我使用了三种不同的颜色以及实线和虚线来表示这六个场景。但是,我无法在图例上显示虚线(只有三种颜色,每种颜色重复两次)。最初我使用了六种不同的颜色,但同事要求我改变它。
我在 ggplot2 中使用了 geom_pointrange,但是在正确显示图例时遇到了问题:
sens <- cbind.data.frame(
group = rep(c("Grp1", "Grp2", "Grp3", "Grp4", "Grp5"), 6),
mean = c(0.70, 0.70, 0.65, 0.60, 0.72, 0.85, 0.84, 0.77, 0.68, 0.77,
0.71, 0.71, 0.69, 0.65, 0.76, 0.90, 0.88, 0.82, 0.82, 0.87,
0.78, 0.78, 0.79, 0.73, 0.83, 0.92, 0.92, 0.92, 0.90, 0.93) * 100,
lower = c(0.64, 0.64, 0.59, 0.55, 0.68, 0.73, 0.73, 0.65, 0.55, 0.68,
0.66, 0.66, 0.63, 0.59, 0.72, 0.80, 0.78, 0.70, 0.70, 0.79,
0.73, 0.72, 0.74, 0.68, 0.80, 0.84, 0.84, 0.82, 0.79, 0.87) * 100,
upper = c(0.75, 0.75, 0.70, 0.66, 0.76, 0.92, 0.91, 0.87, 0.80, 0.84,
0.77, 0.76, 0.74, 0.70, 0.79, 0.95, 0.94, 0.90, 0.90, 0.92,
0.82, 0.82, 0.83, 0.79, 0.86, 0.97, 0.97, 0.97, 0.96, 0.97) * 100,
type = rep(c("A1&B1", "A1&B2", "A2&B1", "A2&B2", "A3&B1", "A3&B2"), each=5),
type2 = rep(c(rep("B1", 5), rep("B2", 5)), 3),
type3 = rep(c(rep("A1", 10), rep("A2", 10), rep("A3", 10))))
ggplot(sens, aes(group, mean, colour = type, group = type, linetype = type)) +
geom_pointrange(aes(ymin = lower, ymax = upper), position = position_dodge(width = 0.5), size=1.25) +
coord_flip() +
xlab("Group") +
ylab("Sensitivity (95% CI)") +
scale_color_manual(values = c("red", "red", "dark grey", "dark grey", "blue", "blue")) +
scale_linetype_manual(values=c(1,2,1,2,1,2)) +
theme(axis.text.x=element_text(size=15, vjust=0.5, color = 'black'), # x-axis labels
axis.text.y=element_text(size=15, vjust=0.5, color = 'black'), # y-axis labels
axis.title.x=element_text(size=17.5, vjust=0.1), # x-title justification
axis.title.y=element_text(size=17.5, vjust=1.5), # y-title justification
legend.title=element_blank(),
legend.position= "bottom",
legend.text = element_text(size = 14.5)
)
我希望看到一个带有六个不同标签(3 种不同颜色 x 2 种不同线型)的图例。
解决方案
您可以删除图例中的点以使线条更明显并增加legend.key的大小:
ggplot(sens, aes(group, mean, colour = type, group = type, linetype = type)) +
geom_pointrange(aes(ymin = lower, ymax = upper), position = position_dodge(width = 0.5), size=1.25) +
coord_flip() +
xlab("Group") +
ylab("Sensitivity (95% CI)") +
scale_color_manual(values = c("red", "red", "dark grey", "dark grey", "blue", "blue"),
########### CHANGE HERE ###############
guide = guide_legend(override.aes = list(shape = NA))) +
scale_linetype_manual(values=c(1,2,1,2,1,2)) +
theme(axis.text.x=element_text(size=15, vjust=0.5, color = 'black'), # x-axis labels
axis.text.y=element_text(size=15, vjust=0.5, color = 'black'), # y-axis labels
axis.title.x=element_text(size=17.5, vjust=0.1), # x-title justification
axis.title.y=element_text(size=17.5, vjust=1.5), # y-title justification
legend.title=element_blank(),
legend.key.size = unit(2.5, "lines"), ########### CHANGE HERE ###############
legend.position= "bottom",
legend.text = element_text(size = 14.5)
)
一种不同的方法可能是改变点的形状:
ggplot(sens, aes(group, mean, colour = type, group = type, shape = type)) +
geom_pointrange(aes(ymin = lower, ymax = upper), position = position_dodge(width = 0.5), size=1.25) +
coord_flip() +
xlab("Group") +
ylab("Sensitivity (95% CI)") +
scale_color_manual(values = c("red", "red", "dark grey", "dark grey", "blue", "blue")) +
scale_shape_manual(values = c(16,17,16,17, 16,17)) +
theme(axis.text.x=element_text(size=15, vjust=0.5, color = 'black'), # x-axis labels
axis.text.y=element_text(size=15, vjust=0.5, color = 'black'), # y-axis labels
axis.title.x=element_text(size=17.5, vjust=0.1), # x-title justification
axis.title.y=element_text(size=17.5, vjust=1.5), # y-title justification
legend.title=element_blank(),
legend.position= "bottom",
legend.text = element_text(size = 14.5)
)
这也可以与不同的线型结合使用,但这可能会变得混乱......
推荐阅读
- ocr - PDF 文件 DOCUMENT_TEXT_DETECTION 跳过最后几行
- javascript - 在 Node 中连续、异步设置变量的最简洁方法
- sql - Oracle 更新和合并更新语句的性能
- css - 将单选按钮圆圈与容器顶部对齐
- javascript - Discord.js 命令更改 Config.json 中的变量
- r - 我可以使用 `map` 执行多个进程吗?
- python - 使用 Django sqlite3 数据库登录时出现问题
- keras - Keras 调谐器结果
- git - zsh git 自动完成:__git_find_on_cmdline 错误
- php - 在 Woocommerce 产品的产品属性值中允许 html