R 如何在图形边框的边缘打上记号?(ggplot)

R 如何在图形边框的边缘打上记号?(ggplot),r,ggplot2,graph,graphing,R,Ggplot2,Graph,Graphing,我希望我的记号/网格线位于绘图边界的最边缘 我想要的是: 它看起来像什么: 用于创建上述内容的代码: data <- data.frame( year = seq(1998,2018), soc = runif(21,1,90) ) ggplot(data, aes(x = year, y=soc)) + geom_line(data=data) + xlim(c(1995,2020)) + theme_bw() + theme(panel.grid.majo

我希望我的记号/网格线位于绘图边界的最边缘

我想要的是:

它看起来像什么:

用于创建上述内容的代码:

data <- data.frame(
  year = seq(1998,2018),
  soc = runif(21,1,90)
)

ggplot(data, aes(x = year, y=soc)) +
  geom_line(data=data) +
  xlim(c(1995,2020)) +
  theme_bw() +
  theme(panel.grid.major = element_line(colour="gray", size=0.5))

data您可以通过在
coord\u cartesian
中设置
expand=FALSE
来删除额外的绘图区域。 然后我修改了绘图边距,这样最右边的数字就不会被截断

ggplot(data, aes(x = year, y=soc)) +
  geom_line(data=data) +
  scale_x_continuous(limits = c(1995,2020)) +
  theme_bw() +
  theme(panel.grid.major = element_line(colour="gray", size=0.5)) +
  coord_cartesian(expand = F)+
  theme(plot.margin = margin(6,10,6,6))