增加 R 图中点之间的距离 [英] Increasing distance between points in an R plot

查看:58
本文介绍了增加 R 图中点之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码生成一个图,其中一个点的大小与具有相同 x 和 y 值的案例数成正比.如何增加点之间的空间,使它们不会相互交叉或相交?虽然下面的例子使用了假数据,但真实数据包含大约 200 个高度交叉的点,因此很难感知每个单独的点.

The code below produces a plot in which the size of a point is proportional to the number of cases that have the same values of x and y. How can I increase the space between the points so that they do not cross or intersect each other? While the example below uses fake data, the real data comprehends about 200 points that are highly intersected, thus making it difficult to perceive each individual point.

谢谢,

索菲亚

    x = seq(1:10)
    y = c(4,3.8,3.8,3.2,3.1,2.5,2,1.5,1.2,1.3)
    size = c(7,20,2,70,100,70,5,80,110,2)
    pdf("example.pdf")
    par(mar=c(5,5,1.8,1.8))
    plot(y ~ x, cex = sqrt(size), pch = 1,
         ylab="y",
         xlab = "x",ylim=c(0,4),
         cex.lab =1.8,cex.axis =1.3,
         lwd = .5,type= "p", col="grey60")
    dev.off()

推荐答案

为此我会使用 ggplot2:

df = data.frame(x = seq(1:10), 
                y = c(4,3.8,3.8,3.2,3.1,2.5,2,1.5,1.2,1.3),
                size = c(7,20,2,70,100,70,5,80,110,2))

library(ggplot2)
ggplot(df, aes(x = x, y = y, size = size)) + geom_point()

ggplot2 根据size 中的值缩放地图区域,它不直接使用size 作为cex.因此不存在重叠的问题,也不需要手动调整cex.

ggplot2 scales the area of the maps according to the values in size, it does not directly use the size as cex. Therefore, the problem of overlapping is not present, and there is no need to manually tweak the cex.

这篇关于增加 R 图中点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆