本文介绍了如何在ggplot2中的x轴下面添加注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! library(ggplot2) library(scales) library ($) df1 结构(列表(x =结构( 1:5,。标签= c(5,4 ,3,2,1),class =factor),y = c( 0.166666666666667,0.3361111111111111, 0.0833333333333333,0.2222222222222222,0.2291666666666667 )).Names = c(x,y),row.names = c(NA,-5L),class = c(tbl_df, tbl,data.frame),drop = TRUE ) df1%>%ggplot(aes(x,y))+ geom_bar(stat =identity)+ scale_y_continuous(labels = percent) 我想用粗体添加两行注释例如,'Highest \\\value'bellow 5和'Lowest \\\value'below 1。 我试过 geom_text 但我不能将文本放在我想要的位置。解决方案这可以使用 annotation_custom()来完成。我从这个答案绘图。 困难在于 ggplot 剪辑放置在绘图区域之外的注释,这是您想要执行的操作。但是可以关闭剪辑。 annotation_custom()使用grobs,因此您首先需要创建它们: library(grid) text_high value,gp = gpar(fontsize = 13,fontface =bold)) text_low< - textGrob(Lowest \\\value,gp = gpar(fontsize = 13,fontface =bold)) 接下来,你设置图并存储它: p df1%>%ggplot(aes(x,y))+ geom_bar(stat =identity)+ scale_y_continuous(labels = percent)+ theme(plot.margin = unit(c(1,1,2,1),lines))+ annotation_custom(text_high,xmin = 1,xmax = 1,ymin = -0.07,ymax = -0.07)+ annotation_custom(text_low,xmin = 5,xmax = 5,ymin = -0.07,ymax = -0.07) 第三行确保标签下面有足够的空间,最后两行添加注释。位置以两个坐标的最小值和最大值给出。 grob将以由这些坐标定义的区域为中心。在目前的情况下,通过设置最小值和最大值相同来简单定义一个点似乎是最简单的。 现在您所要做的就是关闭剪辑,如下所示: gt gt $布局$ clip [gt $布局$ name = =panel]< - off grid.draw(gt) 最后一行绘制了图。 I have the following graph:library(ggplot2)library(scales)library(magrittr)df1 <- structure( list( x = structure( 1:5, .Label = c("5", "4", "3", "2", "1"), class = "factor" ), y = c( 0.166666666666667, 0.361111111111111, 0.0833333333333333, 0.222222222222222, 0.291666666666667 ) ), .Names = c("x", "y"), row.names = c(NA,-5L), class = c("tbl_df", "tbl", "data.frame"), drop = TRUE )df1 %>% ggplot(aes(x , y )) + geom_bar(stat = "identity") + scale_y_continuous(labels = percent)I would like to add a two lines note with bold text below 5 and 1. For example, 'Highest \nvalue' bellow 5 and 'Lowest \nvalue' below 1.I tried geom_text but I cannot place the text where I want. 解决方案 This can be done using annotation_custom(). I am drawing from this answer.The difficulty is that ggplot clips annotations that are placed outside the plot area, which is what you want to do. But clipping can be turned off.annotation_custom() uses grobs, so you first need to create the them:library(grid)text_high <- textGrob("Highest\nvalue", gp=gpar(fontsize=13, fontface="bold"))text_low <- textGrob("Lowest\nvalue", gp=gpar(fontsize=13, fontface="bold"))Next, you set up the plot and store it:p <-df1 %>% ggplot(aes(x , y )) + geom_bar(stat = "identity") + scale_y_continuous(labels = percent) + theme(plot.margin = unit(c(1,1,2,1), "lines")) + annotation_custom(text_high,xmin=1,xmax=1,ymin=-0.07,ymax=-0.07) + annotation_custom(text_low,xmin=5,xmax=5,ymin=-0.07,ymax=-0.07)The third line makes sure that there is enough room beneath the plot for your labels and the last two add the annotations. The position is given as min and max values for both coordinates. The grob will be centered in the region that is defined by these coordinates. In the present situation, it seems easiest to simply define a point by setting min and max values identical.All you have to do now is turn off clipping as follows:gt <- ggplot_gtable(ggplot_build(p))gt$layout$clip[gt$layout$name == "panel"] <- "off"grid.draw(gt)The last line draws the plot. 这篇关于如何在ggplot2中的x轴下面添加注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 04:26