【问题标题】:How to add subtext to axes in ggplot2 R如何在ggplot2 R中向轴添加潜文本
【发布时间】:2019-06-03 05:36:37
【问题描述】:

对于主要的 y 轴和 x 轴,我有通用标题,例如“坦克的比率”和“计数”。我想要第二行标签,在其中指定比率和计数。例如。在“坦克的比率”下方,我想要“# in water/# in sand”以较小的字体但沿 y 轴。对于 x 轴也是如此。 这是基本代码

data <- data.frame(set = c(1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4), density = c(1, 3, 3, 1, 3, 1, 1, 1, 3, 3, 1, 3), counts = c(100, 2, 3, 76, 33, 12, 44, 13, 54, 36, 65, 1), ratio = c(1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 90, 1))

library(ggplot2)

ggplot(data, aes(x = counts, y = ratio)) + 
  geom_point() + 
  ylab("Tank's Ratio") + 
  xlab("Counts") 

【问题讨论】:

  • 我不确定是否要调整字体大小,但您可以在 ylab 中添加换行符 (\n)。试试ggplot(data,aes(x=counts, y=ratio)) + geom_point() +ylab("Tank's Ratio \n#in water/# in sand") + xlab("Counts")

标签: r ggplot2 axes subtitle


【解决方案1】:

这不是最优雅的解决方案,但希望它有所帮助:

library(ggplot2)
library(gridExtra)
library(grid)

首先,创建没有 ylab 的情节:

g <- ggplot(data, aes(x = counts, y = ratio)) + 
  geom_point()  +
  ylab("") + 
  xlab("Counts") 

然后为两个轴添加字幕:

g2 <- grid.arrange(g, 
                   bottom = textGrob("in water/ # in sand", 
                                     x = 0.55, y = 1, gp = gpar(fontsize = 9)),
                   left = textGrob("in water/ # in sand",  rot = 90, 
                                   x = 1.5, gp = gpar(fontsize = 9)))

最后,添加 y 轴的描述

grid.arrange(g2, 
             left = textGrob("Tank's Ratio",  rot = 90, 
                             x = 1.7, gp = gpar(fontsize = 12)))

【讨论】:

    【解决方案2】:

    您可以添加 x 和主要标题。

    编辑:这太荒谬了!

      #library(extrafont)
    #loadfonts(dev="win")
    library(tidyverse)
    data %>%
    ggplot(aes(x=counts, y=ratio)) + geom_point() +
    labs(y=expression(atop(bold("Tank's Ratio"),atop(italic("#in water #in sand")))))+
      theme_minimal()+
      theme(axis.title.y = element_text(size=15,family="Comic Sans MS"))
    

    原文:

    library(tidyverse) 
    
          data %>%
        ggplot(aes(x=counts, y=ratio)) + geom_point() +
        labs(y="Tank's Ratio \n #in Water#in sand")
    

    【讨论】:

    • 这不会像 OP 想要的那样使用更小的字体。
    • 已编辑,但我认为它太慢了。
    【解决方案3】:

    您可以使用以下代码,自己定义边距、轴标题和子标题:

    我们使用theme 来增加底部和左边距,并抑制自动生成的轴标题。

    我们使用annotate来生成作为轴标题和副标题的文本,必要时旋转文本。

    我们生成绘图,将其转换为grob,使用此grob,我们可以关闭剪辑并显示绘图。

    g1 <- ggplot(data = data, aes(x = counts, y = ratio, group = 1)) +
      geom_point()  + 
    
      ## increase margin size for left and bottom and
      ## remove the axis titles
      theme(plot.margin = unit(c(1, 1, 4, 4), "lines"),
            axis.title.y = element_blank(),
            axis.title.x = element_blank() ) +
    
      ## define the plotting area to NOT include the annotations
      coord_cartesian(xlim = c(0, 100), ylim= c(0, 100), expand = FALSE) +
    
      ## annotate y axis
      annotate(geom = "text", x = -9, y = 50, label = "Tank's Ratio", angle = 90, size = 5) +
      annotate(geom = "text", x = -5, y = 50, label = "#in water/#in sand", angle = 90, size = 4) +
    
      ## annotate x axis
      annotate(geom = "text", x = 50, y = -5, label = "Counts", size = 5) +
      annotate(geom = "text", x = 50, y = -9, label = "#in water/#in sand", size = 4)
    
    ## turn off  clipping for axis extra labels
    g2 <- ggplot_gtable(ggplot_build(g1))
    g2$layout$clip[g2$layout$name == "panel"] <- "off"
    grid::grid.draw(g2)
    

    这会产生以下图片:

    请告诉我这是否是你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 2016-01-13
      相关资源
      最近更新 更多