在 ggplot 中使用 geom_vline() 复制图例
Duplicate legend with geom_vline() in ggplot
我想创建这个图形:
aux_graf = structure(list(lines = structure(c(2L, 2L, 1L, 3L), .Label = c("h0",
"ic", "median"), class = "factor"), values = c(21.19755, 23.06978,
24, 22.13366)), .Names = c("lines", "values"), row.names = c(NA,
-4L), class = "data.frame")
dadosGraf = structure(list(dados = c(18.7997, 20.5035, 18.6214, 19.9192,
21.117, 20.8353, 17.527, 17.078, 17.6197, 21.4255, 18.7545, 19.2026,
18.4187, 20.7641, 21.0553, 17.5905, 18.7561, 18.9772, 20.3084,
18.8988, 19.1688, 19.2898, 22.059, 18.5854, 17.8896, 21.1609,
26.1371, 21.4737, 30.9934, 22.8421, 24.4133, 20.4137, 25.5475,
21.8791, 22.6706, 24.7531, 25.7219, 22.6389, 26.2308, 26.7998,
28.4708, 26.9941, 25.1489, 24.6179, 27.0194, 25.0589, 22.1119,
20.3069, 23.6758, 27.1201, 29.6136, 25.9948, 18.223, 23.7336,
22.4208), y2 = 1:55), .Names = c("dados", "y2"), class = "data.frame", row.names = c(NA,
-55L))
gp2 <- ggplot(data = aux_graf)+
geom_vline(aes(xintercept = values, colour=lines, linetype=lines), show_guide=TRUE)+
geom_point(data = dadosGraf, aes(x = dados, y=y2), size=3.2, shape=21, colour = 'black', fill = 'grey')+
theme_bw()
现在,我只想更改标签、颜色和线型,但是当我这样做时,它会添加另一个图例,我不知道为什么。
gp2 + scale_color_manual(name = '', labels = c(ic = 'Limites I.C.', median = 'Mediana', h0 = 'Hipotese Nula'), values = c(ic = 'red', median = 'black',h0 = 'grey40'))
+ scale_linetype_manual(values=c(ic = 'dashed', median = 'solid',h0 = 'dotted'))
有什么帮助吗?谢谢!
您可以结合使用 override.aes
和隐藏 linetype
图例。有:
gp2 +
scale_color_manual(name = '',
labels = c(ic = 'Limites I.C.', median = 'Mediana', h0 = 'Hipotese Nula'),
values = c(ic = 'red', median = 'black',h0 = 'grey40')) +
guides(color = guide_legend(override.aes = list(linetype = c('dotted', 'dashed', 'solid'))),
linetype = FALSE)
你会得到这个:
我想创建这个图形:
aux_graf = structure(list(lines = structure(c(2L, 2L, 1L, 3L), .Label = c("h0",
"ic", "median"), class = "factor"), values = c(21.19755, 23.06978,
24, 22.13366)), .Names = c("lines", "values"), row.names = c(NA,
-4L), class = "data.frame")
dadosGraf = structure(list(dados = c(18.7997, 20.5035, 18.6214, 19.9192,
21.117, 20.8353, 17.527, 17.078, 17.6197, 21.4255, 18.7545, 19.2026,
18.4187, 20.7641, 21.0553, 17.5905, 18.7561, 18.9772, 20.3084,
18.8988, 19.1688, 19.2898, 22.059, 18.5854, 17.8896, 21.1609,
26.1371, 21.4737, 30.9934, 22.8421, 24.4133, 20.4137, 25.5475,
21.8791, 22.6706, 24.7531, 25.7219, 22.6389, 26.2308, 26.7998,
28.4708, 26.9941, 25.1489, 24.6179, 27.0194, 25.0589, 22.1119,
20.3069, 23.6758, 27.1201, 29.6136, 25.9948, 18.223, 23.7336,
22.4208), y2 = 1:55), .Names = c("dados", "y2"), class = "data.frame", row.names = c(NA,
-55L))
gp2 <- ggplot(data = aux_graf)+
geom_vline(aes(xintercept = values, colour=lines, linetype=lines), show_guide=TRUE)+
geom_point(data = dadosGraf, aes(x = dados, y=y2), size=3.2, shape=21, colour = 'black', fill = 'grey')+
theme_bw()
现在,我只想更改标签、颜色和线型,但是当我这样做时,它会添加另一个图例,我不知道为什么。
gp2 + scale_color_manual(name = '', labels = c(ic = 'Limites I.C.', median = 'Mediana', h0 = 'Hipotese Nula'), values = c(ic = 'red', median = 'black',h0 = 'grey40'))
+ scale_linetype_manual(values=c(ic = 'dashed', median = 'solid',h0 = 'dotted'))
有什么帮助吗?谢谢!
您可以结合使用 override.aes
和隐藏 linetype
图例。有:
gp2 +
scale_color_manual(name = '',
labels = c(ic = 'Limites I.C.', median = 'Mediana', h0 = 'Hipotese Nula'),
values = c(ic = 'red', median = 'black',h0 = 'grey40')) +
guides(color = guide_legend(override.aes = list(linetype = c('dotted', 'dashed', 'solid'))),
linetype = FALSE)
你会得到这个: