Jared Brewer
Jared Brewer

Reputation: 164

ggrepel label fill color questions

I'm working with ggplot2 for the first time, and I'm having trouble making the colors of the labels I created with ggrepel change dynamically. Currently, my code looks like this:

    ggplot(tstat) + 
        geom_point(aes(Mu, Sigma),size = 5, color = 'black') + 
        geom_label_repel(aes(Mu, Sigma, label = VarNames, fill = factor(Hemisphere)), fontface = 'bold', color = 'white',
               box.padding = unit(0.25, 'lines'),point.padding = unit(0.5, 'lines')) + 
        geom_rangeframe() + 
        theme_tufte() + 
        xlab(expression(paste(mu, "*"))) + 
        ylab(expression(sigma)) + 
        theme(axis.title.x = element_text(vjust=-0.5), axis.title.y = element_text(vjust=1.5)) + 
        ggtitle("Model Sensitivity by Hemisphere")

In general, this works pretty well, except I strongly dislike the toothpaste green color it gives me for one of the two factors plotted. I want to dictate the specific colors of that fill = factor(Hemisphere)) line, but I don't know how.

I have already tried using the scale_colours_manual function, but when I include it within the geom_label_repel(.....) paratheses in line 3, the program complains that "ggplot2 doesn't know how to deal with data of class ScaleDiscrete/Scale/ggproto", and when I place the scale_colours_manual line outside of line 3, it has no effect at all, as in this example, which produced an identical plot to the one above:

    ggplot(tstat) + 
        geom_point(aes(Mu, Sigma),size = 5, color = 'black') + 
        scale_colour_manual(values = c('blue', 'red')) + 
        geom_label_repel(aes(Mu, Sigma, label = VarNames, fill = factor(Hemisphere)), fontface = 'bold', color = 'white',
               box.padding = unit(0.25, 'lines'),point.padding = unit(0.5, 'lines')) + 
        geom_rangeframe() + 
        theme_tufte() + 
        xlab(expression(paste(mu, "*"))) + 
        ylab(expression(sigma)) + 
        theme(axis.title.x = element_text(vjust=-0.5), axis.title.y = element_text(vjust=1.5)) + 
        ggtitle("Model Sensitivity by Hemisphere")

I know there has to be a way to do this, but I'm at a loss. Thanks for any help you've got!

EDIT: At request, I've attached a dput() of tstat. Not a big data frame.

    structure(list(VarNames = structure(c(4L, 1L, 3L, 2L, 5L, 6L, 
    4L, 1L, 3L, 2L, 5L, 6L), .Label = c("Dry Deposition", "MEGAN Acetone", 
    "MEGAN Terpenes", "Monoterpene Yield", "Ocean", "Photolysis"), class = "factor"), 
    Mu = c(2703.09, 8066.01, 6566.6, 19741.7, 5809.6, 14231.8, 1493.56, 3067.54, 3631.32, 9951.06, 8748.95, 7967.93),
    Sigma = c(3478.28, 8883.23, 7276.49, 18454.4, 6218.8, 14989.7, 1925.14, 3410.27, 4017.64, 9289.57, 9354.64, 8403.1),
    Hemisphere = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L),
    .Label = c("Northern", "Southern"), class = "factor")), 
    .Names = c("VarNames", "Mu", "Sigma", "Hemisphere"), 
    class = "data.frame", row.names = c(NA, -12L))

Upvotes: 1

Views: 6806

Answers (1)

lukeA
lukeA

Reputation: 54247

You can use scale_fill_manual:

tstat <- structure(list(VarNames = structure(c(4L, 1L, 3L, 2L, 5L, 6L, 
    4L, 1L, 3L, 2L, 5L, 6L), .Label = c("Dry Deposition", "MEGAN Acetone", 
    "MEGAN Terpenes", "Monoterpene Yield", "Ocean", "Photolysis"), class = "factor"), 
    Mu = c(2703.09, 8066.01, 6566.6, 19741.7, 5809.6, 14231.8, 1493.56, 3067.54, 3631.32, 9951.06, 8748.95, 7967.93),
    Sigma = c(3478.28, 8883.23, 7276.49, 18454.4, 6218.8, 14989.7, 1925.14, 3410.27, 4017.64, 9289.57, 9354.64, 8403.1),
    Hemisphere = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L),
    .Label = c("Northern", "Southern"), class = "factor")), 
    .Names = c("VarNames", "Mu", "Sigma", "Hemisphere"), 
    class = "data.frame", row.names = c(NA, -12L))
library(ggplot2)
library(ggrepel)
library(ggthemes)
ggplot(tstat) + 
  geom_point(aes(Mu, Sigma),size = 5, color = 'black') + 
  geom_label_repel(aes(Mu, Sigma, label = VarNames, fill = factor(Hemisphere)), fontface = 'bold', color = 'white',
         box.padding = unit(0.25, 'lines'),point.padding = unit(0.5, 'lines')) + 
  geom_rangeframe() + 
  theme_tufte() + 
  xlab(expression(paste(mu, "*"))) + 
  ylab(expression(sigma)) + 
  theme(axis.title.x = element_text(vjust=-0.5), axis.title.y = element_text(vjust=1.5)) + 
  ggtitle("Model Sensitivity by Hemisphere") + 
  scale_fill_manual(values = setNames(c("lightblue", "darkgreen"), levels(tstat$Hemisphere)))

enter image description here

Upvotes: 4

Related Questions