如何在 r 中更改和/或分离图形图例中的组件?
Posted
技术标签:
【中文标题】如何在 r 中更改和/或分离图形图例中的组件?【英文标题】:How to change and/or separate components in a figure legend in r? 【发布时间】:2020-07-15 22:19:17 【问题描述】:我在 R 中创建了下图,以可视化我运行的模型的原始数据和预测。预测以线的形式给出,原始数据以圆圈的形式给出。 2018 年的数据目前显示为空心圆圈或实线,而 2019 年的数据显示为实心圆圈和虚线。海洋鱼类数据是黑色的,而淡水鱼数据包括灰色的。就目前而言,这个传说有点难以理解。我想做两件事。
我不想在图例中将方向符号化为点和线,我想要一个显示各自颜色的矩形。
我想分离年份符号。我希望它的格式如下:
实线(空白)2018(空白)实心圆
虚线(空白)2019(空白)开圆
这是我的数据:
structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L
), .Label = c("2018", "2019"), class = "factor"), Yday = c(176L,
178L, 178L, 182L, 178L, 180L, 177L, 180L, 174L, 180L, 177L, 181L,
175L, 178L, 177L, 177L, 178L, 178L, 192L, 204L, 166L, 168L, 168L,
174L, 173L, 169L, 165L, 168L, 196L, 208L, 171L, 177L, 163L, 175L,
168L, 177L, 230L, 177L, 174L, 166L, 179L, 169L, 177L, 167L, 167L,
173L, 177L, 177L, 170L, 177L, 167L, 173L, 176L, 169L, 172L),
TL = c(63, 62.5, 63.5, 62, 62, 41, 58, 59, 74, 52.2, 45.7,
41, 59, 53.3, 56.5, 57, 42, 42.6, 76, 84, 73.8, 73.6, 73,
66, 69.4, 68, 68.6, 65.6, 80.8, 84.8, 68, 58, 58, 56, 56,
62.5, 62.5, 69, 64, 64, 67, 67, 64, 64, 60, 60, 64, 60, 65.8,
62, 62, 61, 55.2, 55.2, 67), Direction = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Marine",
"River"), class = "factor")), class = "data.frame", row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
56L))
这是当前图和第二个数据集:
Firsts.modelRand <- lmer(Yday~Year+Direction*TL + (1|Transmitter), data = firsts2WOFD,
REML = T, na.action = 'na.fail')
Firstdats <- expand.grid(TL = seq(min(firsts2WOFD$TL),max(firsts2WOFD$TL), by = 1),
Year = unique(firsts2WOFD$Year), Direction = unique(firsts2WOFD$Direction))
Firstdats$pred <- predict(Firsts.modelRand, Firstdats, re.form = ~0, type = 'response')
First.plot <- ggplot(data = firsts2WOFD, aes(x = TL, y = Yday, color = Direction, shape = Year, linetype = Year))+geom_point(size = 2.5) + theme_bw()+
geom_line(data = subset(Firstdats, Year == 2018 & Direction == 'River' & TL < 75), aes(x = TL, y = pred), size = 1.5)+
geom_line(data = subset(Firstdats, Year == 2019 & Direction == 'River' & TL < 75), aes(x = TL, y = pred), size = 1.5)+
geom_line(data = subset(Firstdats, Year == 2019 & Direction == 'Marine' & TL > 62), aes(x = TL, y = pred), size = 1.5)
First.plot <- First.plot + scale_color_manual(values = c("black", 'grey50'), labels = c("Marine", 'Fresh'))+scale_linetype_manual(values = c(1,2))+
scale_shape_manual(values = c(19,21))
First.plot <- First.plot+ theme(axis.title = element_text(size = 16), axis.text = element_text(size = 14),
legend.title = element_text(size = 16), legend.text = element_text(size = 14), legend.key.width = unit(1.7, 'cm'))
First.plot <- First.plot+ xlab("Total Length (cm)")+ ylab("Date of Entry")+scale_y_continuous(breaks = c(180,210,240), labels = c("Jun 29", 'Jul 29', 'Aug 28'))
First.plot
【问题讨论】:
您的 ggplot 代码中有两个数据集:firsts2WOFD and Firstdats
。但是您在问题中给出了一个数据集。
@Sathish 哎呀,你是对的。现在应该修复了
【参考方案1】:
在您的lmer()
调用中有一个名为Transmitter
的东西在您提供的数据中找不到。但由于问题只是关于绘图,我创建了一个新的简单线性模型,称为lm1
。线条的位置不正确,但应该足够接近以使绘图点穿过。
回答您的问题:
-
见
override.aes
例如this answer
不确定这是否可行,至少没有过于复杂的解决方案。我将形状更改为填充三角形,以便更容易区分上下以及颜色。我还让线条稍微变细(size = 1
),我认为这有助于使图例清晰。
# Create dataframe from provided data
# firsts2WOFD <- data
lm1 <- lm(Yday ~ TL + Direction + Year, data = firsts2WOFD)
dat_plot <- firsts2WOFD %>%
mutate(pred = predict(lm1, newdata = .))
ggplot(dat_plot) +
theme_bw() +
geom_point(aes(TL, Yday, shape = Direction, fill = Year, color = Year), size = 2.5) +
geom_line(aes(TL, pred, linetype = Direction, color = Year), size = 1) +
scale_fill_manual(values = c("black", 'grey50')) +
scale_color_manual(values = c("black", 'grey50'),
guide = guide_legend(override.aes = list(linetype = rep(NA, 2),
shape = c( 15, 15)))) +
scale_shape_manual(values = c(25, 24)) +
scale_linetype_manual(values = c(1, 2)) +
scale_y_continuous(breaks = c(180, 210, 240),
labels = c("Jun 29", 'Jul 29', 'Aug 28')) +
labs(x = 'Total Length (cm)', y = 'Date of Entry') +
theme(axis.title = element_text(size = 16),
axis.text = element_text(size = 14),
legend.title = element_text(size = 16),
legend.text = element_text(size = 14),
legend.key.width = unit(1.7, 'cm'))
【讨论】:
对不起,我错过了 Transmitter 不包括在内,但你是对的。那没那么重要。正如您所指出的,我开始意识到我的第二个问题可能是不可能的。我喜欢你为第一个问题所做的,但有没有办法更进一步,将年份标记为实心正方形而不改变包含点的形状? 刚刚更新了scale_color_manual
中的override.aes()
调用,以便绘制形状15,即正方形。以上是关于如何在 r 中更改和/或分离图形图例中的组件?的主要内容,如果未能解决你的问题,请参考以下文章