当前位置 : 主页 > 网页制作 > html >

`geom_text()`标签非常轻/暗 – 需要它们正常/黑暗

来源:互联网 收集:自由互联 发布时间:2021-06-12
# Create the data framelibrary(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) %% mutat
# 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")

light text on plot

我一直假设ggplot按顺序绘制事物,逐行逐项.我上面的ggplot的最后一行是:

geom_text(aes(y = 1 - lab.y.mid, label = ind), color = "black")

但似乎这个命令不是ggplot“做”的最后一件事.如果你看一下上面的情节,你会发现我的文字标签非常微弱.文本要么是在情节的某些部分的后面,要么是继承了某种类型的alpha级别,或者其他正在发生的事情,我没有想到.

如何让文字变暗(就像通常那样)?如下图所示.

dark crisp clear text on graph

geom_text从ggplot()继承了alpha美学,这就是文本没有出现在“黑色”中的原因.

将您的最后一行更改为

...  + 
  geom_text(aes(x = variable, y = 1 - lab.y.mid, label = ind), inherit.aes = FALSE)

得到这个结果

enter image description here

另一种选择是覆盖alpha

... +
geom_text(aes(y = 1 - lab.y.mid, label = ind), alpha = 1)
网友评论