簡體   English   中英

根據類別繪制手動圖例ggplot2

[英]Plotting manual legend ggplot2 based on categories

我正在使用ggplot2中的geom_tile函數構建數據集的可視化。 我對它幾乎感到滿意,但是我想添加一個特定的圖例,但我還沒有找到合適的方法。 我將在后期解釋我的意思。

這是我目前正在使用的代碼,它可能是一個巨大的混亂因為我不精通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軸上的每個值代表不同的研究,具有不同的實驗條件。 我嘗試使用此線程對它們進行分組,並且每個組都獲得不同的顏色。

現在我理想的是,圖例顯示每個類別的相應純色,而不是基於每個單元格的值的整體漸變。 當然,它不需要是使用ggplot2生成的圖例,只要有技巧,任何其他方法都可以接受。

提前致謝!

你可以包括shape = Category (它不會改變繪圖的外觀,因為它在geom_tile沒有效果),然后使用override.aes來獲取每個Category的顏色。 如果您只需要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"))))

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM