mva*_*man 2 plot r scatter-plot ggplot2 tidyverse
我有这个情节:
library(ggplot2)
ggplot(iris,
aes(
x = Petal.Length,
y = Petal.Width,
color = Species,
linetype = Species,
shape = Species
)) +
geom_count(alpha = 0.20) +
geom_smooth(method = "lm", se = FALSE) +
labs(
color = "Species (Line)",
linetype = "Species (Line)",
shape = "Species (Points)"
) +
guides(
size = "none"
)
#> `geom_smooth()` using formula = 'y ~ x'
Run Code Online (Sandbox Code Playgroud)

创建于 2023 年 11 月 2 日,使用reprex v2.0.2
我正在尝试格式化图例,以便有一个具有点形状(也按颜色)的图例和一个单独的线型图例(也按颜色)。
我实际上得到的是一个仅用于形状(无颜色)的图例和一个具有彩色点的线型图例。我已经尝试了我能想到的图例标签的每一次迭代,但都无济于事。
问题:保持其他一切不变,我怎样才能摆脱物种(线)图例中的那些点?以及如何为形状添加颜色?
您需要删除颜色参考线并覆盖形状和线型参考线的美观性:
library(ggplot2)
ggplot(iris,
aes(
x = Petal.Length,
y = Petal.Width,
color = Species,
linetype = Species,
shape = Species
)) +
geom_count(alpha = 0.20) +
geom_smooth(method = "lm", se = FALSE) +
labs(
linetype = "Species (Line)",
shape = "Species (Points)"
) +
guides(
size = "none",
color = "none",
shape = guide_legend(override.aes = list(alpha = 1,
color = scales::hue_pal()(3))),
linetype = guide_legend(override.aes = list(color = scales::hue_pal()(3)))
)
Run Code Online (Sandbox Code Playgroud)
然而,我可能会谦虚地建议您遇到这种困难的原因是您将相同的变量映射到多种美学上。对于清晰易读的情节来说,这是完全没有必要的——在两个图例之间来回翻转来解释情节只会让读者更难,而不是更容易。我建议摆脱图表垃圾,也许是这样的:
library(geomtextpath)
ggplot(iris, aes(x = Petal.Length, y = Petal.Width, group = Species)) +
geom_count(alpha = 0.3, aes(color = Species)) +
geom_textsmooth(aes(label = Species), method = "lm", se = FALSE,
vjust = -0.2, size = 6) +
scale_size_area() +
guides(size = "none", color = "none") +
theme_classic(base_size = 20)
Run Code Online (Sandbox Code Playgroud)
我认为这样可以更清楚地展示数据。
| 归档时间: |
|
| 查看次数: |
56 次 |
| 最近记录: |