[R] ggplot2: Controlling line width of panel borders

thomas.parr at maine.edu thomas.parr at maine.edu
Wed Mar 27 21:37:12 CET 2013


This is an old post, but I had the exact problem described here and after 
two days of monkeying around in theme, I wanted to provide the non-ggplot 
related "solution." 

In summary, the problem is that in the faceted plots, lines on the right an 
bottom in the subplots appear to be thicker than faceted.  This is a 
problem with the graphics device you are using (a windows x11 device I 
suspect).  I have provided a slightly more concise illustration of the 
problem. 
################
x11()
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(. ~ cyl)+
theme_bw()+  #drop a mostly white theme on for contrast
theme(panel.border = element_rect(fill=NA, colour = "black", size=1)) #bump 
up the border size to 1, the minimum for many publications.
################
Now do the same with: 
################
pdf(file="mygraphic.pdf")
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(. ~ cyl)+
theme_bw()+  #drop a mostly white theme on for contrast
            theme(panel.border = element_rect(fill=NA, colour = "black", 
size=1))                                       #bump up the border size to 
1, the minimum for many publications.
dev.off()
################
This will output directly to a file so you will need to hunt it down. 
 Inspect it and you will see that there is no problem, everything looks 
exactly the way you coded it.

In short, the problem is not with ggplot.  X11(), I assume,  or some unseen 
process calling a windows graphic device is just not visually representing 
the code properly.  If you use a pdf graphics device to output the figure 
the "problem" goes away.  However, this means you can't view it 
instantly... but you do have a vectorized figure.

Trust your code not your graphics device.


    


>


More information about the R-help mailing list