r - ggplot2:带有映射到两个分类变量组合的颜色和文本标签的箱线图

标签 r ggplot2 legend label boxplot

我想使用 ggplot2 从数据集 密度AGRLKA 创建一个箱线图,x 轴上有 3 个分类变量(物种、地点、位置)。

以下函数:

ggplot(densityAGRLKA, aes(species, density, fill=location, alpha=position), dodge=species, position) + 
stat_boxplot(geom ='errorbar') + 
geom_boxplot() 

创建一个情节,其中物种的分组很好,但颜色具有误导性。我不知道如何解决这个问题。

我需要一个具有以下属性的图:

  • 数据按物种分组
  • 位置在组内,
  • 并在顺序为顶部然后底部的位置内。

此外,如果将 location 写在属于在一起的两个框下方,并将 position 写在每个框下方,那就太好了。或者也许更好地为盒子着色/着色,然后提供清晰的图例?

示例数据:

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")

最佳答案

以下是三个逐渐复杂的选项,用于按照您在问题中描述的方式添加文本,然后是使用分面的不同方法:

设置

首先,创建几个我们稍后将使用的实用程序值:

# Color vectors
LocPosCol = c(hcl(0,100,c(50,80)), hcl(240,100,c(50,80)))
LocCol = c(hcl(c(0,240),100,65))

# Dodge width
pd = position_dodge(0.7)

现在创建一个基本的箱线图。我们使用interaction函数根据locationposition的所有组合创建填充美感:

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)

现在有该箱线图的五个变体。三种基于您问题中的请求,两种基于分面的替代方案:

底部有图例

p + labs(fill="Location-Position") +
  theme(legend.position="bottom")

enter image description here

框下有文本标识符

library(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)

enter image description here

框下有两层文本标识符

p + 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)

enter image description here

使用标准 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="")

enter image description here

使用跨越给定类别的单个分面标签进行分面

要更改构面标签,以便单个标签跨越给定类别(而不是为每个构面重复使用相同的标签),需要超出 ggplot 并使用较低级别的网格函数来更改构面 strip 标签组。这是一个例子:

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 species
pos = 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 location
pos=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)

enter image description here

关于r - ggplot2:带有映射到两个分类变量组合的颜色和文本标签的箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36968581/

相关文章:

r - 减少整体图例大小(元素和文本)

r - 将自定义文本插入到ggplot2

R Studio 更改 pandoc .tex 模板

r - 在geom_sf_text中,如何在美学上微调x和y?

r - 使用 ggplot 为世界地图中的特定国家添加颜色

r - 如何在 R 上的绘图中包含文本的上标?

r - 拆分因数、sapply 和 lm

r - 如何从图例中删除已用值?

r - ggplot2 将两个数据集绘制成一张图片

python - 在 Python pylab rose/polar plot 中更改图例标题的字体大小