Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 限制geom_线的x轴范围(由斜率和截距定义) 库(ggplot2) ## df_R_Ggplot2 - Fatal编程技术网

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")