R 如何保持图形中点的相对大小相等?

R 如何保持图形中点的相对大小相等?,r,plot,graph,weighted,R,Plot,Graph,Weighted,我想做一个图表,其中圆圈的大小表示样本的大小。如果我在p1()中使用plot,它可以正常工作 但是如果我尝试将不同类型的点着色,那么相对大小是错误的 如何使红色和绿色圆圈的大小相同 p1<-function() { plot(t$x,t$y,cex=100*t$size,xlim=c(0,1),ylim=c(0.,1.)) } p2<-function() { plot(t$x[t$r=="0"],t$y[t$r=="0"],xlim=c(0,1),ylim=

我想做一个图表,其中圆圈的大小表示样本的大小。如果我在p1()中使用plot,它可以正常工作

但是如果我尝试将不同类型的点着色,那么相对大小是错误的

如何使红色和绿色圆圈的大小相同

p1<-function() {
    plot(t$x,t$y,cex=100*t$size,xlim=c(0,1),ylim=c(0.,1.))    
}
p2<-function() {
    plot(t$x[t$r=="0"],t$y[t$r=="0"],xlim=c(0,1),ylim=c(0.,1.),cex=100*t$size,col="red")
    points(t$x[t$r=="1"],t$y[t$r=="1"],xlim=c(0,1),ylim=c(0.,1.),cex=100*t$size,col="green")
}
l<-20
x<-seq(0,1,1/l)
y<-sqrt(x)
r=round(runif(n=length(x),min=0,max=.8))
n<-1:length(x)
size=n/sum(n)
t<-data.frame(x,y,r,n,size)
t$r<-factor(r)
str(t)
p1()

p1您必须稍微更改功能
p2
。当您应该按系数
t$r
进行子集设置时,您正在使用
t$size
,因为您在绘制点时正在这样做

如果绘制
t$x[t$r==“0”]
t$y[t$r==“0”]
的对比图,则必须使用与这些点对应的大小,即
t$size[t$r==“0”]
。或者,您可以先将数据帧
t
子集,然后使用这两个结果数据帧来绘制点。请参见末尾的功能
p2\u alt

p2 <- function() {
  plot(t$x[t$r == "0"], t$y[t$r == "0"],
       xlim = c(0, 1), ylim = c(0., 1.),
       cex = 100*t$size[t$r == "0"],
       col = "red",
       xlab = "x", ylab = "y")
  points(t$x[t$r == "1"],
         t$y[t$r == "1"],
         xlim = c(0, 1), ylim = c(0., 1.),
         cex = 100*t$size[t$r == "1"],
         col = "green")
}

set.seed(651)    # make the results reproducible

l <- 20
x <- seq(0, 1, 1/l)
y <- sqrt(x)
r <- round(runif(n = length(x), min = 0, max = 0.8))
n <- 1:length(x)
size <- n/sum(n)
t <- data.frame(x, y, r, n, size)
t$r <- factor(r)
#str(t)
#p1()
p2()

p2
cex=100*t$size[t$r=='0']
cex=100*t$size[t$r=='1']
是的,向量的长度将不同。这个看起来不错。这似乎有悖常理:(@RayTayek,等等,我将用解释和图表进行编辑。@RayTayek完成,请参阅新版本。
p2_alt <- function() {
  df1 <- subset(t, r == "0")
  df2 <- subset(t, r == "1")
  plot(df1$x, df1$y,
       xlim = c(0, 1), ylim = c(0., 1.),
       cex = 100*df1$size,
       col = "red",
       xlab = "x", ylab = "y")
  points(df2$x,
         df2$y,
         xlim = c(0, 1), ylim = c(0., 1.),
         cex = 100*df2$size,
         col = "green")
}

p2_alt()