Problems with panel.border in ggplot

I would like to make the border of my plots a specific color other than black. I notice that if I set the default theme themeww () using:

theme_set(theme_bw()) 

that I can set the border with the color that I want to use

 theme(panel.border = element_rect(color="darkred", size=0.5, linetype="solid"). 

It seems to work. The border of each panel, whether it is a faceted plot or a single plot, takes a β€œdark” color, and the rest of the graph is the same as before I changed the panel.

However, if I use a different default theme, such as theme_gray () or theme_classic (), the border changes, but the contents of each of the facets are destroyed (completely white).

Any idea what causes this difference in behavior or what can I do to fix this? I would like to use theme_gray () and place a thin colored line around the border of each face.

+6
source share
1 answer

The theme() help page says panel.borded= This should be used with fill=NA because it spans the panels.

For theme_bw() , panel.border = element_rect(fill = NA,colour = "grey50") theme_bw() already exists, so when you use your operator, only the color changes and the fill remains as NA .

For theme_grey() and theme_bw() there is panel.border = element_blank() , so when you add the expressions color= and fill= change, because earlier this element was empty, and the default value for rect is fill="white" (at least least for theme_grey() ).

Use

 + theme(panel.border = element_rect(fill=NA,color="darkred", size=0.5, linetype="solid")) 
+6
source

All Articles