R 如何使用ggplot2在双因素面的特定绘图上添加不同的垂直线?
这是我使用mtcars绘制图形的代码R 如何使用ggplot2在双因素面的特定绘图上添加不同的垂直线?,r,ggplot2,R,Ggplot2,这是我使用mtcars绘制图形的代码 p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(vs ~ am) p我找到的最简单的方法是在单独的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.d
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
facet_wrap(vs ~ am)
p我找到的最简单的方法是在单独的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)
line.df我找到的最简单的方法是在单独的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)
line.df答案在帮助文件?geom_hline()
例如:
# 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)
#要在不同方面显示不同的线条,请使用美学
p答案在帮助文件?geom_hline()
例如:
# 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)
#要在不同方面显示不同的线条,请使用美学
p