Add secondary X axis labels to ggplot with one X axis

后端 未结 3 1669
囚心锁ツ
囚心锁ツ 2020-12-03 16:16

**Edit, there are two great solutions here, one is marked as the answer, but @hrbrmstr provides a great solution combining two ggplots which works well for this simple plot.

3条回答
  •  情歌与酒
    2020-12-03 16:54

    Something like this, perhaps. Note the setting of expand for both axes to deal with proper spacing, and positions of the category names.

    The labels in your figure aren't really off-centre, they are in the centre of their category boundary. It's just that by default the axes are expanded a bit further.

    If you want to get more fancy, you can draw outside of the plotting area too, but it requires a bit more fiddeling. This question should get you started.

    ggplot(mpg, aes(class))+
      geom_bar()+
      geom_text(data = data.frame(br = breaks.minor), aes(y = br, label = br, x = 7.75),
                size = 4, col = 'grey30') +
      coord_flip()+
      scale_y_continuous(limit = lims, minor_breaks = breaks.minor, 
                         breaks = breaks.major, labels = labels.minor, 
                         expand = c(0, 0)) +
      scale_x_discrete(expand = c(0.05, 0)) +
      theme(panel.grid.major.x = element_blank()) +
      theme(panel.grid.major.y = element_blank()) +
      theme(axis.ticks.x=element_blank()) +
      theme(axis.title= element_blank())
    

提交回复
热议问题