ggplot2 如何使轴线在最后一个刻度处结束

ggplot2 How to make axis line end at the last tick

我不希望轴线在原点相交,我还希望轴线在第一个刻度处开始并在最后一个刻度处结束。怎么做?

ggthemes 软件包实现了 tufte 的 rangeframe。您可以将 geom_rangeframetheme_tufte 一起使用以获得范围框。然后您可以另外添加 scalelimits 以使轴从第一个刻度开始并在最后一个刻度结束。

require(ggthemes) 
qplot(mpg, wt, data=mtcars) + geom_rangeframe() + theme_tufte()

qplot(mpg, wt, data=mtcars) + 
  geom_rangeframe(data=data.frame(x=c(10, 35), y=c(0, 6)), aes(x, y)) + 
  theme_tufte() +
  scale_x_continuous(limits = c(10, 35)) +
  scale_y_continuous(limits = c(0, 6)) 

如果您不喜欢这个主题,您可以查看 theme_tufte 以获取来源并相应地更改您自己的主题。

qplot(mpg, wt, data=mtcars) + 
  geom_rangeframe(data=data.frame(x=c(10, 35), y=c(0, 6)), aes(x, y)) + 
  theme_bw(base_size = 16) + 
  theme(legend.background = element_blank(), legend.key = element_blank(), 
        panel.background = element_blank(), panel.border = element_blank(), 
        strip.background = element_blank(), plot.background = element_blank(), 
        axis.line = element_blank(), panel.grid = element_blank()) +
  scale_x_continuous(limits = c(10, 35)) +
  scale_y_continuous(limits = c(0, 6)) 

或者,您也可以完全不使用坐标轴,而是使用网格线。

qplot(mpg, wt, data=mtcars) +
  scale_x_continuous(limits = c(10, 35)) +
  scale_y_continuous(limits = c(0, 6)) +
  theme_bw() + 
  theme(panel.border=element_rect(color='white'), 
        axis.ticks = element_line(color='gray90'))