根据类别绘制手动图例ggplot2

Plotting manual legend ggplot2 based on categories

我正在使用 ggplot2 中的 geom_tile 函数构建 dataset 的可视化。我几乎对它感到满意,但是我想为它添加一个特定的图例,但我还没有找到合适的方法。我稍后会在 post.

中解释我的意思。

这是我目前正在使用的代码,它可能是一团糟,因为我不精通 R 并且基本上是通过反复试验生成的。如果有人愿意清理它,请把自己打倒:-) .

#Read source data
meta <- read.csv("Dropbox/meta_censored.csv")

#import libraries  
library("ggplot2")
library("plyr")
library("reshape2")
library("scales")
library("grid")

#transform and rescale data in prep for later steps
meta.m <- melt(meta)
meta.s <- ddply(meta.m, .(variable), transform,
                rescale = scale(value))


#generate categories to sort conditions by colour
meta.s$Category <- meta.s$variable
levels(meta.s$Category) <-
  list("1_early" = c("X1", "X2"),
       "2_early" = c("X3", "X4", "X5", "X6", "X7"),
       "1_late" = c("X10", "X17"),
       "2_late" = c("X8", "X9", "X11", "X12", "X14", "X15", "X16"),
       "3_late" = "X13",
       "4_late" = c("X18", "X19"))

#define colours per category
meta.s$rescaleoffset <- meta.s$rescale + 100*(as.numeric(meta.s$Category)-1)
scalerange <- range(meta.s$rescale)
gradientends <- scalerange + rep(c(0,100,200,300,400,500), each = 2)
colorends <- c("white", "red", "white", "green", "white", "red", "white", "green", "white", "orange", "white", "purple")

#reorder by category
meta.s$variable2 <- reorder(meta.s$variable, as.numeric(meta.s$Category))

#reverse y axis labels (were z-a, now a-z)
flevels <- levels(meta.s$Param)
flevels <- rev(flevels)

#x axis annotation variables
text_early <- textGrob("Early", gp=gpar(fontsize = 5, fontface = "bold", col = "red"))
text_late <- textGrob("Late", gp=gpar(fontsize = 5, fontface = "bold", col = "red"))

#plot heatmap
p <- ggplot(meta.s, aes(variable2, Param)) +
  geom_tile(aes(fill = rescaleoffset), colour = "lightgrey") + 
  #add line to seperate early from late
  geom_vline(xintercept = 7.5) +
  scale_fill_gradientn(colours = colorends, values = rescale(gradientends)) +
  scale_x_discrete("", expand = c(0, 0)) +
  scale_y_discrete("", limits = flevels, expand = c(0, 0)) +
  theme_grey(base_size = 5) + 
  theme(legend.position = "right",
        axis.ticks = element_blank(),
        axis.text.x = element_text(angle = 270, hjust = 0, size = 5, vjust = 0, face = "bold"),
        plot.margin = unit(c(1,1,2,1), "lines")) +
  annotation_custom(text_early, xmin = 0, xmax = 8, ymin=168.5, ymax = 168.5) +
  annotation_custom(text_late, xmin = 8, xmax = 19, ymin=168.5, ymax = 168.5)

gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

基本上,我试图通过 x 轴上的每个值显示列 Param 中每个对象的值。 x 轴上的每个值代表具有不同实验条件的不同研究。我尝试使用 this thread 对它们进行分组,并且每组都有不同的颜色。

现在我最理想的是图例显示每个类别各自的纯色,而不是基于每个单元格的值的整体渐变。当然不一定需要是ggplot2生成的图例,其他方法只要能行就行

提前致谢!

您可以包含 shape = Category(这不会改变绘图的外观,因为它在 geom_tile 中没有效果),然后使用 override.aes 获取每个颜色类别。如果你只想要 4 个类别,你可以使用 substr 来定义基于数字(第一个元素)的填充颜色。为了删除渐变图例,您可以将 guide = FALSE 添加到 scale_fill_gradientn:

ggplot(meta.s, aes(variable2, Param)) +
  geom_tile(aes(fill = rescaleoffset, shape = substr(Category, 1, 1)), colour = "lightgrey", show.legend = TRUE) + 
  #add line to seperate early from late
  geom_vline(xintercept = 7.5) +
  scale_fill_gradientn(colours = colorends, values = rescale(gradientends), guide = FALSE) +
  scale_x_discrete("", expand = c(0, 0)) +
  scale_y_discrete("", limits = flevels, expand = c(0, 0)) +
  theme_grey(base_size = 5) + 
  theme(legend.position = "right",
        axis.ticks = element_blank(),
        axis.text.x = element_text(angle = 270, hjust = 0, size = 5, vjust = 0, face = "bold"),
        plot.margin = unit(c(1,1,2,1), "lines")) +
  annotation_custom(text_early, xmin = 0, xmax = 8, ymin=168.5, ymax = 168.5) +
  annotation_custom(text_late, xmin = 8, xmax = 19, ymin=168.5, ymax = 168.5) +
  guides(shape = guide_legend("Category", override.aes = list(fill = c("red", "green", "orange", "purple"))))