简体   繁体   English

更改R中点的大小

[英]Changing size of points in R

I would like to make a scatter plot using R base graphics, though I can use ggplot if it is necesary. 我想使用R基本图形制作散点图,但是如果需要可以使用ggplot。 I have a data frame containing the x and y coordinates, and also two other covariates, call them a and b. 我有一个包含x和y坐标的数据框,还有另外两个协变量,分别称为a和b。 I would like the size of each point to vary with a (high values of a -> larger points) and the brightness/solidity of each point to vary with b (so that those points with low enough b are practically invisiable). 我希望每个点的大小随a(a的高值->较大的点)而变化,并且每个点的亮度/坚固度随b的变化而变化(以便实际上看不到那些具有足够低的b的点)。 Does anyone have any advice on how to do this? 有人对此有任何建议吗? The documentation for R graphics seems comprehensive, but so general and high-level that I scarcely know where to begin. R图形的文档似乎很全面,但是如此通用和高级,我几乎不知道从哪里开始。 Thanks for your help 谢谢你的帮助

下面的示例行显示了如何在绘图中同时使用大小和alpha

plot(1:6, sample(6), cex = sample(6)/2, col = rgb(0,0,0, sample(6)/6), pch = 19)

The cex parameter is a quick and dirty way of changing the size of the plotting characters, but you do need to work quite a bit if you want to have an informative scale etc. symbols() is one way to do this more accurately with base graphics. cex参数是一种更改绘图字符大小的快速而肮脏的方法,但是如果您想要一个信息量大的比例尺,您确实需要做很多工作cex symbols()是使用base更精确地执行此操作的一种方法图形。

Using some dummy data, here is one plot using cex and one using symbols() : 使用一些虚拟数据,这是一个使用cex和一个使用symbols()绘图:

## dummy data first
set.seed(123)
dat <- data.frame(X = rnorm(10), Y = rnorm(10,2,2), a = runif(10), b = runif(10))

## plot using cex
plot(Y ~ X, data = dat, cex = 10 * a)

## plot using symbols()
with(dat, symbols(X, Y, circles = a))

You can colour the points using the col parameter in the cex example or using the fg argument in the symbols case. 您可以在cex示例中使用col参数或在符号情况下使用fg参数为点着色。 The trick will be get an appropriate scale. 诀窍将是获得适当的规模。 In our dummy data, b is on the interval (0,1], so our scale might be to break this into 5 categories and use one of five plotting colours. 在我们的虚拟数据中, b处于(0,1]区间,因此我们的规模可能是将此划分为5类并使用五种绘图颜色之一。

## Eg using heat.colors(5)
cut.pts <- seq(0, 1, by = 0.1)
cuts <- with(dat, cut(b, cut.pts))
with(dat, symbols(X, Y, circles = a, bg = heat.colors(5)[cuts]))

As you see, it is important to choose appropriate colours though as here, one colour is the same as the background. 如您所见,选择合适的颜色很重要,尽管在这里,一种颜色与背景相同。

Doing this with ggplot2 is likely going to be much simpler as all the heavy lifting has already been done for you by Hadley Wickham. 使用ggplot2ggplot2可能会更简单,因为Hadley Wickham已经为您完成了所有繁重的工作。

## Eg using ggplot
require(ggplot2)
p <- ggplot(dat, aes(X, Y, colour = b, size = a))
p + geom_point()

HTH 高温超导

This comes up often in R-help such as here . 这在R-help中经常出现,例如这里 Basically, cex will be the argument of interest for changing the size of the points. 基本上, cex将是更改点数大小时感兴趣的参数。 For changing color of the points I would suggest something like setting col=heat.colors() or making your own color list and define as a factor before plotting. 对于改变点的颜色,我建议设置col=heat.colors()或制作自己的颜色列表并在绘制之前将其定义为一个因素。

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

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