如何使用 geom_text 在堆叠条形图的比例尺上添加频率或数字?
How to add a frequency or number on scalebar of stacked barplot using geom_text?
我是Rstudio新手,有一些问题想请教
我想为10个站点的物种组成制作比例尺,并在比例尺内添加数字。
结果是这样的
我想把物种组成的频率数放在比例尺内。我曾尝试输入 geom_text 的代码,但结果根本不合适。
我希望有解决此问题的答案。非常感谢。
这是我的数据,也是我运行在R中的编码。
data <- as.matrix(data.frame(Bng = c(0, 0, 0, 41, 0, 9, 6, 25, 11, 2, 5, 7),
Krs = c(0, 25, 0, 82, 0, 0, 0, 0, 23, 0, 0, 0),
Bny = c(0, 0, 0, 0, 0, 0, 0, 23, 16, 0, 10, 0),
Kmb = c(1, 0, 0, 0, 20, 0, 0, 25, 8, 1, 0, 0),
Sgk = c(0, 0, 0, 18, 0, 2, 0, 11, 0, 0, 0, 0),
Lwb = c(1, 0, 2, 73, 0, 5, 0, 7, 5, 0, 0, 0),
Lws = c(0, 0, 0, 4, 0, 0, 0, 4, 0, 4, 1, 0),
Krp = c(0, 0, 0, 115, 0, 0, 2, 0, 2, 0, 0, 0),
Hrt = c(4, 0, 0, 0, 2, 22, 0, 7, 4, 2, 3, 0),
Gmb = c(0, 2, 0, 42, 2, 0, 0, 1, 6, 4, 3, 0)))
rownames(data) <- c("Cbr", "Csx", "Rax", "Hdd", "Hlv", "Mst", "Mps", "Mbr", "Rfs", "Rbn", "Rct", "Rps")
data
barplot(data)
barplot(prop.table(data, 2))```
library(reshape2)
data_long <- as.data.frame(data)
data_long$subgroup <- rownames(data_long)
data_long <- melt(data_long, id.vars = "subgroup")
library(ggplot2)
ggp <- ggplot(data_long,
aes(x = variable,
y = value,
fill = subgroup)) +
geom_bar(position = "fill", stat = "identity")+
theme_bw()+
scale_fill_grey()
ggp
ggp +
scale_y_continuous(labels = scales::percent_format())
你可以试试
library(dplyr)
data_long %>%
group_by(subgroup) %>%
mutate(key = sum(value),
value = value/sum(value)
) %>%
filter(value != 0) %>%
ggplot(aes(x = variable,
y = value,
fill = subgroup)) +
geom_bar(position = "fill", stat = "identity")+
theme_bw()+
scale_fill_grey() +
scale_y_continuous(labels = scales::percent_format()) +
geom_text(aes(label = value * key), position = position_fill(vjust = .5))
我是Rstudio新手,有一些问题想请教
我想为10个站点的物种组成制作比例尺,并在比例尺内添加数字。
结果是这样的
我想把物种组成的频率数放在比例尺内。我曾尝试输入 geom_text 的代码,但结果根本不合适。
我希望有解决此问题的答案。非常感谢。
这是我的数据,也是我运行在R中的编码。
data <- as.matrix(data.frame(Bng = c(0, 0, 0, 41, 0, 9, 6, 25, 11, 2, 5, 7),
Krs = c(0, 25, 0, 82, 0, 0, 0, 0, 23, 0, 0, 0),
Bny = c(0, 0, 0, 0, 0, 0, 0, 23, 16, 0, 10, 0),
Kmb = c(1, 0, 0, 0, 20, 0, 0, 25, 8, 1, 0, 0),
Sgk = c(0, 0, 0, 18, 0, 2, 0, 11, 0, 0, 0, 0),
Lwb = c(1, 0, 2, 73, 0, 5, 0, 7, 5, 0, 0, 0),
Lws = c(0, 0, 0, 4, 0, 0, 0, 4, 0, 4, 1, 0),
Krp = c(0, 0, 0, 115, 0, 0, 2, 0, 2, 0, 0, 0),
Hrt = c(4, 0, 0, 0, 2, 22, 0, 7, 4, 2, 3, 0),
Gmb = c(0, 2, 0, 42, 2, 0, 0, 1, 6, 4, 3, 0)))
rownames(data) <- c("Cbr", "Csx", "Rax", "Hdd", "Hlv", "Mst", "Mps", "Mbr", "Rfs", "Rbn", "Rct", "Rps")
data
barplot(data)
barplot(prop.table(data, 2))```
library(reshape2)
data_long <- as.data.frame(data)
data_long$subgroup <- rownames(data_long)
data_long <- melt(data_long, id.vars = "subgroup")
library(ggplot2)
ggp <- ggplot(data_long,
aes(x = variable,
y = value,
fill = subgroup)) +
geom_bar(position = "fill", stat = "identity")+
theme_bw()+
scale_fill_grey()
ggp
ggp +
scale_y_continuous(labels = scales::percent_format())
你可以试试
library(dplyr)
data_long %>%
group_by(subgroup) %>%
mutate(key = sum(value),
value = value/sum(value)
) %>%
filter(value != 0) %>%
ggplot(aes(x = variable,
y = value,
fill = subgroup)) +
geom_bar(position = "fill", stat = "identity")+
theme_bw()+
scale_fill_grey() +
scale_y_continuous(labels = scales::percent_format()) +
geom_text(aes(label = value * key), position = position_fill(vjust = .5))