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:
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 = ?
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"))
NOTE that:
colour = "black"
scale_colour_brewer
to scale_fill_brewer
fill = year
instead of colour = year
(I increased the size of the points only because I couldn't see the final result)