图例中的平方点变化 - ggplot R

Squared shaped points change in legend - ggplot R

我正在用 ggplot 绘制几个点,我想用一个空心方形点突出显示其中一个点。我通过以下方式进行:

ggplot(data.frame(x=rnorm(7), y=rnorm(7)), aes(x,y))+
geom_point(aes(shape = "Points"), size=1.4)+
geom_point(data = data.frame(x=rnorm(1),y=rnorm(1)), aes(shape="Square"), size = 1.9, stroke = 1.7) +
coord_cartesian(xlim = c(-3,3),ylim = c(-3,3))+
scale_shape_manual(name = "Shape", values = c(16,0))

图中的方形点很好,但图例中的那个比图中的点粗。更准确地说,它看起来更厚,但它实际上有第二个薄的内部正方形,当我使用 tikzDevice 导出绘图时可以看到它(见下图)。

即使尝试通过以下方式直接修改图例中的形状,问题仍然存在:

guides(shape = guide_legend(override.aes = list(shape=c(16,0))))

你能帮我看看如何让图例中的点看起来像点形状 0 吗?

您尝试过其中一个主题吗? +theme_economist() 可能会修复图例问题

您可以覆盖图例中的笔划宽度;如果将其设置为 1,则会得到两个完美重叠的正方形(如果将其设置为 NULL、0 或 NA,则根本不会得到 none):

library(ggplot2)
set.seed(1)
ggplot(data.frame(x = rnorm(7), y = rnorm(7)), aes(x, y)) +
    geom_point(aes(shape = "Points"), size = 1.4) +
    geom_point(
        data = data.frame(x = rnorm(1), y = rnorm(1)),
        aes(shape = "Square"),
        size = 1.9,
        stroke = 1.7
    ) +
    coord_cartesian(xlim = c(-3, 3), ylim = c(-3, 3)) +
    scale_shape_manual(name = "Shape", values = c(16, 0)) +
    guides(shape = guide_legend(override.aes = list(size=c(1, 1.4), stroke = 1)))

reprex package (v0.3.0)

于 2020 年 3 月 10 日创建