我想将总和和行总计添加到我的热图,并在已经建议的实现方法中挣扎,例如:ggplot2: Independent Continuous Fill for 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)
非常感谢您的帮助!
答案 0 :(得分:3)
让我们看看我能否在您引用的帖子(即ggplot2: Independent Continuous Fill for Summary Row & Column
中解释答案)首先要注意的几点:
y
轴上,您正在绘制一个数字矢量,这被认为是一个连续的比例尺,这就是为什么在运行scale_y_discrete
时,当标签工作正常时,轴标签消失了的原因,一旦我们决定在轴上添加新值(即Total
),就会引起问题,这就是为什么我认为Scenario
应该是字符向量的原因。Scenario
将列as.character
转换为字符串会弄乱值的排序,例如尝试运行sort(as.character(1:20))
,可以使用2位数字来避免这种情况(01、02, 03,.....),这就是我在那里做的所以我们开始:
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
现在,我们需要允许我们为Landscape_Name
添加一个额外类别和为Scenario
添加一个额外类别的数据,例如:
Landscape_Name
的类别(水平总和)是每个SoP
的所有Scenario
的总和,并且Scenario
的类别(垂直总和)是每个SoP
的所有Landscape_Name
的总和基本上我们需要group_by
和sum
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')
现在,我们可以使用geom_point
将分组数据添加到原始图中,因为我们在新数据中使用了相同的列名,因此x
和y
的美感将被继承与原始图不同,并且要使用与原始图不同的配色方案,我们使用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
最后添加主题自定义项,标题以及轴和图例标签
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")
我终于用ggsave(' PATH/TO/plot.jpeg', width =20, height = 40, units = 'cm')
这就是输出