Search code examples
rggplot2borderpoints

R ggplot(): geom_point() with color-palette "Greens" , how to get black point border?


I plot a scatter plot with ggplot() and use a certain color palette, namely 'Greens'. Basically I am very happy with the plot, but I would like to have a black border around each point. My code for the plot is:

p <- ggplot(data = df.dataCorrelation, aes(x = prod1, y = prod2)) +
     geom_point(aes(color = year)) +
     geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
     theme_classic() +
     theme(legend.position = "none") +
     theme(panel.background = element_blank()) +
     scale_color_brewer(palette = 'Greens') +   # customized color palette
     xlab(product1) +
     ylab(product2) +
     ggtitle("Correlation Scatter Plot (Pearson)") +
     theme(plot.title = element_text(hjust = 0.5, face = "bold"))

and provides the following graphic:

scatterPlot

I know that I can draw black borders with the command:

geom_point(aes(color = year, fill = ?), color = "black", pch = 21),

but that doesn't work with my selected color palette because I don't know what to use in fill = ?


Solution

  • Try with this.

    Here data for a reproducible example

    library(dplyr)
    
    product1 <- "product1"
    product2 <- "product2"
    df.dataCorrelation <- iris %>% rename(prod1 = Petal.Length,
                                          prod2 = Petal.Width,
                                          year  = Species)
    

    Here your code

    library(ggplot2)
    
    ggplot(data = df.dataCorrelation, aes(x = prod1, y = prod2)) +
     geom_point(aes(fill = year), colour = "black", shape = 21, size = 3) +
     geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
     theme_classic() +
     theme(legend.position = "none") +
     theme(panel.background = element_blank()) +
     scale_fill_brewer(palette = 'Greens') +   # customized color palette
     xlab(product1) +
     ylab(product2) +
     ggtitle("Correlation Scatter Plot (Pearson)") +
     theme(plot.title = element_text(hjust = 0.5, face = "bold"))
    

    enter image description here

    NOTE that:

    • I wrote colour = "black"
    • I changed scale_colour_brewer to scale_fill_brewer
    • I wrote fill = year instead of colour = year

    (I increased the size of the points only because I couldn't see the final result)