根据类别绘制手动图例ggplot2

时间:2016-06-13 11:14:27

标签: r plot ggplot2 data-visualization legend

我使用ggplot2中的geom_tile函数构建dataset的可视化。我对它几乎感到满意,但是我想添加一个特定的传说,但我还没有找到合适的方法。我将在后期解释我的意思。

这是我目前正在使用的代码,由于我不精通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生成的图例,只要有技巧就可以接受任何其他方法。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以加入shape = Category(因为它在geom_tile中没有效果而无法更改地图的外观),然后使用override.aes获取每个地图的颜色类别。如果您只需要4个类别,则可以使用substr根据数字(第1个元素)定义填充颜色。要删除渐变图例,您可以将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"))))

enter image description here