ggplot-geom_smooth趋势线/置信带没有延伸到图形的结尾

时间:2018-11-29 00:45:57

标签: r ggplot2 statistics scatter-plot

尝试使用ggplot2创建具有趋势线和置信带的散点图,但趋势线在图形边缘之前停止。有人对如何将线延伸到图形的边缘有建议吗?我尝试使用fullrange=TRUEcoord_cartesian,但似乎无法使其正常工作。

g2 <- ggplot(data = CT_Data, aes(x = Income, y = -(ACC_Right), color = Group))+
  geom_jitter()+
  geom_smooth(data = CT_Data, aes(x = Income, y = -(ACC_Right)), method=lm, inherit.aes = FALSE, color = "grey3")+
  ggtitle("Income and Cortical Thinning in the Right ACC")+
  ylab("Cortical Thinning over 5 years (mm)") +
  xlab("Income Level")+
  theme_bw()+
  theme(axis.line = element_line(size = 1, colour = "black"),
        panel.grid.major = element_line(colour = "#d3d3d3"),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(), panel.background = element_blank(),
        text = element_text(family = "Times"),
        plot.title = element_text(hjust = 0.5),
        axis.text.x = element_text(colour = "black", size = 10),
        axis.text.y = element_text(colour = "black", size = 10)) +
        scale_fill_brewer(palette = "Set1")
g2



structure(list(SubjectID = c("CONTROL01-JG", "CONTROL03-RG", 
"CONTROL08-JC", "CONTROL06-AV", "CONTROL12-AP", "CONTROL07-JS", 
"CONTROL17-IB", "CONTROL19-LE", "CONTROL101-KP", "CONTROL102-RP", 
"CONTROL109-RA", "CONTROL107-AH", "CONTROL112-KC", "CONTROL113-AV", 
"CONTROL116-IS", "MUSIC02-SS", "MUSIC09-AS", "MUSIC12-OM", "MUSIC04-MA", 
"MUSIC03-GG", "MUSIC05-RM", "MUSIC01-AP", "MUSIC10-MV", "MUSIC15-NR", 
"MUSIC102-ET", "MUSIC109-RS", "MUSIC107-LL", "MUSIC108-AR", "MUSIC113-RH", 
"SPORT01-AM", "SPORT05-JG", "SPORT04-DM", "SPORT07-GP", "SPORT08-FP", 
"SPORT14-BM", "SPORT28-JZ", "SPORT103-AS", "SPORT104-NC", "SPORT114-KC", 
"SPORT111-DW", "SPORT108-IM", "SPORT105-ED", "SPORT109-CO", "SPORT107-IA", 
"SPORT117-EC"), Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Control", "Music", "Sport"
), class = "factor"), ACC_Right = c(-0.453269, -0.480981, -0.290443, 
-0.540288, -0.891965, -0.190356, -0.74666, -0.11268, -0.810887, 
-0.450247, -0.969715, 0.21439, -0.238609, -0.494756, -0.561778, 
-0.10717, -0.769544, -0.355883, -0.03262, -0.492434, -0.159326, 
-0.067637, -0.622628, -0.814374, -0.572156, -0.023438, -1.257273, 
-0.453762, -0.359776, -0.830652, -0.549457, -0.197343, -0.486254, 
0.033923, -0.07222, -0.806687, -0.447038, -1.231295, -0.513544, 
-0.649501, -0.425264, -0.41097, -0.206202, -0.903076, -0.734864
), Income = c(3, 3, 3, 2, 2, 6, 1, 2, 1, 1, 3, 3, 2, 2, 3, 2, 
3, 4, 5, 2, 4, 5, 2, 2, 2, 3, 2, 3, 2, 2, 3, 3, 2, 2, 1, 3, 3, 
1, 4, 2, 1, 4, 4, 2, 3)), .Names = c("SubjectID", "Group", "ACC_Right", 
"Income"), row.names = c(NA, -45L), class = c("tbl_df", "tbl", 
"data.frame"))

enter image description here

0 个答案:

没有答案