如何在ggplot2中将点大小调整为图的比例尺? [英] How to adjust the point size to the scale of the plot in ggplot2?

查看:2207
本文介绍了如何在ggplot2中将点大小调整为图的比例尺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们生成一些数据:

pre $ code> x < - -10 * cos(seq(0,pi,length.out = 100))+ 1
y< - 10 * seq(0,pi,length.out = 100)
xerr < - rep(2,100)
yerr< - rep (2,100)
dd< - as.data.frame(cbind(x,y,xerr,yerr))

在这里,我有 x y 有点错误的坐标, xerr yerr (为了方便起见,我将它们设置为常量)。我想用点的大小来表示这些错误。这很容易实现:

  ggplot()+ 
geom_point(data = dd,aes(x,y,size = sqrt(xerr ^ 2 + yerr ^ 2)),color =gray)+
geom_path(data = dd,aes(x,y),color =red,size = .5)+
scale_size_identity()+
theme_bw()



然而,这些点的大小是按比例定义的这与情节的规模没有任何关系。有没有办法根据图的比例调整点的尺寸?在上面的例子中,每个点的半径的大小应该等于2.828,并且不小于现在的一个。

解决方案

一种方法是明确地绘制椭圆,其轴由错误大小定义。

  x < -  -10 * (0,pi,length.out = 10)
xerr < - runif(10, 1,5)
yerr< - runif(10,1,5)
dd< - as.data.frame(cbind(x,y,xerr,yerr))
dd $ frame< - factor(seq(1:10))

为此我们定义函数生成椭圆:

  ellipseFun < -  function(center = c(0,0),axes = c(1,1 ),npoints = 101){
tt < - seq(0,2 * pi,length.out = npoints)
xx < - center [1] + axes [1] * cos(tt )
yy< - center [2] + axes [2] * sin(tt)
return(data.frame(x = xx,y = yy))
}

然后生成所有椭圆的矩阵:

  ddEll<  -  data.frame()
(k为水平(dd $ frame)){
ddEll <-rbind(ddEll,cbind(as.data.frame(with(dd [dd $ frame == k,],ellipseFun(center = c(x,y),axes = c(xerr,yerr) ,npoints = 101))),frame = k))
}

,我们可以绘制它们:

  library(ggplot2)
ggplot()+
geom_point(data = dd,aes(x,y))+
geom_polygon(data = ddEll,aes(x = x,y = y,group = frame),color =gray,fill =red,alpha =。 2)+
scale_size_identity()+
theme_bw()+
xlim(c(-20,20))+
ylim(c(-5,35))+
coord_fixed()


Let's generate some data:

x <- -10*cos(seq(0, pi, length.out = 100))+1
y <- 10*seq(0, pi, length.out = 100)
xerr <- rep(2, 100)
yerr <- rep(2, 100)
dd <- as.data.frame(cbind(x, y, xerr, yerr))

Here I have x and y coordinates of some points with their errors, xerr and yerr (for convenience I have set them constant). I would like to represent these errors with the size of the points. This is easily doable:

ggplot() + 
  geom_point(data = dd, aes(x, y, size = sqrt(xerr^2 + yerr^2)), colour = "gray") +
  geom_path(data = dd, aes(x, y), colour = "red", size = .5) +
  scale_size_identity() +
  theme_bw()

However, the size of these points is defined on a scale that doesn't have any relation with the scale of the plot. Is there a way to adjust the dimension of the points in relation to the scale of the plot? In the above example, the radius of each point should have size equal to 2.828 and not less than one as it is now.

解决方案

One way is to explicitly draw ellipses with the axes defined by the size of the errors.

x <- -10*cos(seq(0, pi, length.out = 10))+1
y <- 10*seq(0, pi, length.out = 10)
xerr <- runif(10, 1, 5)
yerr <- runif(10, 1, 5)
dd <- as.data.frame(cbind(x, y, xerr, yerr))
dd$frame <- factor(seq(1:10))

For this purpose we define our function to generate ellipses:

ellipseFun <- function(center = c(0, 0), axes = c(1, 1), npoints = 101){
  tt <- seq(0,2*pi, length.out = npoints)
  xx <- center[1] + axes[1] * cos(tt)
  yy <- center[2] + axes[2] * sin(tt)
  return(data.frame(x = xx, y = yy))
}

We then generate the matrices for all ellipses:

ddEll <- data.frame()
for(k in levels(dd$frame)){
  ddEll <- rbind(ddEll, cbind(as.data.frame(with(dd[dd$frame == k,], ellipseFun(center = c(x, y), axes = c(xerr, yerr), npoints = 101))),frame = k))
}

And, finally, we can plot them:

library(ggplot2)
ggplot() + 
  geom_point(data = dd, aes(x, y)) +
  geom_polygon(data=ddEll, aes(x = x, y = y, group = frame), colour = "gray", fill = "red", alpha = .2) +
  scale_size_identity() +
  theme_bw() +
  xlim(c(-20, 20)) + 
  ylim(c(-5, 35)) +
  coord_fixed()

这篇关于如何在ggplot2中将点大小调整为图的比例尺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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