本文介绍了你如何在ggplot2中添加一个通用标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我经常使用数值来表示刻面。 我希望提供足够的信息来解释补充标题中的这些分面值,类似于轴标题。 标签选项重复了很多不必要的文本,并且对于较长的变量标题不可用。 有什么建议吗? test< -data.frame(x = 1:20,y = 21:40,facet。 a = rep(c(1,2),10),facet.b = rep(c(1,2),each = 20)) qplot(data = test,x = x,y = y, facets = facet.b〜facet.a) 我会喜欢的: 最好我可以在ggplot中完成: qplot(data = test,x = x,y = y)+ facet_grid(facet。 b〜facet.a,labeller = label_both) 如@Hendy所示,类似于: 为ggplot2图添加一个辅助y轴 - 使它完美 解决方案 ggplot2 在内部使用 gtable ,所以修改一个数字很容易: library(ggplot2) test< - data.frame(x = 1:20,y = 21:40,面face.a = rep(c(1,2),10), facet.b = rep(c(1,2),each = 20))p #get gtable object z< - ggplotGrob(p) 图书馆(网格)图书馆(gtable)#为右边的条带添加标签z< - gtable_add_cols(z,unit(z $ widths [[7]],'cm'), 7)z list(rectGrob(gp = gpar(col = NA,fill = gray(0.5))), textGrob(Variable 1,rot = -90,gp = gpar(col = gray(1)))), 4,8,6,name = paste(runif(2))) #add label (z,单位(z $ heights [[3]],'cm'),2)z< - gtable_add_grob(z, list(rectGrob (gp = gpar(col = NA,fill = gray(0.5))), textGrob(Variable 2,gp = gpar(col = gray(1)))), 3,4 ,3,6,name = paste(runif(2))) #添加边距z< - gtable_add_cols(z,单元(1/8,line),7) z< - gtable_add_rows(z,unit(1/8,line),3) #绘制 grid.newpage() grid。 draw(z) 当然,您可以编写一个自动添加条形标签的函数。未来版本的 ggplot2 可能具有此功能;不确定。 I often have numeric values for faceting.I wish to provide sufficient information to interpret these faceting values in a supplemental title, similar to the axis titles.The labeller options repeat much unnecessary text and are unusable for longer variable titles.Any suggestions?The default:test<-data.frame(x=1:20, y=21:40, facet.a=rep(c(1,2),10), facet.b=rep(c(1,2), each=20))qplot(data=test, x=x, y=y, facets=facet.b~facet.a)What I would love:The best I can do in ggplot:qplot(data=test, x=x, y=y)+facet_grid(facet.b~facet.a, labeller=label_both)As indicated by @Hendy, similar to:add a secondary y axis to ggplot2 plots - make it perfect 解决方案 As the latest ggplot2 uses gtable internally, it is quite easy to modify a figure:library(ggplot2)test <- data.frame(x=1:20, y=21:40, facet.a=rep(c(1,2),10), facet.b=rep(c(1,2), each=20))p <- qplot(data=test, x=x, y=y, facets=facet.b~facet.a)# get gtable objectz <- ggplotGrob(p)library(grid)library(gtable)# add label for right stripz <- gtable_add_cols(z, unit(z$widths[[7]], 'cm'), 7)z <- gtable_add_grob(z, list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))), textGrob("Variable 1", rot = -90, gp = gpar(col = gray(1)))), 4, 8, 6, name = paste(runif(2)))# add label for top stripz <- gtable_add_rows(z, unit(z$heights[[3]], 'cm'), 2)z <- gtable_add_grob(z, list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))), textGrob("Variable 2", gp = gpar(col = gray(1)))), 3, 4, 3, 6, name = paste(runif(2)))# add marginsz <- gtable_add_cols(z, unit(1/8, "line"), 7)z <- gtable_add_rows(z, unit(1/8, "line"), 3)# draw itgrid.newpage()grid.draw(z)Of course, you can write a function that automatically add the strip labels. A future version of ggplot2 may have this functionality; not sure though. 这篇关于你如何在ggplot2中添加一个通用标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-11 03:12