【问题标题】:How can I make bar chart label colours conditional on the colour of the bar itself?如何使条形图标签颜色以条形本身的颜色为条件?
【发布时间】:2020-03-22 23:02:54
【问题描述】:

根据发布在其他地方 (ggplot2: text color based on background contrast) 的解决方案,我创建了一个简单的条形图,其标签应根据条形的颜色显示为白色或黑色(即黑色条的白色文本)。

首先,我不确定代码是否按预期工作,因为白色文本被用于标记浅色条。其次,图例包含一些我用来分配颜色的代码。

您对解决这两个问题有什么建议吗?我已附上图表的图像以供参考。

# Create a table of the proportions for use when creating the plot.
plotdata <- treatment_dataset %>%
  group_by(STAGE, TREATMENT_GROUP) %>%
  summarize(n = n()) %>% 
  mutate(pct = n/sum(n),
         percentages = scales::percent(pct))
plotdata

# Plot the data from the table above
ggplot(plotdata, 
       aes(x = STAGE, 
           y = pct, 
           fill = TREATMENT_GROUP)) + 
  geom_bar(stat = "identity",
           position = "fill") +
  labs(y = "Proportion of tumours",
       x = "Tumour stage at diagnosis",
       fill = "Treatment group"
       )  +
  scale_y_continuous(labels = scales::percent) -> treatment_by_stage

# Apply PHE formatting.
treatment_by_stage + theme_phe("phe") +
  scale_fill_phe(name = "Treatment group",
                 theme = "phe") +
  geom_text(aes(label = ifelse(pct < 0.05, NA, percentages), color = plotdata$pct > 0.01), 
            size = 3, 
            position = position_stack(vjust = 0.5)
            ) +
  scale_color_manual(values = c("black", "white"))

【问题讨论】:

标签: r ggplot2 bar-chart


【解决方案1】:

您的情况与@Tjebo 提到的帖子有点不同。在您的情况下,您知道需要用白色文本标记哪些颜色,哪些用黑色标记。这是将文本颜色分配给正确的组(治疗组)的情况。

首先我们模拟你的数据:

library(tidyverse)
library(RColorBrewer)

#simulate data

set.seed(100)
df <- data.frame(
STAGE = rep(LETTERS[1:4],each=5),
n=sample(1:100,20),
TREATMENT_GROUP=rep(paste("T",1:5,sep=""),4))

我们像您一样制作用于绘图的数据,注意我在这里设置了 NA。

# make plotdata with percentages

plotdata <- df %>%
  group_by(STAGE) %>%
  mutate(pct = n/sum(n),
         percentages = scales::percent(pct))

# set NA to percentages here
# not very sure the ifelse you had previously works
plotdata$percentages[plotdata$pct < 0.05]<-NA

现在我们可以手动定义颜色。我找不到您使用的配色方案,所以我使用了 RColorBrewer 的“Oranges”。

COLS=brewer.pal(5,"Oranges")
names(COLS) = levels(plotdata$TREATMENT_GROUP)
# here the last 2 colors are dark, so we just set 
# first 3 to black and last 2 to white
TEXTCOLS = c("black","black","black","white","white")
names(TEXTCOLS) = levels(plotdata$TREATMENT_GROUP)

制作初步情节

treatment_by_stage =
ggplot(plotdata, 
       aes(x = STAGE, 
           y = pct, 
           fill = TREATMENT_GROUP)) + 
  geom_bar(stat = "identity") +
  labs(y = "Proportion of tumours",
       x = "Tumour stage at diagnosis",
       fill = "Treatment group"
       )  +
  scale_y_continuous(labels = scales::percent)+
  scale_fill_manual(values=COLS)

现在我们添加标签:

treatment_by_stage + 
geom_text(aes(label=percentages,
          color = TREATMENT_GROUP), 
            size = 3, show.legend=FALSE,
            position = position_stack(vjust = 0.2)
            ) +
  scale_color_manual(values = TEXTCOLS)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多