J.Sabree
J.Sabree

Reputation: 2526

how to change text color only in geom_label_repel() and not line color

I'm using geom_label_repel(), and I'm trying to change the text color of my label to white but keep the line black. When I try to put the color command outside of the aes(), it makes the line and text white:

With color argument outside of the aes()

library(dplyr)
library(ggplot2)
library(ggrepel)
 
data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
                n = c(10, 30, 59, 1),
                prop = c(10, 30, 59, 1)) %>%
    dplyr::arrange(desc(name)) %>%
    dplyr::mutate(text_y = cumsum(prop)-prop/2)

my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")


 ggplot(data, aes(x = 2, y = prop, fill = name)) +
    geom_bar(stat = "identity", color = "white") +
    coord_polar(theta = "y", start = 0)+
    geom_label_repel(aes(y = text_y, label = paste0(n, "\n", prop, "%")), force_pull = 100, nudge_x = 1, color = "white") +
    scale_fill_manual(values = my_colors) +
    theme_void() +
    xlim(.5, 2.5)

enter image description here

But, if I put it in the aes(), it's all...orange?

With color argument inside the aes()

library(dplyr)
library(ggplot2)
library(ggrepel)
 
data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
                n = c(10, 30, 59, 1),
                prop = c(10, 30, 59, 1)) %>%
    dplyr::arrange(desc(name)) %>%
    dplyr::mutate(text_y = cumsum(prop)-prop/2)

my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")

 ggplot(data, aes(x = 2, y = prop, fill = name)) +
    geom_bar(stat = "identity", color = "white") +
    coord_polar(theta = "y", start = 0)+
    geom_label_repel(aes(y = text_y, label = paste0(n, "\n", prop, "%"), color = "white"), force_pull = 100, nudge_x = 1) +
    scale_fill_manual(values = my_colors) +
    theme_void() +
    xlim(.5, 2.5)

I'd like the white text, black line, and not have the "white" legend on the side that's present in the second one. (Also, in the second one, it makes the text in the orange label totally disappear, which isn't desirable).

enter image description here

Upvotes: 1

Views: 1669

Answers (1)

J.Sabree
J.Sabree

Reputation: 2526

Figured it out! There's another argument--segement.color--that sets the line (segment) color. Both color and segment.color should be set within the geom_label_repel() function but outside its aes() function:

library(dplyr)
library(ggplot2)
library(ggrepel)

my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")

        data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
                       n = c(10, 30, 59, 1),
                       prop = c(10, 30, 59, 1)) %>%
            dplyr::arrange(desc(name)) %>%
            dplyr::mutate(text_y = cumsum(prop)-prop/2)
        
        
        ggplot(data, aes(x = 2, y = prop, fill = name)) +
            geom_bar(stat = "identity", color = "white") +
            coord_polar(theta = "y", start = 0)+
            geom_label_repel(aes(y = text_y, label = paste0(n, "\n", prop, "%")), force_pull = 100, nudge_x = 1,
                             color="white", segment.color="black") +
            scale_fill_manual(values = my_colors) +
            theme_void() +
            xlim(.5, 2.5)

enter image description here

Upvotes: 3

Related Questions