r - 如何将颜色图例添加到具有不同几何图层的 ggplot
问题描述
我已经搜索并找到了几个我认为可以解决此问题的示例,但是当我实施它们时似乎没有任何效果。
我有一个小标题,并创建了一个由两条带有误差线的 geom_lines 组成的图表。我想在一侧创建一个图例,在红线旁边显示“肢体 A”,在蓝线旁边显示“肢体 B”。尽管感觉每次我都根据搜索时出现的合理解决方案修改代码,但我所做的任何事情似乎都毫无用处。
所以,这是我的代码和一个例子。如果有人可以指导我,我将不胜感激。我的假设是我错过了一些非常基本的东西,但我完全迷失了。
compliance_graph <- ggplot(compliance_stats) +
aes(x = Compliance_B_L_per_cmH2O, y = Mean_VtB) +
geom_line(colour = 'deepskyblue', size = 0.75) +
geom_point(colour = 'deepskyblue', size = 1.0) +
geom_errorbar(aes(ymin = (Mean_VtB - SD_VtB),
ymax = (Mean_VtB + SD_VtB),
width= 0.003),
colour = 'deepskyblue') +
geom_line(aes(y = Mean_VtA, colour = 'Limb A'), colour = 'firebrick4', size = 0.75) +
geom_point(aes(y = Mean_VtA), colour = 'firebrick4', size = 1) +
geom_errorbar(aes(ymin = (Mean_VtA - SD_VtA),
ymax = (Mean_VtA + SD_VtA),
width= 0.003),
colour = 'firebrick4') +
labs(
title = 'Compliance of Limb A vs Tidal Volume',
x = 'Limb A Compliance (L/cmH2O)',
y = 'Mean Tidal Volume (mL)')
compliance_graph <- compliance_graph +
scale_x_continuous(expand = c(0, 0), breaks = breaks_width(0.01)) +
scale_y_continuous(expand = c(0, 0), breaks = breaks_width(50), limits = c(150, 450))
dput(compliance_stats) 返回以下内容:
structure(list(Compliance_B_L_per_cmH2O = c(0.01, 0.02, 0.03,
0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14,
0.15), Vent_P1 = c(10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10), Vent_PEEP = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5), Vent_RR = c(14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14), Vent_IE = c("1:2", "1:2", "1:2", "1:2",
"1:2", "1:2", "1:2", "1:2", "1:2", "1:2", "1:2", "1:2", "1:2",
"1:2", "1:2"), APL_pos = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3), Mean_VtA = c(344, 359, 377, 380, 379, 382, 377, 377,
377, 376, 375, 374, 375, 375, 376), Min_VtA = c(342, 358, 374,
379, 375, 380, 376, 375, 375, 375, 372, 373, 374, 373, 373),
Max_VtA = c(345, 360, 380, 381, 385, 385, 378, 378, 379,
377, 377, 376, 376, 376, 379), SD_VtA = c(2, 1, 3, 1, 5,
3, 1, 2, 2, 1, 3, 2, 1, 2, 3), Mean_PEEPA = c(6.9, 7, 7.1,
7.2, 7.3, 7.3, 7.4, 7.4, 7.5, 7.5, 7.4, 7.4, 7.4, 7.5, 7.4
), Min_PEEPA = c(6.9, 6.9, 7, 7.2, 7.3, 7.3, 7.4, 7.4, 7.4,
7.4, 7.4, 7.3, 7.3, 7.4, 7.3), Max_PEEPA = c(6.9, 7, 7.1,
7.2, 7.4, 7.4, 7.5, 7.5, 7.5, 7.6, 7.5, 7.6, 7.5, 7.6, 7.4
), SD_PEEPA = c(0, 0.1, 0.1, 0, 0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.2, 0.1, 0.1, 0.1), Mean_PeakA = c(14.2, 14.7,
15.4, 15.9, 16.1, 16.3, 16.3, 16.4, 16.4, 16.5, 16.4, 16.5,
16.4, 16.5, 16.5), Min_PeakA = c(14.2, 14.6, 15.3, 15.9,
16.1, 16.2, 16.1, 16.3, 16.3, 16.4, 16.4, 16.5, 16.4, 16.4,
16.4), Max_PeakA = c(14.2, 14.7, 15.5, 15.9, 16.1, 16.3,
16.4, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.6), SD_PeakA = c(0,
0.1, 0.1, 0, 0, 0.1, 0.2, 0.1, 0.1, 0.1, 0.1, 0, 0.1, 0.1,
0.1), Mean_VtB = c(167, 229, 319, 362, 386, 396, 401, 405,
407, 409, 409, 412, 412, 415, 414), Min_VtB = c(166, 228,
316, 358, 385, 393, 399, 404, 406, 409, 408, 411, 410, 412,
413), Max_VtB = c(168, 230, 321, 364, 386, 400, 402, 405,
408, 410, 410, 412, 414, 417, 416), SD_VtB = c(1, 1, 3, 3,
1, 4, 2, 1, 1, 1, 1, 1, 2, 3, 2), Mean_PEEPB = c(6.4, 6.4,
6.5, 6.6, 6.9, 7, 7.1, 7.2, 7.1, 7.2, 7.2, 7.3, 7.3, 7.3,
7.3), Min_PEEPB = c(6.3, 6.3, 6.4, 6.5, 6.7, 6.9, 7, 7.1,
7, 7.1, 7.1, 7.1, 7.2, 7.2, 7.2), Max_PEEPB = c(6.4, 6.5,
6.6, 6.7, 7, 7.1, 7.3, 7.3, 7.3, 7.3, 7.3, 7.5, 7.5, 7.5,
7.5), SD_PEEPB = c(0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.2, 0.1,
0.2, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2), Mean_PeakB = c(18, 18,
17.9, 17.8, 17.8, 17.7, 17.5, 17.5, 17.5, 17.4, 17.4, 17.4,
17.3, 17.2, 17.2), Min_PeakB = c(17.9, 18, 17.8, 17.8, 17.7,
17.6, 17.4, 17.4, 17.3, 17.3, 17.3, 17.2, 17.1, 17.1, 17),
Max_PeakB = c(18.2, 18.1, 18, 17.9, 17.8, 17.8, 17.6, 17.5,
17.7, 17.5, 17.5, 17.6, 17.4, 17.4, 17.4), SD_PeakB = c(0.2,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.1, 0.2, 0.2,
0.2, 0.2)), row.names = c(NA, -15L), groups = structure(list(
Compliance_B_L_per_cmH2O = c(0.01, 0.02, 0.03, 0.04, 0.05,
0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15
), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -15L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
解决方案
如果你想有一个图例,你必须在美学上进行映射,即color
进入aes()
并使用scale_color_manual
来设置正确的颜色:
library(ggplot2)
compliance_graph <- ggplot(compliance_stats) +
aes(x = Compliance_B_L_per_cmH2O, y = Mean_VtB) +
geom_line(aes(colour = "Limb B"), size = 0.75) +
geom_point(aes(colour = "Limb B"), size = 1.0) +
geom_errorbar(aes(
ymin = (Mean_VtB - SD_VtB),
ymax = (Mean_VtB + SD_VtB),
width = 0.003,
colour = "Limb B"
)) +
geom_line(aes(y = Mean_VtA, colour = "Limb A"), size = 0.75) +
geom_point(aes(y = Mean_VtA, colour = "Limb A"), size = 1) +
geom_errorbar(aes(
ymin = (Mean_VtA - SD_VtA),
ymax = (Mean_VtA + SD_VtA),
colour = "Limb A",
width = 0.003
)) +
scale_color_manual(values = c("Limb A" = "firebrick4", "Limb B" = "deepskyblue")) +
labs(
title = "Compliance of Limb A vs Tidal Volume",
x = "Limb A Compliance (L/cmH2O)",
y = "Mean Tidal Volume (mL)"
)
compliance_graph <- compliance_graph +
scale_x_continuous(expand = c(0, 0), breaks = scales::breaks_width(0.01)) +
scale_y_continuous(expand = c(0, 0), breaks = scales::breaks_width(50), limits = c(150, 450))
compliance_graph
推荐阅读
- c# - 代码在 .Net 5 中有效,但在 .Net 6 中无效
- python-3.x - Python-pptx 库标准幻灯片
- firebase - Firebase Web SDK 对所有 httpsCallable 请求使用自定义域
- c# - 将表单名称传递给用户控件
- ruby-on-rails - Rails 6 将哈希合并到 ActiveRecord 对象
- flutter - 如何获取任意定位的矩形,并将其在视图中居中,并仅使用 Canvas 变换操作对其进行缩放?
- reactjs - 如何在本机反应中的当前对话框之后显示对话框
- windows - % 和 有什么区别!!在批处理文件中?
- python - 如何搜索具有多个键的字典列表?
- python-3.x - ModuleNotFoundError:没有名为“numpy.core._multiarray_umath”的模块