Search code examples
rggplot2facet-grid

ggplot2, how can I change the position of strip labels or add a legend


I created a pair of bar plots using facet_grid in ggplot2. I have customized everything as I like except the strip labels. I figured out how to turn off the strip labels using strip.text.y = element_blank(), and I can move the labels to the left side of the chart (switch). However, neither are satisfactory. I would like the labels placed at the top or to make a legend. If I can get them to the top of each plot I would probably want to remove teh strip background and justify the position of the text, say to the right side of each plot. I found a reference to a strip.position element but it did not work for me.

I tried strip.position but could not get it to work. In fact strip.position did not appear as an option when typing in code in R studio.

Create dataframe

cage<-c(3:11)
sage<-c(2:8,10,12:16)
Age<-c(cage,sage)
c<-rep("Choptank",9)
s<-rep("Severn",13)
River<-c(c,s)
n<-c(2,35,19,4,1,52,4,3,2,1,2,39,11,5,2,57,2,1,3,4,2,2)
B<-data.frame(River,Age,n) %>% 
group_by(River,Age) %>% 
tally() %>% 
mutate(prop=n/sum(n))

Theme for bar plots

bartheme<- theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.spacing.y=unit(1, "lines"),
axis.line = element_line(size = 1,colour = "black"),
axis.title = element_text(colour="black",
size = rel(1.5)),
plot.title = element_text(hjust = 0.5, size=14),
plot.subtitle = element_text(hjust = 0.5,size=14),
panel.background = element_rect(fill="whitesmoke"),
axis.text = element_text(colour = "black",
size = rel(1)))

Faceted bar plot, 1 stacked over the other

Bplot <- ggplot(data = B,
aes(x = Age, y =prop))+ 
geom_bar(stat = 'identity', position = 
position_dodge(),fill="dodgerblue")+
geom_text(aes(label=round(prop,2)), vjust=-0.3, size=3.5)+
facet_grid(rows=vars(River))+
scale_y_continuous(limits=c(0,1), oob=rescale_none,expand=c(0,0))+
scale_x_continuous(breaks=c(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16))

Bplot+bartheme+ggtitle("Age distribution of white 
perch")+ylab("Proportion")

I obtain the stacked plots as expected with strip labels on right side of each plot (text vertical). I would prefer strip labels placed at the top of each plot - preferably with some control over appearance.


Solution

  • The layout of facets can be more finely controlled using facet_wrap and you can control the appearance of facet labels using the theme elements prefixed by strip. See here for details.

    Here is an example with your data that places the labels on top of the graphs and removes the shading, plus right justifies the text. I'm not sure if this is exactly what you are a looking for but it should give you the tools to adjust to your liking.

    bartheme <- theme(panel.border = element_blank(),
                      panel.grid.major = element_blank(),
                      panel.grid.minor = element_blank(),
                      panel.spacing.y=unit(1, "lines"),
                      axis.line = element_line(size = 1,colour = "black"),
                      axis.title = element_text(colour="black", size = rel(1.5)),
                      plot.title = element_text(hjust = 0.5, size=14),
                      plot.subtitle = element_text(hjust = 0.5,size=14),
                      panel.background = element_rect(fill="whitesmoke"),
                      axis.text = element_text(colour = "black", size = rel(1)),
                      strip.background = element_blank(),
                      strip.text = element_text(hjust = 1)
    )
    
    Bplot <- 
      ggplot(data = B, aes(x = Age, y = prop)) +
        geom_bar(stat = 'identity', position = position_dodge(), fill = "dodgerblue") +
        geom_text(aes(label = round(prop, 2)), vjust = -0.3, size = 3.5) +
        facet_wrap(vars(River), ncol = 1) +
        scale_y_continuous(limits=c(0,1), oob=rescale_none,expand=c(0,0))+
        scale_x_continuous(breaks = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16))
    
    Bplot + bartheme + ggtitle("Age distribution of white perch") + ylab("Proportion")