从 ggplot2 图中删除右边框

MYa*_*208 3 plot r ggplot2 r-grid

使用以下代码,我可以删除顶部和右侧边框以及其他内容。我想知道如何ggplot2仅删除图形的右边框。

p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() 

p + theme_classic()
Run Code Online (Sandbox Code Playgroud)

bap*_*ste 5

主题系统妨碍了,但稍加改动,您就可以破解主题元素,

library(ggplot2)
library(grid)
element_grob.element_custom <- function(element, ...)  {

  segmentsGrob(c(1,0,0),
               c(0,0,1),
               c(0,0,1),
               c(0,1,1), gp=gpar(lwd=2))
}
## silly wrapper to fool ggplot2
border_custom <- function(...){
  structure(
    list(...), # this ... information is not used, btw
    class = c("element_custom","element_blank", "element") # inheritance test workaround
  ) 

}
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
  theme_classic() +
  theme(panel.border=border_custom())
Run Code Online (Sandbox Code Playgroud)