4

I have produced a nice Likert-style plot below. I've put labels on segments to enhance clarity. However, I would like the labels to be white on dark background and black on light background. How can I accomplish that?

Likert-style plot

I tried setting the additional dummy with zeros for all variables for which I would like to have black data-labels and ones for all variables for which I'd like to have white data-labels. Then I added parameter color = dummy to geom_text_repel. Although the labels have correct color, the segments change color with a bad result.

Another (wrong) Likert-style_plot

My code below:

color_palette = c("#525199", "#9393C6", "#A8A8A8","#FFA166", 
                  "#FF6200", "#FFFFFF", "#000000")

title <- "If you were to buy a car, would you choose...?"
subtitle <- "Subtitle"

# basic plot

p <- dane_3long %>% 
  ggplot() +
  geom_segment(aes(x = pytanie, y = start, xend = pytanie, 
                   yend = start + value, colour = variable), 
               size = 8) +
  scale_color_manual("", 
                     labels = c("1 - definitely no", "2", "3", "4", "5 - definitely yes"), 
                     values = ING_Likert_palette, guide = "legend") +
  coord_flip() +
  geom_hline(yintercept = 0, color =c("#A8A8A8"))

# Below I create labels 

p <- p + geom_text(aes(label = scales::percent(round((value), digits = 2)), 
                       colour = dummy, x = pytanie, y = (start + value)-value/2), 
                   size = 3, family = "ING Me") +
  theme_classic() +
  scale_y_continuous(breaks = seq(- 75, 75, 25), limits=c(- 0.73, 0.7))

# Below is styling of the plot

p <- p + labs(title = title, 
              subtitle = subtitle, 
              caption = "Źródło: Barometr finansowy ING", 
              y= " Percent", x= "") +
  theme(plot.title = element_text(face = "bold", size = 12),
        plot.subtitle = element_text(size = 9),
        plot.caption = element_text(size = 7, face = "italic"),
        legend.position = "right",
        legend.text = element_text(family = "ING Me",
                                   size = 8,
                                   color = grey(.3)),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
        text = element_text(family = "ING Me",
                            size = 14,
                            color = grey(.3))); p
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 1
    Welcome to *Stack Overflow*. It is customary to provide example data with your question to make your issue reproducible. Please consider [*How to make a great R reproducible example*](https://stackoverflow.com/a/28481250/6574038), thanks. – jay.sf Jun 05 '18 at 12:27
  • 1
    Related: [How to control label color depending on fill darkness of bars?](https://stackoverflow.com/questions/49716005/how-to-control-label-color-depending-on-fill-darkness-of-bars) – Henrik Jun 05 '18 at 12:28
  • @Henrik Thanks for that. The problem is similar indeed. However, it considers different type of plot and the answer is not workable. It uses the color_fill_manual parameter for coloring data-labels. However, I have already used this parameter for other purpose (coloring segments). So I cannot use it again (i believe). There need to be another way... – Karol Pogorzelski Jun 05 '18 at 13:37
  • I have experimented with the order of colors in the color_palette variable. One combination seems to work quite well, namely: ING_likert_palette = c("#FF6200", "#FFA166", "#A8A8A8", "#9393C6", "#525199", "#FFFFFF", "#000000"). However, I did this by experimenting which is not a robust solution. I would love to know how the order of colors in the palette impacts the appearance of the chart. – Karol Pogorzelski Jun 05 '18 at 13:41
  • 1
    If you use `geom_rect` instead of `geom_segment`, you would be coloring the bars with a fill argument, instead of a color one, thus freeing up the `scale_color_manual` for setting the text color. Can't provide a more detailed answer until you post a sample of your data, however – camille Jun 05 '18 at 14:27
  • @camille After consideration, I think that your suggestion is the only way to go. Unfortunately I will need to redisign the code totally to change it from geom_segment to geom_rect... :-( – Karol Pogorzelski Jun 06 '18 at 06:33
  • Post your data & someone can help you out with that – camille Jun 06 '18 at 12:39

0 Answers0