谁还没个明天
2009-08-25 21:05
采纳率: 50%
浏览 3.4k

在 ggplot2中旋转和限制轴标签

我有一个图,x 轴是一个标签很长的因子。虽然可能不是一个理想的可视化,但是现在我想简单地将这些标签旋转为垂直的。 已经用下面的代码解决了这个问题,但是正如你看到的,标签并不是完全可见的。

data(diamonds)
diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))
q <- qplot(cut,carat,data=diamonds,geom="boxplot")
q + opts(axis.text.x=theme_text(angle=-90))

转载于:https://stackoverflow.com/questions/1330989/rotating-and-spacing-axis-labels-in-ggplot2

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • ℡Wang Yan 2009-08-25 22:36
    已采纳

    Change the last line to

    q + theme(axis.text.x = element_text(angle = 90, hjust = 1))
    

    By default, the axes are aligned at the center of the text, even when rotated. When you rotate +/- 90 degrees, you usually want it to be aligned at the edge instead:

    alt text

    The image above is from this blog post.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • derek5. 2010-12-20 04:24

    To make the text on the tick labels fully visible and read in the same direction as the y-axis label, change the last line to

    q + theme(axis.text.x=element_text(angle=90, hjust=1))
    
    评论
    解决 无用
    打赏 举报
  • lrony* 2016-05-02 09:32

    I'd like to provide an alternate solution, a robust solution similar to what I am about to propose was required in the latest version of ggtern, since introducing the canvas rotation feature.

    Basically, you need to determine the relative positions using trigonometry, by building a function which returns an element_text object, given angle (ie degrees) and positioning (ie one of x,y,top or right) information.

    #Load Required Libraries
    library(ggplot2)
    library(gridExtra)
    
    #Build Function to Return Element Text Object
    rotatedAxisElementText = function(angle,position='x'){
      angle     = angle[1]; 
      position  = position[1]
      positions = list(x=0,y=90,top=180,right=270)
      if(!position %in% names(positions))
        stop(sprintf("'position' must be one of [%s]",paste(names(positions),collapse=", ")),call.=FALSE)
      if(!is.numeric(angle))
        stop("'angle' must be numeric",call.=FALSE)
      rads  = (angle - positions[[ position ]])*pi/180
      hjust = 0.5*(1 - sin(rads))
      vjust = 0.5*(1 + cos(rads))
      element_text(angle=angle,vjust=vjust,hjust=hjust)
    }
    

    Frankly, in my opinion, I think that an 'auto' option should be made available in ggplot2 for the hjust and vjust arguments, when specifying the angle, anyway, lets demonstrate how the above works.

    #Demonstrate Usage for a Variety of Rotations
    df    = data.frame(x=0.5,y=0.5)
    plots = lapply(seq(0,90,length.out=4),function(a){
      ggplot(df,aes(x,y)) + 
        geom_point() + 
        theme(axis.text.x = rotatedAxisElementText(a,'x'),
              axis.text.y = rotatedAxisElementText(a,'y')) +
        labs(title = sprintf("Rotated %s",a))
    })
    grid.arrange(grobs=plots)
    

    Which produces the following:

    Example

    评论
    解决 无用
    打赏 举报
  • Didn"t forge 2017-08-01 02:29

    Use coord_flip()

    In "R for Data Science," Wickham and Grolemund speak to this exact problem. In Chapter 3.8, Position Adjustments, they write:

    coord_flip() switches the x and y axes. This is useful (for example), if you want horizontal boxplots. It’s also useful for long labels: it’s hard to get them to fit without overlapping on the x-axis.

    Applying this to your plot, we just add + coord_flip() to the ggplot:

    data(diamonds)
    diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))
    
    qplot(cut,carat,data = diamonds, geom = "boxplot") +
      coord_flip()
    

    enter image description here

    And now the super long titles are horizontally spread out and very easy to read!

    评论
    解决 无用
    打赏 举报
  • bug^君 2018-11-06 22:23

    The ggpubr package offers a shortcut that does the right thing by default (right align text, middle align text box to tick):

    library(ggplot2)
    diamonds$cut <- paste("Super Dee-Duper", as.character(diamonds$cut))
    q <- qplot(cut, carat, data = diamonds, geom = "boxplot")
    q + ggpubr::rotate_x_text()
    

    Created on 2018-11-06 by the reprex package (v0.2.1)

    Found with a GitHub search for the relevant argument names: https://github.com/search?l=R&q=element_text+angle+90+vjust+org%3Acran&type=Code

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题