ggplot2: yaxis labels not aligning across facets

前端 未结 1 1182
灰色年华
灰色年华 2021-01-25 15:27

I\'m having some trouble aligning axis labels across facets in ggplot. I\'m trying to left align all of the y-axis labels to make the text look more uniform, but the moment the

相关标签:
1条回答
  • 2021-01-25 16:21

    You can pad the labels out to the length of the longest string and then display them in a fixed-width font:

    max_width = max(nchar(as.character(test$label)))
    test$label_padded = sprintf(paste0("%-", max_width, "s"), test$label)
    
    # (Ignore this if not on Windows)
    windowsFonts(Consolas = "Consolas")
    ggplot(test, aes(x = xdum, y = label_padded, label = data)) + 
        facet_grid(facett~., scales = "free", space = "free") + 
        geom_tile() + 
        theme(axis.ticks = element_blank(), 
              axis.text.y = element_text(hjust = 0, family = "Consolas"))
    

    0 讨论(0)
提交回复
热议问题