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()
图形是完全相同的,但可能代码更可读。
xlab
ylab
对双方
p2()
p2_alt()
.