Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

ggplot2 - R: Specify a contrasting text_geom color palette to accompany a scale_fill_manual palette?

I'm trying to get a contrasting text colors. First 5 percentages need to be white, last one needs to be black:

enter image description here

library(tidyverse)

data <- tribble(
  ~Group, ~Key, ~Pct, ~OrderBy,
  "Group 1", "Very good"   , 8  , 1,
  "Group 1", "Good"        , 32 , 2,
  "Group 1", "Neutral"     , 16 , 3,
  "Group 1", "Bad"         , 9  , 4,
  "Group 1", "Very bad"    , 3  , 5,
  "Group 1", "N/A"         , 32 , 6,
  "Group 2", "Very good"   , 16 , 1,
  "Group 2", "Good"        , 20 , 2,
  "Group 2", "Neutral"     , 20 , 3,
  "Group 2", "Bad"         , 19 , 4,
  "Group 2", "Very bad"    , 3  , 5,
  "Group 2", "N/A"         , 22 , 6,
)

pal <- c("#014782","#31859C","#95B3D7","#E46C0A","#953735","#EEECE1")
pal_text <- c("#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#000000")

#
# all black text
#
ggplot(data, aes(fill=fct_reorder(Key,OrderBy,.desc=TRUE), x=Group, y=Pct)) +
  geom_bar(stat="identity", width=0.8) +
  geom_text(aes(label=paste0(Pct,"%"), x=Group,y=Pct), position = position_stack(vjust=0.5), vjust=0.5, size = 4) +
  coord_flip() +
  scale_fill_manual(values=rev(pal)) +
  scale_color_manual(guide = FALSE, values = c("black","white","white","white","white","white"), aesthetics = c("colour")) +
  theme(legend.title=element_blank()) +
  theme(axis.text.x=element_blank(), axis.title=element_blank()) +
  guides(fill = guide_legend(nrow=1,reverse=TRUE))

#
# colors are correct, but labels are now in incorrect order
#
ggplot(data, aes(fill=fct_reorder(Key,OrderBy,.desc=TRUE), x=Group, y=Pct)) +
  geom_bar(stat="identity", width=0.8) +
  geom_text(aes(label=paste0(Pct,"%"), x=Group,y=Pct, color=Key), position = position_stack(vjust=0.5), vjust=0.5, size = 4) +
  coord_flip() +
  scale_fill_manual(values=rev(pal)) +
  scale_color_manual(guide = FALSE, values = c("black","white","white","white","white","white"), aesthetics = c("colour")) +
  theme(legend.title=element_blank()) +
  theme(axis.text.x=element_blank(), axis.title=element_blank()) +
  guides(fill = guide_legend(nrow=1,reverse=TRUE))

After a lot of fiddeling I was able to get the contrast in using scale_color_manual together with specifiying color=Key in the text_geom, but this then messes up the order of the values:

enter image description here

I've been trying a lot of different approaches, but can't seem to get it right. I'm just a few days into R now, so my code might not entirely be comprehensible.

question from:https://stackoverflow.com/questions/65903230/r-specify-a-contrasting-text-geom-color-palette-to-accompany-a-scale-fill-manua

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...