将边际总数添加到 R 中的 ggplot 热图

Boo*_*iro 2 r heatmap ggplot2

我想将总和和行总数添加到我的热图中,并努力在其他帖子中使用已经建议的方法来实现:ggplot2:汇总行和列的独立连续填充

上面帖子的问题是,我不明白创建总计(行、列)的代码。虽然它被标记为“#create the summary row & column”,但我不明白。

所以如果...... 1. ...有人可以帮助我并告诉我一个(简单)的方法来参考我发布的代码(下面)以及2. ...如果行和列总数可以有单独的色阶。

我试过这个...

# create sample
scen <- 1:32
ls <- rep(1:7, length(scen))
df <- data.frame(Landscape = ls, Scenario = scen)
df$SoP <- sample(seq(-0.070, 0.070, by = 0.01),replace=T, nrow(df))
df$Landscape_Name <- LETTERS[1:7]

# create heatmap
library(ggplot2)
df.diff <- ggplot(df, aes(x = Landscape_Name, y = Scenario)) +
  geom_tile(aes(fill = SoP)) +
  geom_text(size = 3, aes(label = round(SoP,2))) + #displays cell values
  scale_fill_gradient2(low = "gold", #colors
                       mid = "white",
                       high = "grey",
                       midpoint = 0) +
  theme(panel.grid.major.x=element_blank(), #no gridlines
        panel.grid.minor.x=element_blank(), 
        panel.grid.major.y=element_blank(), 
        panel.grid.minor.y=element_blank(),
        panel.background=element_rect(fill="white"),
        axis.text.x = element_text(angle=0, hjust = 0.5,vjust=0.5, size = 8,face = NULL),
        axis.text.y = element_text(size = 8,face = NULL),
        plot.title = element_text(size=10,face="bold")) +
  ggtitle("Treatment efficiency") + 
  theme(legend.title=element_text(face="bold", size=8)) + 
  scale_x_discrete(name="Landscape", position = "top") +
  scale_y_discrete(name="Scenario") +
  labs(fill="SoP")
print(df.diff)
Run Code Online (Sandbox Code Playgroud)

非常感谢你的帮助!

DS_*_*UNI 7

让我们看看我是否可以解释您引用的帖子中的答案,即ggplot2:汇总行和列的独立连续填充

首先是几个注意事项:

  • y轴上绘制一个数字向量,这被认为是一个连续的比例,这就是为什么当你运行时轴标签消失scale_y_discrete,而绘图工作正常,一旦我们决定向轴添加一个新值(即, Total) 这会引起问题,这就是为什么我认为Scenario应该是字符向量。
  • 将列转换Scenario为字符串 usingas.character会弄乱对值的排序,例如尝试运行sort(as.character(1:20)),这可以通过使用 2 位数字(01、02、03,.....)来避免,这就是我在那里所做的
  • 在上面提到的答案中,总数与原始 df 绑定,但是我将它们用作外部数据以使其更易于理解(或者至少我认为这样更容易)

所以我们开始:

library(ggplot2)
library(dplyr)

# pad numbers with zeros to get 2 digit numbers, this will be a string
scen <- sprintf('%02d', 1:32)
ls <- rep(1:7, length(scen))
df <- data.frame(Landscape = ls, Scenario = scen)
df$SoP <- sample(seq(-0.070, 0.070, by = 0.01),replace=T, nrow(df))
df$Landscape_Name <- LETTERS[1:7]

# create the main plot, and take a look at it
df.diff <- ggplot(df, aes(x = Landscape_Name, y = Scenario)) +
  geom_tile(aes(fill = SoP)) +
  geom_text(size = 3, aes(label = round(SoP,2))) + #displays cell values
  scale_fill_gradient2(low = "gold", #colors
                       mid = "white",
                       high = "grey",
                       midpoint = 0) 

df.diff
Run Code Online (Sandbox Code Playgroud)

现在我们想要的数据允许我们添加一个额外的类别Landscape_Name和一个额外的类别到Scenario,这样:

  • 添加到Landscape_Name(水平总和)的类别是SoP每个的总和Scenario,并且
  • 添加到Scenario(垂直总和)的类别是SoP每个类别的总和Landscape_Name

基本上我们需要group_bysum

h_total <- df %>% 
  group_by(Scenario) %>% 
  summarise(SoP = sum(SoP)) %>% 
  mutate(Landscape_Name = 'Total')


v_total <- df %>% 
  group_by(Landscape_Name) %>% 
  summarise(SoP = sum(SoP)) %>% 
  mutate(Scenario = 'Total')
Run Code Online (Sandbox Code Playgroud)

现在我们可以将分组数据添加到原始绘图中,使用geom_point,因为我们在新数据中使用了相同的列名,因此xy美学将从原始绘图中继承,并且与我们使用的原始绘图具有不同的配色方案color(不是fill),它适用于所选形状。

如果您还需要总计的单元格值,则还必须为这些值添加图层

p <- df.diff + 
  geom_point(data = h_total, 
             aes(color = SoP), 
             size = 10, 
             shape = 19) +
  geom_point(data = v_total, 
             aes(color = SoP), 
             size = 10, 
             shape = 19) +
  scale_color_gradient2(low = "red", #colors
                        mid = "white",
                        high = "grey",
                        midpoint = 0) +
  geom_text(data = h_total, size = 3, aes(label = round(SoP,2))) +
  geom_text(data = v_total, size = 3, aes(label = round(SoP,2)))

p
Run Code Online (Sandbox Code Playgroud)

最后添加主题自定义、标题、轴和图例标签

p  +
  theme(panel.grid.major.x=element_blank(), #no gridlines
        panel.grid.minor.x=element_blank(), 
        panel.grid.major.y=element_blank(), 
        panel.grid.minor.y=element_blank(),
        panel.background=element_rect(fill="white"),
        axis.text.x = element_text(angle=0, hjust = 0.5,vjust=0.5, size = 8,face = NULL),
        axis.text.y = element_text(size = 8,face = NULL),
        plot.title = element_text(size=10,face="bold"),
        legend.title=element_text(face="bold", size=8))  + 
  scale_x_discrete(name="Landscape", position = "top") +
  scale_y_discrete(name="Scenario", 
                  # if you want the total to be at the bottom instead of at the top, 
                  # you can set the limits of y  with the reversed order of the categories 
                  limits = rev(c(unique(as.character(df$Scenario)), 'Total'))) + 
  # you can here change the y/x ratio 
  coord_fixed(ratio = 0.4) +
  labs(fill="SoP", color ="SoP Total") +
  ggtitle("Treatment efficiency")
Run Code Online (Sandbox Code Playgroud)

我终于用 ggsave(' PATH/TO/plot.jpeg', width =20, height = 40, units = 'cm')

这是输出

在此处输入图片说明