使用R基图形的面_包裹样绘图
我想比较两个具有相同x和y变量的数据集。但是,并非所有X变量点都存在于这两个点上。举一个玩具的例子,说这就是我所拥有的:使用R基图形的面_包裹样绘图,r,plot,R,Plot,我想比较两个具有相同x和y变量的数据集。但是,并非所有X变量点都存在于这两个点上。举一个玩具的例子,说这就是我所拥有的: position.x <- c(1,2,3) score.x <- c(450,220,330) x <- data.frame(position,score.x) position.y <- c(2,3,5) score.y <- c(333,423,988) y<- data.frame(position.y,score.y)
position.x <- c(1,2,3)
score.x <- c(450,220,330)
x <- data.frame(position,score.x)
position.y <- c(2,3,5)
score.y <- c(333,423,988)
y<- data.frame(position.y,score.y)
par(mfrow = c(2,1))
plot(x, pch = 19)
plot(y, pch = 19)
position.x您可以通过xlim
和slim
指定x轴和y轴的范围
position.x <- c(1,2,3)
score.x <- c(450,220,330)
x <- data.frame(position,score.x)
position.y <- c(2,3,5)
score.y <- c(333,423,988)
y<- data.frame(position.y,score.y)
par(mfrow = c(2,1))
plot(x, pch = 19, xlim=c(1,5))
plot(y, pch = 19, xlim=c(1,5))
position.x如果要重复此操作,不妨编写某种函数(这是ggplot的优点之一,它可以为您完成所有设置):
看不到代码,但我明白你的意思!成功了。我猜你的意思是ylim而不是slim?复制和粘贴失败…再次检查。是的,太好了!什么是超薄的?
## data needs to be in a long format
dat <- data.frame(position = c(1,2,3,2,3,5),
score = c(450,220,330,333,423,988),
z = c('x','x','x','y','y','y'))
facet_wrap <- function(data, x, y, z, horiz = TRUE, ...) {
## save current par settings and return after finished
op <- par(no.readonly = TRUE)
on.exit(par(op))
zz <- unique(data[, z])
## sets up the layout to cascade horizontally or vertically
## and sets xlim and ylim appropriately
if (horiz) {
par(mfrow = c(1, length(zz)), ...)
ylim <- range(data[, y])
xlim <- NULL
} else {
par(mfrow = c(length(zz), 1), ...)
xlim <- range(data[, x])
ylim <- NULL
}
## make a subset of data for each unique by variable
## and draw a basic plot for each one
for (ii in zz) {
tmp <- data[data[, z] %in% ii, ]
plot(tmp[, x], tmp[, y], xlim = xlim, ylim = ylim)
}
}
facet_wrap(dat, 'position', 'score', 'z', mar = c(5,4,2,2))
facet_wrap(dat, 'position', 'score', 'z', mar = c(5,4,1,2), horiz = FALSE)