【问题标题】:How to rotate a part of label axis with several variables in R ggplot2如何在R ggplot2中使用多个变量旋转标签轴的一部分
【发布时间】:2017-07-18 12:07:39
【问题描述】:

我以发布here的相同示例

所以dataframe如下:

results <- data.frame(Name=factor(c("Mark", "Mark", "Sue", "Sue")), 
           Tutor= factor(c("Eric", "Eric", "Richard", "Richard")),
           Test= factor(c("Maths","English","Maths", "English")),
           Score= c(100,91,88,71),
           Percent=c(100,91,100,80.7),
           school.year= c(2,2,5,5))

我采用了第一个建议的解决方案来标记带有多个变量的轴:

 results$label <- paste(results$Name,results$Tutor,sep='\n')

 ggplot(results, aes(y=Percent, x=label, colour=Test, fill=Test)) +
 geom_bar(stat='identity', position='dodge') +
 ggtitle('Test Results') +
 ylab('Percent')

我们得到这张图

现在我想只旋转 (90°) x 轴标签的第一行,如下图所示:

有没有办法做到这一点?非常感谢

【问题讨论】:

    标签: r ggplot2 axis-labels


    【解决方案1】:

    这可能不是最好的方法,但您可以使用来自ggplot 的annote() 来执行此操作。 我将标签从Tutor 和Name 更改为Tutor,并为Name 添加了一个文本标签。

    results$label <- paste(results$Tutor)
    row1 <- results$Name
    
        ggplot(results, aes(y=Percent, x=label, colour=Test, fill=Test)) +
      geom_bar(stat='identity', position='dodge') +
      ggtitle('Test Results') +
      annotate(geom = "text", x=results$label, y=-5, label=row1, size=3, angle = 90)+
      ylab('Percent')
    

    当然你可以调整添加的文本标签的大小和位置。

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多