简体   繁体   English

如何用变量给定的半径绘制散点图?

[英]How to plot a scatter plot with a radius given by a variable?

I'm pretty new to ggplot, I need to make a scatterplot with the size proportional to a variable of the df, so that some of the points get "zero radius" because a 0 value in the variable, but when I use the size aes, the points with 0 value are mapped to a non-zero radius point.我对 ggplot 很陌生,我需要制作一个散点图,其大小与 df 的变量成正比,以便某些点获得“零半径”,因为变量中的值为 0,但是当我使用大小时aes,具有0值的点被映射到非零半径点。

how can I get the desired effect?我怎样才能得到想要的效果?

According to the help, " scale_size_area ensures that a value of 0 is mapped to a size of 0".根据帮助,“ scale_size_area确保将值 0 映射到大小为 0”。 [Also worth noting, you can get a similar effect if you use scale_radius or scale_size and set the lower limit of the range argument to zero]. [另外值得注意的是,如果您使用scale_radiusscale_size并将range参数的下限设置为零,您可以获得类似的效果]。 So we can do:所以我们可以这样做:

df <- data.frame(x=0:5, y=0:5, s=0:5)

ggplot(df, aes(x=x, y=y)) + 
  geom_point(aes(size=s)) +
  scale_size_area()    # NB scale_size(range = c(0, 6)) also works 

在此处输入图片说明

Note that this still shows the location of 'zero size' points using a single pixel.请注意,这仍然使用单个像素显示“零尺寸”点的位置。 If you want them to be actually invisible, you can filter out the rows of zero size like this:如果您希望它们实际上不可见,您可以像这样过滤掉零大小的行:

ggplot(df, aes(x=x, y=y, size=s)) + 
  geom_point(data = df[df$s != 0,]) +
  scale_size_area()

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

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