将图例添加到基于图的误差条颜色
Adding legend to a plot based error bar color
我准备了示例数据。我想根据 error bar
颜色为此图添加图例。我尝试手动添加图例,但它不起作用。谢谢你的帮助。
data = data.frame(x = c(5, 10, 15, 20, 25, 30, 35, 40, 50),
Y1 = c(179, 212, 201, 204, 220, 181, 176, 219, 182),
SD1 = c(58, 93, 74, 55, 59 ,56, 53, 62, 62),
Y2 = c(273, 267, 329, 329, 386, 401, 399, 350, 274),
SD2 = c(107, 85, 141, 126, 94, 101, 65, 65, 58))
Y21 = data$Y2/5
SD21 = data$SD2/5
data = cbind(data, Y21, SD21)
ggplot(data=data,aes(x = x ,y=Y1))+
geom_errorbar(data=data,aes(ymin=Y1-SD1,ymax=Y1+SD1), colour="orange", width = 0.9, size = 1.5, linetype = "solid")+
geom_point(aes(y=Y1), color = "black")+
geom_errorbar(data=data,aes(ymin=Y2-SD2,ymax=Y2+SD2),color="blue", width = 0.9, size = 1.5, linetype = "solid")+
scale_y_continuous("first y axis", sec.axis = sec_axis(Y2~ .*(5) , name = "second y axis" ))+
geom_point(aes(y=Y2), color = "black")+
expand_limits(x = 0, y = 0)
您可以通过
跨多个几何添加图例
- 为每个geom在
aes()
内设置color = "label"
,其中"label"
是每个geom的唯一标签;然后
- 添加
scale_color_manual()
(或scale_linetype_manual()
等,取决于美学),并将values
arg设置为名称对应于geom标签和值的命名向量对应于所需的颜色(或线型,或其他)。
ggplot(data = data,aes(x = x, y = Y1)) +
geom_errorbar(
aes(ymin = Y1 - SD1, ymax = Y1 + SD1, color = "Y1"),
width = 0.9,
size = 1.5
)+
geom_point(aes(y = Y1), color = "black") +
geom_errorbar(
aes(ymin = Y2 - SD2, ymax = Y2 + SD2, color = "Y2"),
width = 0.9,
size = 1.5
) +
geom_point(aes(y = Y2), color = "black") +
scale_y_continuous(
"first y axis",
sec.axis = sec_axis(Y2 ~ .*(5), name = "second y axis")
) +
scale_color_manual("legend title", values = c(Y2 = "blue", Y1 = "orange")) +
expand_limits(x = 0, y = 0)
通常,如果您发现自己使用手动美学制作了多个相同的几何图形,这表明您应该 pivot your data to long format 并改用具有映射美学的单个几何图形。在您的情况下,这将为您提供图例并简化 ggplot2
规范。
library(tidyverse)
data %>%
pivot_longer(Y1:SD2, names_to = c(".value", "yNum"), names_pattern = "(\D+)(\d)") %>%
ggplot(aes(x = x, y = Y)) +
geom_errorbar(
aes(ymin = Y - SD, ymax = Y + SD, color = yNum),
width = 0.9,
size = 1.5
) +
geom_point(aes(y = Y), color = "black") +
scale_y_continuous(
"first y axis",
sec.axis = sec_axis(Y2 ~ .*(5), name = "second y axis")
) +
scale_color_manual(values = c("orange", "blue")) +
expand_limits(x = 0, y = 0)
不过,如果您确实需要或更喜欢使用单独的 geom,我已经在单独的答案中描述了如何跨多个 geom 生成图例。
我准备了示例数据。我想根据 error bar
颜色为此图添加图例。我尝试手动添加图例,但它不起作用。谢谢你的帮助。
data = data.frame(x = c(5, 10, 15, 20, 25, 30, 35, 40, 50),
Y1 = c(179, 212, 201, 204, 220, 181, 176, 219, 182),
SD1 = c(58, 93, 74, 55, 59 ,56, 53, 62, 62),
Y2 = c(273, 267, 329, 329, 386, 401, 399, 350, 274),
SD2 = c(107, 85, 141, 126, 94, 101, 65, 65, 58))
Y21 = data$Y2/5
SD21 = data$SD2/5
data = cbind(data, Y21, SD21)
ggplot(data=data,aes(x = x ,y=Y1))+
geom_errorbar(data=data,aes(ymin=Y1-SD1,ymax=Y1+SD1), colour="orange", width = 0.9, size = 1.5, linetype = "solid")+
geom_point(aes(y=Y1), color = "black")+
geom_errorbar(data=data,aes(ymin=Y2-SD2,ymax=Y2+SD2),color="blue", width = 0.9, size = 1.5, linetype = "solid")+
scale_y_continuous("first y axis", sec.axis = sec_axis(Y2~ .*(5) , name = "second y axis" ))+
geom_point(aes(y=Y2), color = "black")+
expand_limits(x = 0, y = 0)
您可以通过
跨多个几何添加图例- 为每个geom在
aes()
内设置color = "label"
,其中"label"
是每个geom的唯一标签;然后 - 添加
scale_color_manual()
(或scale_linetype_manual()
等,取决于美学),并将values
arg设置为名称对应于geom标签和值的命名向量对应于所需的颜色(或线型,或其他)。
ggplot(data = data,aes(x = x, y = Y1)) +
geom_errorbar(
aes(ymin = Y1 - SD1, ymax = Y1 + SD1, color = "Y1"),
width = 0.9,
size = 1.5
)+
geom_point(aes(y = Y1), color = "black") +
geom_errorbar(
aes(ymin = Y2 - SD2, ymax = Y2 + SD2, color = "Y2"),
width = 0.9,
size = 1.5
) +
geom_point(aes(y = Y2), color = "black") +
scale_y_continuous(
"first y axis",
sec.axis = sec_axis(Y2 ~ .*(5), name = "second y axis")
) +
scale_color_manual("legend title", values = c(Y2 = "blue", Y1 = "orange")) +
expand_limits(x = 0, y = 0)
通常,如果您发现自己使用手动美学制作了多个相同的几何图形,这表明您应该 pivot your data to long format 并改用具有映射美学的单个几何图形。在您的情况下,这将为您提供图例并简化 ggplot2
规范。
library(tidyverse)
data %>%
pivot_longer(Y1:SD2, names_to = c(".value", "yNum"), names_pattern = "(\D+)(\d)") %>%
ggplot(aes(x = x, y = Y)) +
geom_errorbar(
aes(ymin = Y - SD, ymax = Y + SD, color = yNum),
width = 0.9,
size = 1.5
) +
geom_point(aes(y = Y), color = "black") +
scale_y_continuous(
"first y axis",
sec.axis = sec_axis(Y2 ~ .*(5), name = "second y axis")
) +
scale_color_manual(values = c("orange", "blue")) +
expand_limits(x = 0, y = 0)
不过,如果您确实需要或更喜欢使用单独的 geom,我已经在单独的答案中描述了如何跨多个 geom 生成图例。