ggplot2中每个方面的不同函数曲线
Different function curves for each facet in ggplot2
短:
如何在 ggplot2 的每个方面绘制不同的 user/data-defined 曲线?
长:
我想将真实数据的分面散点图与基于分面变量的用户定义的预测数据曲线叠加,即为每个分面使用不同的曲线。
这是一个玩具示例:
我们有两年来在两个地点用两种不同的速率处理被红色或白色皇后玩耍的刺猬数量的数据。我们预计这些处理会以每年 0.5 或 1.5 的指数速率改变刺猬种群。所以数据看起来像
queen <- as.factor(c(rep("red", 8), rep("white",8)))
site <- as.factor(c(rep(c(rep(1,4), rep(2,4)),2)))
year <- c(rep(c(rep(1,2), rep(2,2)),4))
rate <- rep(c(0.5,1.5),8)
hedgehogs <- c(8,10,6,14,16,9,8,11,11,9,9,10,8,11,11,6)
toy.data <- data.frame(queen, site, year, rate, hedgehogs)
使用以下内容按比率创建网站的四个不错的方面:
library("ggplot2")
ggplot(toy.data, aes(year, hedgehogs)) +
geom_point(aes(colour=queen), size=10) +
scale_colour_manual(values=c("red", "white")) +
facet_grid(rate ~ site, labeller= label_both)
我想将利率曲线叠加到这些图上。
我们的预测曲线如下:
predict.hedgehogs <- function(year, rate){
10*(rate^(year-1))
}
根据比率和年数乘以起始数量(此处给出 10 只刺猬)的指数预测的刺猬数量。
我尝试了各种方式来填充 stat_function
并在正确的轨道上产生了一些东西,但就是不在那里,
例如:
根据 geom_hline
添加方面特定数据 (see bottom page here)
facet.data <- data.frame(rate=c(0.5, 0.5, 1.5, 1.5),
site=c(1, 2, 1, 2))
然后绘图
ggplot(toy.data, aes(year, hedgehogs)) +
geom_point(aes(colour = queen), size = 10) +
scale_colour_manual(values = c("red", "white")) +
facet_grid(rate ~ site, labeller = label_both) +
stat_function(mapping = aes(x = year, y = predict.hedgehogs(year,rate)),
fun = predict.hedgehogs,
args = list(r = facet.data$rate), geom = "line")
或单独 stat_function
调用每个费率(即 this strategy):
ggplot(toy.data, aes(year, hedgehogs)) +
geom_point(aes(colour=queen), size=10) +
scale_colour_manual(values=c("red", "white")) +
facet_grid(rate ~ site, labeller= label_both) +
stat_function(fun=predict.hedgehogs, args=list(rate=0.5), geom="line", rate==0.5)+
stat_function(fun=predict.hedgehogs, args=list(rate=1.5), geom="line", rate==1.5)
Error: `mapping` must be created by `aes()`
有什么想法吗?
非常感谢@Roland
的评论
如果我们从上面的函数 predict.hedgehogs
添加到 toy.data
预测数据:
pred.hogs <- predict.hedgehogs(year, rate)
toy.data <- data.frame(toy.data, pred.hogs)
我们可以绘制:
ggplot(toy.data, aes(year, hedgehogs)) +
geom_point(aes(colour=queen), size=10) +
scale_colour_manual(values=c("red", "white")) +
facet_grid(rate ~ site) +
geom_smooth(aes(x=year, y=pred.hogs), stat="identity", colour = "black")
短:
如何在 ggplot2 的每个方面绘制不同的 user/data-defined 曲线?
长:
我想将真实数据的分面散点图与基于分面变量的用户定义的预测数据曲线叠加,即为每个分面使用不同的曲线。
这是一个玩具示例:
我们有两年来在两个地点用两种不同的速率处理被红色或白色皇后玩耍的刺猬数量的数据。我们预计这些处理会以每年 0.5 或 1.5 的指数速率改变刺猬种群。所以数据看起来像
queen <- as.factor(c(rep("red", 8), rep("white",8)))
site <- as.factor(c(rep(c(rep(1,4), rep(2,4)),2)))
year <- c(rep(c(rep(1,2), rep(2,2)),4))
rate <- rep(c(0.5,1.5),8)
hedgehogs <- c(8,10,6,14,16,9,8,11,11,9,9,10,8,11,11,6)
toy.data <- data.frame(queen, site, year, rate, hedgehogs)
使用以下内容按比率创建网站的四个不错的方面:
library("ggplot2")
ggplot(toy.data, aes(year, hedgehogs)) +
geom_point(aes(colour=queen), size=10) +
scale_colour_manual(values=c("red", "white")) +
facet_grid(rate ~ site, labeller= label_both)
我想将利率曲线叠加到这些图上。
我们的预测曲线如下:
predict.hedgehogs <- function(year, rate){
10*(rate^(year-1))
}
根据比率和年数乘以起始数量(此处给出 10 只刺猬)的指数预测的刺猬数量。
我尝试了各种方式来填充 stat_function
并在正确的轨道上产生了一些东西,但就是不在那里,
例如:
根据 geom_hline
添加方面特定数据 (see bottom page here)
facet.data <- data.frame(rate=c(0.5, 0.5, 1.5, 1.5),
site=c(1, 2, 1, 2))
然后绘图
ggplot(toy.data, aes(year, hedgehogs)) +
geom_point(aes(colour = queen), size = 10) +
scale_colour_manual(values = c("red", "white")) +
facet_grid(rate ~ site, labeller = label_both) +
stat_function(mapping = aes(x = year, y = predict.hedgehogs(year,rate)),
fun = predict.hedgehogs,
args = list(r = facet.data$rate), geom = "line")
或单独 stat_function
调用每个费率(即 this strategy):
ggplot(toy.data, aes(year, hedgehogs)) +
geom_point(aes(colour=queen), size=10) +
scale_colour_manual(values=c("red", "white")) +
facet_grid(rate ~ site, labeller= label_both) +
stat_function(fun=predict.hedgehogs, args=list(rate=0.5), geom="line", rate==0.5)+
stat_function(fun=predict.hedgehogs, args=list(rate=1.5), geom="line", rate==1.5)
Error: `mapping` must be created by `aes()`
有什么想法吗?
非常感谢@Roland
的评论如果我们从上面的函数 predict.hedgehogs
添加到 toy.data
预测数据:
pred.hogs <- predict.hedgehogs(year, rate)
toy.data <- data.frame(toy.data, pred.hogs)
我们可以绘制:
ggplot(toy.data, aes(year, hedgehogs)) +
geom_point(aes(colour=queen), size=10) +
scale_colour_manual(values=c("red", "white")) +
facet_grid(rate ~ site) +
geom_smooth(aes(x=year, y=pred.hogs), stat="identity", colour = "black")