【问题标题】:Transform the shape and colors of legend in scatterplot in base R在基础R的散点图中转换图例的形状和颜色
【发布时间】:2021-11-25 00:25:21
【问题描述】:

我正在尝试在基础 R 中制作此散点图: https://i.stack.imgur.com/qdgGN.png

此散点图基于 mtcars 数据。

现在我尝试了这个,但是我无法获得相同的图例。 如何将正方形变成点? 如何获得完全相同的颜色? 我怎么知道图例是哪种格式?

这是我的代码:

cols = c("deepskyblue", "magenta", "gray50")
plot(mpg ~ wt, xlab="Weight (1000lbs)", ylab="Miles / gallon", col = cyl, pch = 19, data=mtcars)
legend(x = "topright",
       title = "Number of cylinders",
       horiz = TRUE,
       legend = c("4", "6", "8"), 
       bty = 'n',
       cex = 0.7,
       fill = cols)

顺便说一句:我现在制作了 cols,但我认为还有另一种方法可以解决它。

提前致谢!

【问题讨论】:

    标签: r plot colors legend


    【解决方案1】:

    “填充”是指用颜色填充的方块。
    您需要改为使用“pch”参数,这样您就可以添加“col”参数来选择您的点颜色。
    有关信息,pch 对应于您要在图例中选择的形状。
    https://www.r-bloggers.com/2021/06/r-plot-pch-symbols-different-point-shapes-in-r/

    替换

    fill=cols

    通过

    col = cols,
    pch=19)
    

    完整代码:

    cols = c("deepskyblue", "magenta", "gray50")
    plot(mpg ~ wt, xlab="Weight (1000lbs)", ylab="Miles / gallon", col = cyl, pch = 19, data=mtcars)
    legend(x = "topright",
           title = "Number of cylinders",
           horiz = TRUE,
           legend = c("4", "6", "8"), 
           bty = 'n',
           cex = 0.7,
           col = cols,
           pch=19)
    

    如果您想使用不同的颜色,那么您可以选择以下颜色。 http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2016-05-04
      相关资源
      最近更新 更多