I have been asked to place a full border around my plot below:
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!
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.
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.
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) )
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With