# Create the data frame
library(tidyverse)
dat <- read.table(text = "A B C
1 23 234 324
2 34 534 120
3 56 324 124
4 34 234 124
5 123 534 654",
sep = "",
header = TRUE) %>%
gather(key = "variable", value = "value") %>%
group_by(variable) %>%
mutate(ind = as.factor(rep(1:5)),
perc = value / sum(value)) %>%
arrange(variable, -perc) %>%
mutate(ordering = row_number()) %>%
mutate(lab.y = cumsum(perc),
lab.y.mid = lab.y - (perc / 2))
# Toggle whether red is on top/bottom with '1L' or '-1L'
red <- 1L
n_ord <- length(unique(dat$ordering))
fill_scale <- c("darkred", rep("black", n_ord - 1L)) %>%
setNames(red * seq(n_ord))
alpha_scale <- c(0.5, rep(0.3, n_ord - 1L)) %>%
setNames(red * seq(n_ord))
# Plot the data
ggplot(dat, aes(variable,
perc,
fill = factor(red * ordering),
alpha = factor(red * ordering))) +
geom_col(color = "white", size = 1.5) +
scale_fill_manual(guide = "none", values = fill_scale) +
scale_alpha_manual(guide = "none", values = alpha_scale) +
facet_grid(~ variable, scales = "free_x") +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) +
geom_text(aes(y = 1 - lab.y.mid, label = ind), color = "black")
我一直认为ggplot会按行顺序逐行绘制事物。我的ggplot的最后一行是:
geom_text(aes(y = 1 - lab.y.mid, label = ind), color = "black")
但是看来ggplot“ did”是最后一件事。如果看上面的图,您会发现我的文字标签非常模糊。文字要么在绘图的某些部分后面,要么继承了某种类型的Alpha级别,或者发生了我没想到的其他事情。
如何使文本变暗(通常是正常的)?像下面这样的情节。