# To show different lines in different facets, use aesthetics
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
facet_wrap(~ cyl)
mean_wt <- data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00))
p + geom_hline(aes(yintercept = wt), mean_wt)
所以对于你的例子,这可能是:
# create new dataframe
intercept <- data.frame(vs=c(rep(0, 2), rep(1, 2), rep(0,3), rep(1,3)),
am = c(rep(0, 4), rep(1, 6)),
int = c(10, 20, 10, 20, 15, 25, 35, 15, 25, 35))
# add vline to plot
p + geom_vline(aes(xintercept=int), intercept)
2
我发现最简单的方法是在单独的data.frame中定义行:
line.df <- data.frame( am = rep(levels(as.factor(mtcars$am)),3) ,am.int = c(10,15,20,25,NA,35) )
line.df <- line.df[!is.na(line.df$am.int),]
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(vs ~ am)
p + geom_vline(aes(xintercept = am.int), data = line.df)
2 回答
答案在帮助文件
?geom_hline()
中给出那里的例子是:
所以对于你的例子,这可能是:
我发现最简单的方法是在单独的data.frame中定义行: