【问题标题】:ggplot scatterplot points with no fillggplot散点图没有填充
【发布时间】:2013-03-31 06:12:50
【问题描述】:

我想制作一个散点图,其点没有填充(或等效地,使用透明填充)。

# generate some random data for the scatterplot
n <- 5
f <- factor(1:n)
df <- expand.grid(f1 = f, f2 = f)
df <- transform(df, v1 = round(10 * runif(n ** 2)))

# plot the scatterplot
ggplot(df) + geom_point(aes(x = f1, y = f2, size = v1, fill = NA))

fill 设置为NA 似乎合乎逻辑,但不起作用。我也试过NULL"" 无济于事。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我认为你想玩形状,但可能是错的:

    ggplot(df) + geom_point(aes(x = f1, y = f2, size = v1), shape=1)
    

    或许……

    ggplot(df) + geom_point(aes(x = f1, y = f2, size = v1), fill="green", shape=21)
    

    如果你想填充颜色。

    【讨论】:

    • 谢谢!我阅读了 ggplot2 源代码; fill = NA 是删除填充的正确方法。但是,由于默认形状是实心圆盘,因此无法看到填充(或缺少填充)。
    • 是的。我只是分享,以供将来参考,fill = NA 在语法上没有任何问题;它确实删除了任何填充颜色。尽管如此,它对shape = 19 默认值(实心圆盘)没有影响,因为“填充”已经是形状本身的一部分。术语令人困惑,但这不是你的错!
    【解决方案2】:

    在更高版本的 ggplot (ggplot2_3.0.0) 中,您可以使用任何形状执行以下操作:

        ggplot(df) +
          geom_point(aes(x = f1, y = f2, size = v1, shape = v1)) +
          scale_shape(solid = FALSE)
    

    【讨论】:

    • 不错!我不知道。
    • 这似乎有点不可靠,因为它似乎只有在您设置geomshape 变量时才有效,而不是如果您想对所有带有空白的点使用相同的形状geom_point。另一方面,如果您有兴趣对数据中的组使用不同的形状,这似乎是唯一可行的解​​决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多