Removing right border from ggplot2 graph

前端 未结 2 1030
醉话见心
醉话见心 2021-01-20 06:32

With the following code I can remove top and right borders along with other things. I wonder how to remove the right border of the ggplot2 graph only.

2条回答
  •  北海茫月
    2021-01-20 06:59

    the theme system gets in the way, but with a little twist you can hack the theme elements,

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

提交回复
热议问题