R 限制geom_线的x轴范围(由斜率和截距定义) 库(ggplot2) ## df
我认为这是一种获得你想要的东西的方法:R 限制geom_线的x轴范围(由斜率和截距定义) 库(ggplot2) ## df,r,ggplot2,R,Ggplot2,我认为这是一种获得你想要的东西的方法: library(ggplot2) ## df <- as.data.frame(matrix(rnorm(60*2, mean=3,sd=1), 60, 2)) colnames(df) <- c("A", "B") cf1 <- coef(lm(B~A, data=df)) ## ggplot(df, aes(A,B)) + geom_point() + stat_smooth(method = "lm"
library(ggplot2)
##
df <- as.data.frame(matrix(rnorm(60*2, mean=3,sd=1), 60, 2))
colnames(df) <- c("A", "B")
cf1 <- coef(lm(B~A, data=df))
##
ggplot(df, aes(A,B)) +
geom_point() +
stat_smooth(method = "lm", color="red", fill="red", alpha=0.1, fullrange=TRUE) +
#xlim(0,6)+
geom_abline(intercept = cf1[1], slope = cf1[2], lty="dashed", col="green")
min\x缩放怎么样<代码>+coord_笛卡尔(xlim=c(1,5))
?是的,这是一个很好的启发
min_x <- min(df$A)
min_y <- unname(cf1[1])
max_x <- max(df$A)
max_y <- min_y + unname(cf1[2]) * max_x
##
p <- ggplot(df, aes(A,B)) +
geom_point() +
stat_smooth(
method = "lm", color = "red",
fill = "red", alpha = 0.1,
fullrange = TRUE)
##
R> p + geom_segment(
aes(x = min_x, y = min_y,
xend = max_x, yend = max_y),
linetype = "dashed",
color = "green")