首页 > 解决方案 > 图例中的平方形状点变化 - 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 吗?

标签: rggplot2legendpointshapes

解决方案


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

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 包于 2020-03-10 创建(v0.3.0)


推荐阅读