[英]How to control the order of ggplot2::geom_pointrange elements by colour, shape and linetype
[英]How the get rid off shape from linetype legend and linetype from legend using geom_pointrange() in ggplot2 automatically?
我想对下图做两个调整:
linetype
(模型)相关的图例中的shape
shape
相关的图例中的linetype
(标准错误(SE))代码:
dgmedia2 <- structure(list(Parametros = structure(c(1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L), .Label = c("Intercept", "Bare.ground", "Canopy.cover",
"Shrub.cover", "Feral.mammal.dung", "Volume.lying.CWD"), class = "factor"),
Estimativas = c(-9.08718574009237, 0.246931091933284, 0.499326289121633,
-0.205561450009113, 1.07892606385103, -0.0296414533141213,
-0.0447905575590813, 0.182172232483717, -0.742126805654274,
0.222679274844953, 0.0590787053410465, -0.163485077690949,
0.131647768264277, 0.773377624910276, -7.22777909465642,
0.182391810011366, 0.422549145766675, -0.431294756177376,
1.10872202002585, -0.0291155842687582, -0.0464319202556404,
0.176542050008272, -0.78336973563058, 0.267174066310438,
0.0580762736413778, -0.165656899124107, 0.125965696446778,
0.73599525103876, -10.0274652596287, 0.282857360828957, 0.528018638631668,
-0.111429079805171, 0.987829625275738, -0.0259550802866814,
-0.0440101069770179, 0.177162265315325, -0.840395852786189,
-0.149771436010034, 0.0765372814155488, -0.148146161391884,
0.139214265328571, 0.87042464596807), ErroPadrao = c(6.04094497876376,
0.238714529427829, 0.312371860848067, 1.06699389843587, 0.657421168133384,
0.0401112312237702, 0.0572090823007306, 0.118716588061632,
1.30215431010075, 0.547252531459708, 0.0278076736099682,
0.0732591806388275, 0.0894031462569832, 0.807639175284003,
2.71519349846099, 0.131802564275492, 0.142804294834212, 0.890621281696227,
0.621662162048757, 0.0358381692477308, 0.053776574993421,
0.114266540855619, 0.96074807615628, 0.317046948689855, 0.0232030068948484,
0.0693821532984276, 0.0845515467581205, 0.653857443548544,
1.80370372228358, NaN, 0.346330990416065, 0.977284520368054,
NaN, 0.00173065248814043, 0.0640512617941663, 0.012960579747033,
0.386189704956875, 0.0997213546041854, 0.00866799939995657,
0.0462597890457268, 0.00656045805722545, 0.639150600868637
), Distribuição = c("Poisson", "Poisson", "Poisson", "Poisson",
"Poisson", "Poisson", "Poisson", "Poisson", "Poisson", "Poisson",
"Poisson", "Poisson", "Poisson", "Poisson", "NB", "NB", "NB",
"NB", "NB", "NB", "NB", "NB", "NB", "NB", "NB", "NB", "NB",
"NB", "COM-Poisson", "COM-Poisson", "COM-Poisson", "COM-Poisson",
"COM-Poisson", "COM-Poisson", "COM-Poisson", "COM-Poisson",
"COM-Poisson", "COM-Poisson", "COM-Poisson", "COM-Poisson",
"COM-Poisson", "COM-Poisson"), Tipo = c("Média", "Média",
"Média", "Média", "Média", "Média", "Média", "Média",
"Média", "Média", "Média", "Média", "Média", "Média",
"Média", "Média", "Média", "Média", "Média", "Média",
"Média", "Média", "Média", "Média", "Média", "Média",
"Média", "Média", "Média", "Média", "Média", "Média",
"Média", "Média", "Média", "Média", "Média", "Média",
"Média", "Média", "Média", "Média"), Response = c(9,
9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 9, 9, 9,
9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 9, 9, 9, 9, 10,
10, 10, 10, 10, 11, 11, 11, 11, 11), Names = structure(c(9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
11L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L,
11L, 11L, 11L), .Label = c("1.Amblyopone", "2.Aphaenogaster",
"3.Camponotus", "4.Camponotus", "5.Camponotus", "6.Camponotus",
"7.Camponotus", "8.Cardiocondyla", "9.Crematogaster", "10.Heteroponera",
"11.Iridomyrmex", "12.Iridomyrmex", "13.Iridomyrmex", "14.Iridomyrmex",
"15.Iridomyrmex", "16.Iridomyrmex", "17.Iridomyrmex", "18.Melophorus",
"19.Melophorus", "20.Melophorus", "21.Meranoplus", "22.Monomorium",
"23.Monomorium", "24.Monomorium", "25.Myrmecia", "26.Notoncus",
"27.Notoncus", "28.Nylanderia", "29.Ochetellus", "30.Paraparatrechina",
"31.Pheidole", "32.Pheidole", "33.Pheidole", "34.Pheidole",
"35.Polyrhachis", "36.Rhytidoponera", "37.Rhytidoponera",
"38.Solenopsis", "39.Stigmacros", "40.Tapinoma", "41.Tetramorium"
), class = "factor"), NAN = structure(c(2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", "16"), class = "factor")), row.names = c(NA,
-42L), class = "data.frame")
library(ggplot2)
library(latex2exp)
ggplot(dgmedia2) +
geom_pointrange(aes(x = Parametros,
y = Estimativas,
ymin = Estimativas-1.96*ErroPadrao,
ymax = Estimativas+1.96*ErroPadrao,
linetype = Distribuição,
shape = NAN),
# It controls the vertical distance between models
position = position_dodge(width = .6),
size = .5) +
theme_bw() +
theme(axis.title.y = element_blank(),
text = element_text(size = 20),
legend.position = "bottom") +
scale_shape_manual(values = c(1,20),
labels = c("Without SE", "With SE")) +
scale_linetype_manual(values=c("solid", "dashed", "dotted"))+
facet_wrap(~Names, scales = "free_x") +
coord_flip() +
labs(y = TeX('$\\hat{\\beta}\\pm 1.96 SE}$'),
linetype = "Model",
shape = "Standard Error (SE)")
我想这样做是因为从图例中不清楚哪种linetype
对应于 COM-Poisson 或 NB。 我在theme
上看到了legend.key.height = unit(1, 'cm')
的解决方法,但这对我没有 100% 的帮助。 我也考虑过制作手动图例的想法,但它会减慢编码过程。
谢谢。
这可以通过guide_legend
实现,它允许您通过override.aes
删除shape
和linetype
,如下所示:
library(ggplot2)
library(latex2exp)
ggplot(dgmedia2) +
geom_pointrange(aes(
x = Parametros,
y = Estimativas,
ymin = Estimativas - 1.96 * ErroPadrao,
ymax = Estimativas + 1.96 * ErroPadrao,
linetype = Distribuição,
shape = NAN
),
# It controls the vertical distance between models
position = position_dodge(width = .6),
size = .5
) +
theme_bw() +
theme(
axis.title.y = element_blank(),
text = element_text(size = 20),
legend.position = "bottom"
) +
scale_shape_manual(
values = c(1, 20),
labels = c("Without SE", "With SE")
) +
scale_linetype_manual(values = c("solid", "dashed", "dotted")) +
guides(linetype = guide_legend(override.aes = list(shape = NA)),
shape = guide_legend(override.aes = list(linetype = "blank"))) +
facet_wrap(~Names, scales = "free_x") +
coord_flip() +
labs(
y = TeX("$\\hat{\\beta}\\pm 1.96 SE}$"),
linetype = "Model",
shape = "Standard Error (SE)"
)
#> Warning: Removed 1 rows containing missing values (geom_segment).
#> Warning: Removed 1 rows containing missing values (geom_segment).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.