Вам необходимо создать фиктивную переменную для оси X.Затем используйте geom_col
, который похож на geom_bar(stat = "identity")
, чтобы построить столбчатую диаграмму с накоплением + geom_text
, чтобы поместить текст на полосу.
Показанный вами график использовал theme_economist
из пакета ggthemes
.
library(tidyverse)
Ancestry <- data.frame(Race = c("European", "African American", "Asian", "Hispanic", "Other"),
Proportion = c(40, 30, 10, 15, 5))
Ancestry <- Ancestry %>%
mutate(Year = "2006")
ggplot(Ancestry, aes(x = Year, y = Proportion, fill = Race)) +
geom_col() +
geom_text(aes(label = paste0(Proportion, "%")),
position = position_stack(vjust = 0.5)) +
scale_fill_brewer(palette = "Set2") +
theme_minimal(base_size = 16) +
ylab("Percentage") +
xlab(NULL)
![](https://i.imgur.com/T2HZZFm.png)
library(ggthemes)
ggplot(Ancestry, aes(x = Year, y = Proportion, fill = Race)) +
geom_col() +
geom_text(aes(label = paste0(Proportion, "%")),
position = position_stack(vjust = 0.5)) +
theme_economist(base_size = 14) +
scale_fill_economist() +
theme(legend.position = "right",
legend.title = element_blank()) +
theme(axis.title.y = element_text(margin = margin(r = 20))) +
ylab("Percentage") +
xlab(NULL)
![](https://i.imgur.com/FbuU6sa.png)
Создано в 2018-08-26 пакетом Представить (v0.2.0.9000).