如何自定义两个因素的X轴和基于两个因素的颜色条

时间:2019-04-24 18:04:47

标签: r ggplot2 colors axis

我已经制作了一个图表,其中的数据显示了基于两个主要因素(季节和曝光)在两个不同地点的位移,这两个因素均在X轴上进行了标记。我想按季节编辑X轴,所以它不是按字母顺序排列,而是从春季-冬季(而不是秋季到冬季)标记。在第二行显示曝光;有两组暴露标签;我可以看到这是因为它在“季节”标签的每三个标签中居中,但似乎无法纠正它。

如果可能的话,我想根据季节更改栏的颜色,第二个站点使用相同颜色的浅色。理想情况下,春季为绿色,夏季为黄色,秋季为棕色,冬季为灰色,并在季节之间放置刻度,在曝光之间放置较长的刻度

desired colour coded bars output from excel

我尝试使用此代码; Season <- as.character(data$Season) #Then turn it back into a factor with the levels in the correct order Season <- factor(data$Season), levels=unique(data$Season) 要更正Seasons的字母顺序,即使在我更正了csv中的标签之后,它也无济于事。归档到正确的顺序。

这是我目前正在使用的完整代码,由堆栈溢出用户针对上一个问题为我提供。

output from R using current code

library(ggplot2)
library(gtable)
library(grid)
Season <- (data$Season)
Site <- (data$Site)
Exposure <- (data$Exposure)
Average <- data$Average
SEM <- data$SEM
gg <- ggplot(aes(x=as.factor(Site), y=Average, fill=as.factor(Site)), Season, data = data)
gg <- gg + geom_bar(stat = 'identity')
gg <- gg + scale_fill_discrete(guide_legend(title = 'Site'))
gg <- gg + scale_fill_manual(values=c('black', 'grey85'), guide_legend(title = 'Site')) 
gg <- gg + theme_classic() 
gg <- gg + geom_errorbar(aes(ymin=Average-SEM, ymax=Average+SEM), width=.3)
gg <- gg + facet_wrap(~Exposure*Season, strip.position=c('bottom'), nrow=1, drop=F)
gg <- gg + scale_y_continuous(expand = expand_scale(mult = c(0, .05)))
gg <- gg + theme(axis.text.x = element_blank(),
                 axis.ticks.x = element_blank(),
                 axis.title.x = element_blank(),
                 axis.line = element_line(color='black'),
                 strip.placement = 'outside', 
                 panel.spacing.x=unit(0, "lines"), 
                 panel.grid.major.x = element_blank(), 
                 panel.grid = element_blank(), 
                 panel.background = element_rect(fill='white'), 
                 strip.background = element_rect(fill='white', color='white') 
)
print(gg)
season.levels <- levels(data$Season)
exposure.levels <- levels(data$Exposure)
g <- ggplotGrob(gg)
grob.numbers <- grep("strip-b", g$layout$name)
b.strips <- gtable_filter(g, "strip-b", trim = FALSE)

season.left.panels <- seq(1, by=length(levels(data$Exposure)), length.out = length(season.levels))
season.right.panels <- seq(length(exposure.levels), by=length(exposure.levels), length.out = length(season.levels))
left <- b.strips$layout$l[season.left.panels]
right <- b.strips$layout$r[season.right.panels]
top <- b.strips$layout$t[3]
bottom <- b.strips$layout$b[3]
mat   <- matrix(vector("list", length = 10), nrow = 2)
mat[] <- list(zeroGrob())
for (i in 1:length(season.levels)) {
  res <- gtable_matrix("season.strip", mat, unit(c(1, 0, 1, 0, 1), "null"), unit(c(1, 1), "null"))
  season.left <- season.left.panels[i]
  res <- gtable_add_grob(res, g$grobs[[grob.numbers[season.left]]]$grobs[[3]], 2, 1, 2, 5)
  for (j in 0:2) {
    exposure.x <- season.left+j
    res$grobs[[c(1, 5, 9)[j+1]]] <- g$grobs[[grob.numbers[exposure.x]]]$grobs[[3]]
  }
  new.grob.name <- paste0(levels(data$Season)[i], '-strip')
  g <- gtable_add_grob(g, res, t = top,  l = left[i],  b = top,  r = right[i], name = c(new.grob.name))
  new.grob.no <- grep(new.grob.name, g$layout$name)[3]
  g$grobs[[new.grob.no]]$grobs[[nrow(g$grobs[[new.grob.no]]$layout)]]$children[[3]]$children[[3]]$gp <- gpar(fontface='bold')
}
grid.newpage()
grid.draw(g)

1 个答案:

答案 0 :(得分:0)

您可以使用alpha美学来为每个Site获得不同的颜色阴影,然后手动分配所需的颜色:

data$Season <- factor(data$Season, levels=c('Spring', 'Summer', 'Autumn', 'Winter'))
data$Site <- as.factor(data$Site)
gg <- ggplot(aes(x=Site, y=Average, fill=Season), data=data)
gg <- gg + geom_bar(stat = 'identity', aes(alpha=Site))
gg <- gg + scale_alpha_manual(values=c(1, .7), guide_legend(title = 'Site'))
gg <- gg + scale_fill_manual(values=c('green', 'yellow', 'brown', 'grey'), guide_legend(title = 'Season')) # to get bars desired colors instead of ggplot's default colors
gg <- gg + theme_classic() # get white background and black axis.line for x- and y-axis
gg <- gg + geom_errorbar(aes(ymin=Average-SEM, ymax=Average+SEM), width=.3)
gg <- gg + facet_wrap(~Season*Exposure, strip.position=c('bottom'), nrow=1, drop=F)
gg <- gg + scale_y_continuous(expand = expand_scale(mult = c(0, .05))) # remove space below zero
gg <- gg + theme(axis.text.x = element_blank(),
                 axis.ticks.x = element_blank(),
                 axis.title.x = element_blank(),
                 axis.line = element_line(color='black'),
                 strip.placement = 'outside', # place x-axis above (factor-label-) strips
                 panel.spacing.x=unit(0, "lines"), # remove space between facets (for continuous x-axis)
                 panel.grid.major.x = element_blank(), # remove vertical grid lines
                 # panel.grid = element_blank(), # remove all grid lines
                 # panel.background = element_rect(fill='white'), # choose background color for plot area
                 strip.background = element_rect(fill='white', color='white')  # choose background for factor labels, color just matters for theme_classic()
)
print(gg)

enter image description here