如何保持图中点的相对大小相等?
How to keep equal relative sizes of points in a graph?
我想制作一个图表,其中圆圈的大小表示样本的大小。如果我在 p1() 中使用绘图,它工作正常。
但是如果我尝试对不同类型的点进行着色,则相对大小是错误的。
如何使红色和绿色圆圈大小相同?
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()
您必须稍微更改一下功能 p2
。你正在使用 t$size
,所有这些,当你应该用因子 t$r
进行子集化时,因为你在绘制点时这样做了。
如果绘制 t$x[t$r == "0"]
与 t$y[t$r == "0"]
,则必须使用与这些点对应的大小,即 t$size[t$r == "0"]
。或者,您可以先对数据框 t
进行子集化,然后使用这两个生成的数据框来绘制点。见最后的功能p2_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_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()
图形完全相同,但代码可能更具可读性。
最后,请注意我在 p2()
和 p2_alt()
中添加了参数 xlab
和 ylab
。
我想制作一个图表,其中圆圈的大小表示样本的大小。如果我在 p1() 中使用绘图,它工作正常。
但是如果我尝试对不同类型的点进行着色,则相对大小是错误的。
如何使红色和绿色圆圈大小相同?
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()
您必须稍微更改一下功能 p2
。你正在使用 t$size
,所有这些,当你应该用因子 t$r
进行子集化时,因为你在绘制点时这样做了。
如果绘制 t$x[t$r == "0"]
与 t$y[t$r == "0"]
,则必须使用与这些点对应的大小,即 t$size[t$r == "0"]
。或者,您可以先对数据框 t
进行子集化,然后使用这两个生成的数据框来绘制点。见最后的功能p2_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_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()
图形完全相同,但代码可能更具可读性。
最后,请注意我在 p2()
和 p2_alt()
中添加了参数 xlab
和 ylab
。