本文介绍了ggplot2:boxplot与颜色和文本标签映射到两个分类变量的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想用一个数据集 densityAGRLKA 在x轴上创建一个带有3个分类变量(物种,位置,位置)的gplot2 boxplot。 以下函数: ggplot(densityAGRLKA,aes(species ,密度,填充=位置,alpha =位置),闪避=物种,位置)+ stat_boxplot(geom ='errorbar')+ geom_boxplot() 创建一个情节,其中物种的分组很好,但颜色具有误导性。我不知道如何解决这个问题。 我需要一个具有以下属性的图: 根据位置 species 对数据进行分组 ,并在顶部然后底部。 另外,如果位置写在两个属于一个盒子,每个盒子下面有位置。或者,也许更好的着色/着色框,然后提供一个清晰的图例? 示例数据: densityAGRLKA = structure(list(location = structure(c(1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,2L,2L,2L,2L ,2L,2L,2L,2L,2L,2L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L ,2L, 2L,2L),.Label = c(SF,SS),class =factor),species = structure(c(1L, 1L,1L,1L ,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,2L,2L,2L,2L,2L,2L,2L ,2L,2L,2L,2L,2L,2L, 2L,2L,2L,2L,2L,2L,2L),标签= c(AGR,LKA ), position = structure(c(1L,1L,1L,1L,1L,2L,2L,2L,2L, 2L,1L,1L,1L,1L,1L,2L,2L 2L,2L,2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L, b $ b 2L),.Label = c(top,bottom),class =factor),density = c(0.41, 0.44,0.41,0.43,0.33,0.35,0.43,0.34,0.46,0.32,0.32,0.4, 0.4,0.45,0.34,0.39,0.39,0.31,0.38,0.48,0.3,0.42, 0.34, 0.35,0.35,0.4,0.38,0.42,0.36,0.34,0.46,0.38,0.36, 0.39,0.38,0.39,0.39,0.39,0.36,0.39,0.51,0.38)),.names = c(位置 ,species,position,density),row.names = c(NA,-40L),class =data.frame) 解决方案这里有三个逐步涉及的选项,用于按照您在问题中描述的方式添加文本,使用faceting的方法: 设置 首先,创建一些我们稍后会用到的效用值: #颜色向量 LocPosCol = c(hcl(0,100,c(50,80)),hcl 240,100,c(50,80))) LocCol = c(hcl(c(0,240),100,65)) #道奇宽度 pd = position_dodge(0.7 ) 现在创建一个基本的boxplot。我们使用交互函数根据位置和位置的所有组合创建填充美学: p = ggplot(densityAGRLKA, aes(species,density, fill = interaction(location,position,sep = - ,lex.order = TRUE)))+ geom_boxplot(width = 0.7,position = pd)+ theme_bw()+ scale_fill_manual(values = LocPosCol) 现在这里有五个变种。三个基于你的问题的请求和两个基于faceting的替代方案: 沿着底部的图例 > library(dplyr) p + geom_text(data = densityAGRLKA%>%group_by(species,location,position )%>% summary(value = unique(paste(location,location,sep = - ))), aes(label = value,y = 0.29, color =交互(位置,位置,sep = - ,lex.order = TRUE)), position = pd,size = 3.3,fontface =bold)+ scale_color_manual(values = LocPosCol)+ 指南(color = FALSE,fill = FALSE) 使用两级文本标识符 p + geom_text(data = densityAGRLKA%>%group_by(species,location)%>%总结%>%mutate(position = NA), aes(label = location,color = location,y = 0.29), position = pd,size = 4.2,fontface =bold )+ geom_text(data = densityAGRLKA%>%group_by(species,position,location)%>%汇总, aes(label = position, color = interaction (位置,位置,sep = - ,lex.order = TRUE),y = 0.28), position = pd,size = 3.7,fontface =bold)+ scale_color_manual (值为c(LocCol [1],LocPosCol [1:2],LocCol [2],LocPosCol [3:4]))+ guides(color = FALSE,fill = FALSE) p> 使用标准ggplot方面标签 以下代码相对比较简单,但我对这种方式并不狂热刻面会导致重复标签,而不是在连续刻面重复两次或四次相同级别时使用单个跨度标签。以下是标准ggplot刻面。以下是一个更改方面标签以跨越多个方面的过程(比较痛苦)的示例。 ggplot(densityAGRLKA, aes(,density))+ geom_boxplot(width = 0.7,position = pd)+ theme_bw()+ facet_grid(。〜species + location + position)+ theme(panel.margin = unit(0,lines), panel.border = element_rect(color =grey90), axis.ticks.x = element_blank())+ labs(x =) 面向跨越给定类别的单个facet标签 要更改构面标签,以便使用单个标签跨越给定的类别(而不是为每个方面重复相同的标签)需要超出ggplot的范围,并使用较低级别的网格函数来更改facet strip label grobs。这里有一个例子: library(gtable) library(grid) p = ggplot (密度:AAG(,density))+ geom_boxplot(width = 0.7,position = pd)+ theme_bw()+ facet_grid(。〜species + location + position)+ theme(panel.margin = unit(0,lines), strip.background = element_rect(color =grey30,fill =grey90), panel.border = element_rect(color =grey90), axis.ticks.x = element_blank())+ labs(x =) pg = ggplotGrob(p) #为物种 pos = c(4,11)添加spanning strip标签(i为1:2){ pg list(rectGrob(gp = gpar(col =grey50,fill =grey90)), textGrob(unique(densityAGRLKA $ species)[i], gp = gpar (cex = 0.8))),3,pos [i],3,pos [i] +7, name = c(a,b))} #添加生成带标签fo r位置 pos = c(4,7,11,15)(i in 1:4){ pg = gtable_add_grob(pg, list(rectGrob(gp = gpar(col =grey50,fill =grey90)), textGrob(rep(unique(densityAGRLKA $ location),2)[i], gp = gpar(cex = 0.8) )),4,pos [i],4,pos [i] +3, name = c(c,d))} plot (pg) I would like to create a boxplot with ggplot2 from a dataset densityAGRLKA with 3 categorical variables (species, location, position) on the x-axis. The following function:ggplot(densityAGRLKA, aes(species, density, fill=location, alpha=position), dodge=species, position) + stat_boxplot(geom ='errorbar') + geom_boxplot() creates a plot, in which the grouping of the species is fine, but the colours are misleading. I have no idea how to fix this.I need a plot with the following properties: the data is grouped by specieswithin the group by location, and within the location in the order top and then bottom. Additionally, it would be great, if location would be written underneath the two boxes that belong together, and position underneath every single box. Or maybe better colouring/shading the boxes and then provide a clear legend?Sample data:densityAGRLKA = structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("SF", "SS"), class = "factor"), species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("AGR", "LKA"), class = "factor"), position = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("top", "bottom"), class = "factor"), density = c(0.41, 0.41, 0.43, 0.33, 0.35, 0.43, 0.34, 0.46, 0.32, 0.32, 0.4, 0.4, 0.45, 0.34, 0.39, 0.39, 0.31, 0.38, 0.48, 0.3, 0.42, 0.34, 0.35, 0.4, 0.38, 0.42, 0.36, 0.34, 0.46, 0.38, 0.36, 0.39, 0.38, 0.39, 0.39, 0.39, 0.36, 0.39, 0.51, 0.38)), .Names = c("location", "species", "position", "density"), row.names = c(NA, -40L), class = "data.frame") 解决方案 Here are three progressively more involved options for adding text in the way you describe in your question, followed by a different approach using faceting:Set-upFirst, create a couple of utility values we'll use later:# Color vectorsLocPosCol = c(hcl(0,100,c(50,80)), hcl(240,100,c(50,80)))LocCol = c(hcl(c(0,240),100,65))# Dodge widthpd = position_dodge(0.7)Now create a basic boxplot. We use the interaction function to create a fill aesthetic based on all combinations of location and position:p = ggplot(densityAGRLKA, aes(species, density, fill=interaction(location, position, sep="-", lex.order=TRUE))) + geom_boxplot(width=0.7, position=pd) + theme_bw() + scale_fill_manual(values=LocPosCol)Now here are five variations on that boxplot. Three based on the request in your question and two alternatives based on faceting:With legend along bottomp + labs(fill="Location-Position") + theme(legend.position="bottom")With text identifiers under the boxeslibrary(dplyr)p + geom_text(data=densityAGRLKA %>% group_by(species, location, position) %>% summarise(value=unique(paste(location, position, sep="-"))), aes(label=value, y=0.29, color=interaction(location, position, sep="-", lex.order=TRUE)), position=pd, size=3.3, fontface="bold") + scale_color_manual(values=LocPosCol) + guides(color=FALSE, fill=FALSE)With two levels of text identifiers under the boxesp + geom_text(data=densityAGRLKA %>% group_by(species, location) %>% summarise %>% mutate(position=NA), aes(label=location, color=location, y=0.29), position=pd, size=4.2, fontface="bold") + geom_text(data=densityAGRLKA %>% group_by(species, position, location) %>% summarise, aes(label=position, color=interaction(location, position, sep="-", lex.order=TRUE), y=0.28), position=pd, size=3.7, fontface="bold") + scale_color_manual(values=c(LocCol[1],LocPosCol[1:2],LocCol[2],LocPosCol[3:4])) + guides(color=FALSE, fill=FALSE)Faceting with standard ggplot facet labelsThe code below is relatively straightforward, but I'm not wild about the way faceting results in repeating of labels, rather than using a single spanning label when the same level is repeated two or four times in consecutive facets. Below is the "standard" ggplot faceting. Following that is an example of the (somewhat painful) process of changing the facet labels to span multiple facets.ggplot(densityAGRLKA, aes("", density)) + geom_boxplot(width=0.7, position=pd) + theme_bw() + facet_grid(. ~ species + location + position) + theme(panel.margin=unit(0,"lines"), panel.border=element_rect(color="grey90"), axis.ticks.x=element_blank()) + labs(x="")Faceting with a single facet label spanning a given categoryTo change the facet labels so that a single label spans a given category (rather than having the same label repeated for each facet) requires going outside of ggplot and using lower level grid functions to change the facet strip label grobs. Here's an example:library(gtable)library(grid)p=ggplot(densityAGRLKA, aes("", density)) + geom_boxplot(width=0.7, position=pd) + theme_bw() + facet_grid(. ~ species + location + position) + theme(panel.margin=unit(0,"lines"), strip.background=element_rect(color="grey30", fill="grey90"), panel.border=element_rect(color="grey90"), axis.ticks.x=element_blank()) + labs(x="")pg = ggplotGrob(p)# Add spanning strip labels for speciespos = c(4,11) for (i in 1:2) { pg <- gtable_add_grob(pg, list(rectGrob(gp=gpar(col="grey50", fill="grey90")), textGrob(unique(densityAGRLKA$species)[i], gp=gpar(cex=0.8))), 3,pos[i],3,pos[i]+7, name=c("a","b"))}# Add spanning strip labels for locationpos=c(4,7,11,15)for (i in 1:4) { pg = gtable_add_grob(pg, list(rectGrob(gp = gpar(col="grey50", fill="grey90")), textGrob(rep(unique(densityAGRLKA$location),2)[i], gp=gpar(cex=0.8))), 4,pos[i],4,pos[i]+3, name = c("c", "d"))}plot(pg) 这篇关于ggplot2:boxplot与颜色和文本标签映射到两个分类变量的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 09:38