Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add panel border to ggplot2

Tags:

plot

r

ggplot2

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!

like image 916
Methexis Avatar asked Oct 04 '14 10:10

Methexis


People also ask

How to add a border in ggplot2?

To change the plot border color of a ggplot2 graph in R, we can use theme function with panel. background argument where we can set the border of the plot panel using element_rect to desired color.

How do I change the color of a border in R?

To change the border color of box of a base R plot, we can use box function with col argument where we can pass the color other than black because black is the default color.


2 Answers

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

like image 79
Andrie Avatar answered Oct 10 '22 16:10

Andrie


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.

like image 24
Jaap Avatar answered Oct 10 '22 15:10

Jaap