如何在 ggplot2 中使用 annotation_custom 自定义 x 轴

How to customize x-axis with annotation_custom in ggplot2

在我的真实数据中,我想删除 x 轴文本并添加新文本。

按照 here 中的示例(我可以重现),我希望将 grob 的列表传递给 annotation_custom,但不知何故我没有成功。需要明确的是,我知道旋转 axix.text.x 的方法要简单得多,这不是我的目标,这个练习对某些人来说可能听起来很愚蠢。但如果知道我做错了,我将不胜感激。

#create a dataframe to pass in textGrob
mpg_grob <- c("10", "15", "20", "25", "30", "35")
mpg_axis <- c(10,15, 20, 25, 30, 35)
wt_axis <- rep(-0.07, 6)
dfwow <- data.frame(mpg_grob, mpg_axis, wt_axis)



#create a basic plot
p <- ggplot(mtcars, aes(mpg, wt)) + 
        facet_grid(.~cyl) + 
        geom_point() + theme(axis.title.x = element_text(margin=margin(50,0,0,0)), axis.text.x = element_blank()) +
        coord_cartesian(clip = "off")

#Add annotations
for (i in 1:length(dfwow$mpg_grob)){
        p <- p + annotation_custom(
                textGrob(label = dfwow$mpg_grob[i],
                         rot = 90,
                         gp = gpar(frontsize = 9)),
                xmin = dfwow$mpg_axis[i],
                xmax = dfwow$mpg_axis[i],
                ymin = -25,
                ymax = -15
        )
}

在结果图中,没有注释。

如果我这样做 p$layers 这就是我所看到的

> p$layers
[[1]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 

[[2]]
geom_custom_ann: grob = list(label = "10", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6711", gp = list(frontsize = 9), vp = NULL), xmin = 10, xmax = 10, ymin = -25, ymax = -15, xmin = 10, xmax = 10, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity 

[[3]]
geom_custom_ann: grob = list(label = "15", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6712", gp = list(frontsize = 9), vp = NULL), xmin = 15, xmax = 15, ymin = -25, ymax = -15, xmin = 15, xmax = 15, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity 

[[4]]
geom_custom_ann: grob = list(label = "20", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6713", gp = list(frontsize = 9), vp = NULL), xmin = 20, xmax = 20, ymin = -25, ymax = -15, xmin = 20, xmax = 20, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity 

[[5]]
geom_custom_ann: grob = list(label = "25", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6714", gp = list(frontsize = 9), vp = NULL), xmin = 25, xmax = 25, ymin = -25, ymax = -15, xmin = 25, xmax = 25, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity 

[[6]]
geom_custom_ann: grob = list(label = "30", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6715", gp = list(frontsize = 9), vp = NULL), xmin = 30, xmax = 30, ymin = -25, ymax = -15, xmin = 30, xmax = 30, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity 

[[7]]
geom_custom_ann: grob = list(label = "35", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6716", gp = list(frontsize = 9), vp = NULL), xmin = 35, xmax = 35, ymin = -25, ymax = -15, xmin = 35, xmax = 35, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity 

您的 ymin 和 ymax 超出了 y-axis 的范围。它应该在您要放置新标签的范围内。

试试这个

#create a basic plot
  p <- ggplot(mtcars, aes(mpg, wt)) + 
    facet_grid(.~cyl) + 
    geom_point() + theme(axis.title.x = element_text(margin=margin(50,0,0,0)), axis.text.x = element_blank()) +
    coord_cartesian(clip = "off")
  
  #Add annotations
  for (i in 1:length(unique(dfwow$mpg_grob))){
    p <- p + annotation_custom(
      textGrob(label = paste0('n=',dfwow$mpg_grob[i]),
               rot = 90,
               gp = gpar(fontsize = 8)),
      xmin = dfwow$mpg_axis[i],
      xmax = dfwow$mpg_axis[i],
      ymin = .8,
      ymax = 1.5
    )
  }
  p