简体   繁体   English

R:根据它们的权重改变一些(但不是全部)绘制数据点的大小

[英]R: changing the size of some (but not all) plotted data points according to their weighting

I have generated a plot in R in which the size of each data point corresponds to its individual weighting, for instance like this: 我在R中生成了一个图,其中每个数据点的大小对应于其各自的权重,例如:

x <- runif(10, 2, 200) 
y <- runif(10, 5.0, 7.5)
weighting <- c(1, 1, 1, 1, 1, 10, 15, 15, 25, 25)

I have adjusted the size of the plotted data ponts with cex : 我用cex调整了绘制数据cex的大小:

plot(x, y, cex = weighting)

Since some data points in the plot are very large because of their high weighting factors, I have reduced the size of all points by plot(x, y, cex = weighting/5) which would give a plot like: 由于绘图中的一些数据点因其高权重因素而非常大,我通过plot(x, y, cex = weighting/5)减小了所有点的大小,这将给出如下图: 例1

Unfortunately, data points with a small weighting are tiny now. 不幸的是,加权小的数据点现在很小。 I'm sure there is a possibility to limit the sizing only to those points which have a high weighting factor and to plot the others ( ie weighting = 1 ) at normal size. 我确信有可能只将尺寸限制在那些具有高加权因子的点上,并将其他点( 即加权= 1 )绘制成正常尺寸。 I don't know how to do that, can anybody help? 我不知道怎么做,有人可以帮忙吗?

You may also have a look at scale_size_area in ggplot 您还可以查看scale_size_area中的ggplot

# you need to keep your data in a data.frame
df <- data.frame(x = x, y = y, weighting = weighting)
ggplot(data = df, aes(x = x, y = y, size = weighting)) +
  geom_point() +
  scale_size_area()

Update, on cex and scaling of point size 更新,关于cex和缩放点大小
Because the topic of the question is cex , I take the opportunity to cite a post by @Bert Gunter on R-help: 因为问题的主题是cex ,我借此机会引用@Bert Gunter关于R-help 的帖子

"Here's the problem: in order to accurately represent the value, the "point" = circle area must be proportional to the value. That is, the eye "sees" the areas, not the radii, as the point "size." A delightful reference on this is Howard Wainer's 1982 or so (can't remember exactly) article in THE AMERICAN STATISTICIAN, "How to Graph Data Badly" (or maybe "Plot" Data). “这就是问题所在:为了准确地表示值,”点“=圆形区域必须与该值成比例。也就是说,眼睛”看到“区域而不是半径作为”大小“点。关于这一点的令人愉快的参考是霍华德韦纳1982年左右(不记得确切)美国统计局的文章,“如何绘制数据不好”(或者可能是“绘图”数据)。

Anyway, using cex, I have no idea whether a point drawn with cex = 1.23 is 1.23 times the area or radius -- or neither -- of a point drawn with cex =1. 无论如何,使用cex,我不知道用cex = 1.23绘制的点是否是用cex = 1绘制的点的面积或半径的1.23倍 - 或者两者都不是。 Indeed, it might vary depending on the implementation/OS/graphics fonts. 实际上,它可能会因实现/ OS /图形字体而异。 So it seems better to me to "draw" the point with symbols(), where you can have complete control over the size. 所以对我来说,用符号()来“绘制”这一点似乎更好,你可以完全控制它的大小。

Obviously, let me know if I'm wrong about this." End quotation. 显然,如果我错了,请告诉我。“结束报价。

In the same thread @Gabor Grothendieck points to this nice article , where the base function symbols is used. 在同一个帖子中,@ Gabor Grothendieck指出了这篇很好的文章 ,其中使用了base函数symbols One example where "[c]ircles [are] incorrectly sized by radius instead of area. Large values appear much bigger", and one where "Circles [are] correctly sized by area", and also where the inches argument is used to set size the largest bubble. “[c] ircles [is]按半径而不是区域大小错误的一个例子。大值显示得更大”,一个“Circles [are]正确按区域大小”,还有一个inches参数用于设置规模最大的泡沫。 I think this might be a base equivalent to scale_size_area() in ggplot . 我觉得这可能是一个base相当于scale_size_area()ggplot

如何用大小的weighting log绘图?

plot(x, y, cex = log10(weighting))

The function pmax might help: 函数pmax可能会有所帮助:

minCex <- 1
plot(x, y, cex = pmax(minCex, weighting / 5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM