简体   繁体   中英

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:

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 :

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: 例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. I don't know how to do that, can anybody help?

You may also have a look at scale_size_area in 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
Because the topic of the question is cex , I take the opportunity to cite a post by @Bert Gunter on 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).

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. Indeed, it might vary depending on the implementation/OS/graphics fonts. 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. 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. I think this might be a base equivalent to scale_size_area() in ggplot .

如何用大小的weighting log绘图?

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

The function pmax might help:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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