3

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.

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

p + theme_classic()
halfer
  • 19,824
  • 17
  • 99
  • 186
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
  • it is not so easy because `panel.border` is managed as an `element_rect` object, thus it is already a rectangle. – Davide Passaretti Dec 27 '14 at 12:26
  • 1
    Possible duplicate: http://stackoverflow.com/questions/10861773/remove-grid-background-color-and-top-and-right-borders-from-ggplot2 – lawyeR Dec 27 '14 at 12:30
  • I agree with Davide: it may not be easily possible. I assume that theme_classic actually completely removes the box and instead draws axis lines. To illustrate this, try `p+theme(axis.line=element_line())`. It will plot `p` with the standard theme but with the axis lines added. – Stibu Dec 27 '14 at 12:33
  • @lawyeR: No this is not a duplication of [stackoverflow.com/q/10861773/707145](http://stackoverflow.com/q/10861773/707145). – MYaseen208 Dec 27 '14 at 12:33

2 Answers2

5

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())
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Thanks for your nice answer. Please see me edits and suggest any solution if you don't mind. Thanks again for your help. – MYaseen208 Dec 27 '14 at 14:22
  • presumably, if you apply this theme to the plots before converting them to gtable, this element should be passed along – baptiste Dec 27 '14 at 14:31
2

You can just remove both borders (as it's in the first place with theme_classic()), and then add one with annotate():

p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + theme_classic() + annotate(
    geom = 'segment',
    y = Inf,
    yend = Inf,
    x = -Inf,
    xend = Inf
)

the resulting image

(The idea is from: How to add line at top panel border of ggplot2)


By the way, you of course don't need to use theme_classic(). If you use a theme that has different default borders, you can switch them on/off with the theme() function's parameters panel.border (sets all borders) and axis.line (sets separate axis "borders").

For example (for default theme):

p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + annotate(
    geom = 'segment',
    y = Inf,
    yend = Inf,
    x = -Inf,
    xend = Inf
) + theme(panel.border = element_blank(), axis.line = element_line())

the resulting image 2

gaspar
  • 898
  • 1
  • 13
  • 26