控制 R 散点图中点的大小?

发布于 2024-08-27 08:55:10 字数 186 浏览 3 评论 0原文

在 R 中,plot() 函数采用一个 pch 参数来控制图中点的外观。我正在制作具有数万个点的散点图,并且更喜欢一个小但不是太小的点。基本上,我发现 pch='.' 太小,但 pch=19 太胖。中间有什么东西或者有什么方法可以以某种方式缩小这些点吗?

In R, the plot() function takes a pch argument that controls the appearance of the points in the plot. I'm making scatterplots with tens of thousands of points and prefer a small, but not too small dot. Basically, I find pch='.' to be too small, but pch=19 to be too fat. Is there something in the middle or some way to scale the dots down somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一花一树开 2024-09-03 08:55:10

尝试使用 cex 参数:

?par

  • cex
    给出的数值
    绘制文本的数量和
    符号应相对放大
    为默认值。请注意,一些
    图形函数,例如
    plot.default 有一个这样的参数
    与该图形相乘的名称
    参数,以及一些函数,例如
    点接受值向量
    被回收。其他用途将
    如果向量仅取第一个值
    长度大于一的是
    提供。

Try the cex argument:

?par

  • cex
    A numerical value giving the
    amount by which plotting text and
    symbols should be magnified relative
    to the default. Note that some
    graphics functions such as
    plot.default have an argument of this
    name which multiplies this graphical
    parameter, and some functions such as
    points accept a vector of values
    which are recycled. Other uses will
    take just the first value if a vector
    of length greater than one is
    supplied.
小梨窩很甜 2024-09-03 08:55:10

pch=20 返回大小“.”之间的符号。 19.

这是一个填充符号(这可能就是您想要的)。

除此之外,甚至 R 中的基本图形系统也允许用户对符号大小、颜色和形状进行细粒度控制。例如,

dfx = data.frame(ev1=1:10, ev2=sample(10:99, 10), ev3=10:1)

with(dfx, symbols(x=ev1, y=ev2, circles=ev3, inches=1/3,
                  ann=F, bg="steelblue2", fg=NULL))

Graph示例

pch=20 returns a symbol sized between "." and 19.

It's a filled symbol (which is probably what you want).

Aside from that, even the base graphics system in R allows a user fine-grained control over symbol size, color, and shape. E.g.,

dfx = data.frame(ev1=1:10, ev2=sample(10:99, 10), ev3=10:1)

with(dfx, symbols(x=ev1, y=ev2, circles=ev3, inches=1/3,
                  ann=F, bg="steelblue2", fg=NULL))

Graph example

终陌 2024-09-03 08:55:10

正如 rcs 所说,cex 将在基础图形包中完成这项工作。我认为您不愿意在 ggplot2 中绘制图表,但如果您愿意,那么您可以轻松控制一个 size 美学属性 (ggplot2< /code> 具有用户友好的函数参数:在 ggplot2 中,您可以键入例如 size = 2 和您将得到 2 毫米点)。

这是例子:

### base graphics ###
plot(mpg ~ hp, data = mtcars, pch = 16, cex = .9)

### ggplot2 ###
# with qplot()
qplot(mpg, hp, data = mtcars, size = I(2))
# or with ggplot() + geom_point()
ggplot(mtcars, aes(mpg, hp), size = 2) + geom_point()
# or another solution:
ggplot(mtcars, aes(mpg, hp)) + geom_point(size = 2)

As rcs stated, cex will do the job in base graphics package. I reckon that you're not willing to do your graph in ggplot2 but if you do, there's a size aesthetic attribute, that you can easily control (ggplot2 has user-friendly function arguments: instead of typing cex (character expansion), in ggplot2 you can type e.g. size = 2 and you'll get 2mm point).

Here's the example:

### base graphics ###
plot(mpg ~ hp, data = mtcars, pch = 16, cex = .9)

### ggplot2 ###
# with qplot()
qplot(mpg, hp, data = mtcars, size = I(2))
# or with ggplot() + geom_point()
ggplot(mtcars, aes(mpg, hp), size = 2) + geom_point()
# or another solution:
ggplot(mtcars, aes(mpg, hp)) + geom_point(size = 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文