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

# 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,color = "black")

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

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

dark crisp clear text on graph

解决方法

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

将您的最后一行更改为

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

得到这个结果

enter image description here

另一种选择是覆盖alpha

... +
geom_text(aes(y = 1 - lab.y.mid,alpha = 1)

相关文章

HTML代码中要想改变字体颜色,常常需要使用CSS样式表。CSS是...
HTML代码如何让字体盖住图片呢?需要使用CSS的position属性及...
HTML代码字体设置 在HTML中,我们可以使用标签来设置网页中的...
在网页设计中,HTML代码的字体和字号选择是非常重要的一个环...
HTML(Hypertext Markup Language,超文本标记语言)是一种用...
外链是指在一个网页中添加一个指向其他网站的链接,用户可以...