将 barplot 图层添加到热图中 (geom_tile)
Add barplot layer to the heatmap (geom_tile)
因此,如 header 中所述,我想在我的热图中再添加 1 个可视层。
样本数据来自mpg
.
library(tidyverse) # for dplyr and ggplot2
data <- mpg %>%
mutate(quantity = 1, isnew = ifelse(year == 2008, 1, 0)) %>%
group_by(drv, class) %>%
summarise(quantity = sum(quantity), isnew = round(sum(isnew) / quantity, 2))
两个类别作为主轴 - class 和 drv。
数量只是 class 和 drv 的每个交叉路口的汽车数量。
Isnew 是新车数量的份额(2008 年与 1999 年)。
drv class quantity isnew
<chr> <chr> <dbl> <dbl>
1 4 compact 12 0.67
2 4 midsize 3 0.67
3 4 pickup 33 0.52
4 4 subcompact 4 0.00
5 4 suv 51 0.53
6 f compact 35 0.40
7 f midsize 38 0.50
8 f minivan 11 0.45
9 f subcompact 22 0.50
10 r 2seater 5 0.60
11 r subcompact 9 0.56
12 r suv 11 0.55
热图
ggplot(data, aes(class, drv)) +
geom_tile(aes(fill = quantity)) +
geom_text(aes(label = isnew))
我希望实现的是将每个部分的文本值更改为堆叠条。
每个条形图的顶部块必须是透明的,以保存热图的初始颜色。底部必须代表热图该扇区的 isnew
值。
在 Paint 8p 中绘制所需输出的一部分(只是几条,但我想要全部)
发现 in here 它可能可以通过 ggsubpolot
包完成。但它似乎已被弃用,并且 "Embed into ggplot2 graphics" 如 Grolemund github.
所述
这个情节对我来说似乎很混乱,但我忍不住想尝试一下。我们使用 geom_segment
创建条形,并使用基础 drv
因子编码将条形和文本标签放置在 y 轴上的适当位置。
ggplot(data, aes(class, drv)) +
geom_tile(aes(fill = quantity), colour="white", size=0.5) +
geom_segment(aes(y=as.numeric(as.factor(drv)) - 0.5,
yend=as.numeric(as.factor(drv)) - 0.5 + isnew,
x=class, xend=class),
colour="yellow", size=10) +
geom_text(aes(label = isnew, y=(2*as.numeric(as.factor(drv)) + isnew)/2 - 0.5),
colour="grey30", size=3) +
theme_classic()
也许使用 geom_point
尺寸美学会更好:
ggplot(data, aes(class, drv)) +
geom_tile(aes(fill = quantity), colour="white", size=0.5) +
geom_point(aes(size=isnew), shape=21, fill="white") +
scale_size_continuous(range=c(1,10)) +
theme_classic()
因此,如 header 中所述,我想在我的热图中再添加 1 个可视层。
样本数据来自mpg
.
library(tidyverse) # for dplyr and ggplot2
data <- mpg %>%
mutate(quantity = 1, isnew = ifelse(year == 2008, 1, 0)) %>%
group_by(drv, class) %>%
summarise(quantity = sum(quantity), isnew = round(sum(isnew) / quantity, 2))
两个类别作为主轴 - class 和 drv。 数量只是 class 和 drv 的每个交叉路口的汽车数量。 Isnew 是新车数量的份额(2008 年与 1999 年)。
drv class quantity isnew
<chr> <chr> <dbl> <dbl>
1 4 compact 12 0.67
2 4 midsize 3 0.67
3 4 pickup 33 0.52
4 4 subcompact 4 0.00
5 4 suv 51 0.53
6 f compact 35 0.40
7 f midsize 38 0.50
8 f minivan 11 0.45
9 f subcompact 22 0.50
10 r 2seater 5 0.60
11 r subcompact 9 0.56
12 r suv 11 0.55
热图
ggplot(data, aes(class, drv)) +
geom_tile(aes(fill = quantity)) +
geom_text(aes(label = isnew))
我希望实现的是将每个部分的文本值更改为堆叠条。
每个条形图的顶部块必须是透明的,以保存热图的初始颜色。底部必须代表热图该扇区的 isnew
值。
在 Paint 8p 中绘制所需输出的一部分(只是几条,但我想要全部)
发现 in here 它可能可以通过 ggsubpolot
包完成。但它似乎已被弃用,并且 "Embed into ggplot2 graphics" 如 Grolemund github.
这个情节对我来说似乎很混乱,但我忍不住想尝试一下。我们使用 geom_segment
创建条形,并使用基础 drv
因子编码将条形和文本标签放置在 y 轴上的适当位置。
ggplot(data, aes(class, drv)) +
geom_tile(aes(fill = quantity), colour="white", size=0.5) +
geom_segment(aes(y=as.numeric(as.factor(drv)) - 0.5,
yend=as.numeric(as.factor(drv)) - 0.5 + isnew,
x=class, xend=class),
colour="yellow", size=10) +
geom_text(aes(label = isnew, y=(2*as.numeric(as.factor(drv)) + isnew)/2 - 0.5),
colour="grey30", size=3) +
theme_classic()
也许使用 geom_point
尺寸美学会更好:
ggplot(data, aes(class, drv)) +
geom_tile(aes(fill = quantity), colour="white", size=0.5) +
geom_point(aes(size=isnew), shape=21, fill="white") +
scale_size_continuous(range=c(1,10)) +
theme_classic()