Add panel border to ggplot2

RPlotGgplot2

R Problem Overview


I have been asked to place a full border around my plot below:

enter image description here

Using panel.border = element_rect(colour = "black") results in losing in the plot becoming blank.

I can't use theme_bw() as it does not have the same functionality as the usual theme, the code I am currently using is below:

graph<-ggplot(d,aes(x=d$AOE, y=d$MEI)
            )+
  geom_point(shape=20, size=3)+
  geom_rug()+
  annotate("text", x = -1.1, y = 14000, label = "27/04/2011") +
  annotate("text", x = -1.3, y = 10400, label = "03/04/1974") +
  xlab("MEI")+
  ylab("AOE")+
  scale_y_log10()+
  theme(axis.text.y   = element_text(size=14),
        axis.text.x   = element_text(size=14),
        axis.title.y  = element_text(size=14),
        axis.title.x  = element_text(size=14),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black")
  )

graph 

Any advice on how to get a full black border would be very much appreciated!

R Solutions


Solution 1 - R

To use panel.border you also have to specify a blank fill using fill=NA.

Try this:

library(ggplot2)

ggplot(mtcars, aes(mpg, disp)) + geom_point() + geom_rug() +
  theme(axis.text.y   = element_text(size=14),
        axis.text.x   = element_text(size=14),
        axis.title.y  = element_text(size=14),
        axis.title.x  = element_text(size=14),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_rect(colour = "black", fill=NA, size=5)
  )

enter image description here

Solution 2 - R

You can use theme_bw() and theme() together. This should work:

# creating some data
set.seed(1)
d <- data.frame(MEI=rnorm(100), AOE=rlnorm(100, 10, 5))

# creating the plot
ggplot(d,aes(x=MEI, y=AOE)) +
  geom_point(shape=20, size=3) +
  geom_rug() +
  scale_y_log10() +
  theme_bw() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black", size=4))

this gives: enter image description here


A solution without theme_bw() and inspired by @Andrie, but with the use of panel.background instead of panel.border:

ggplot(d,aes(x=MEI, y=AOE)) +
  geom_point(shape=20, size=3) +
  geom_rug() +
  scale_y_log10() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black", size=4, fill=NA))

this will give the exact same plot. The difference between panel.background and panel.border is that panel.background is drawn underneath the plot and panel.border is drawn on top of the plot.

Solution 3 - R

If you use any of the panel. options you will get a border around each individual facet when facetting. If you want a border around the outside of the entire plot, title etc included, then use plot.background. E.g:

library(ggplot2)

ggplot(mtcars, aes(mpg, disp)) + geom_point() + geom_rug() +
  labs(title = "Hello plot!") +
  facet_wrap(~cyl) +
  theme(axis.text.y   = element_text(size=14),
        axis.text.x   = element_text(size=14),
        axis.title.y  = element_text(size=14),
        axis.title.x  = element_text(size=14),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"),
        plot.background = element_rect(colour = "black", fill=NA, size=5)
  )

Created on 2021-06-22 by the reprex package (v2.0.0)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMethexisView Question on Stackoverflow
Solution 1 - RAndrieView Answer on Stackoverflow
Solution 2 - RJaapView Answer on Stackoverflow
Solution 3 - RMilesMcBainView Answer on Stackoverflow