I am trying to plot a scatter plot with 22 variables, so they all need to have different markers. I thought of repeating some shapes and colors from RColorBrew, and everything works fine, except for the legend that does not update with the colors I selected (please see image below). I also attached a working example below. What could I possibly be doing wrong?
#!/usr/bin/env Rscript
library(ggplot2)
library(ggpubr)
library(RColorBrewer)
theme_set(
theme_pubr()
)
data <- data.frame(
x = c(1:22),
y = as.factor(c(1:22))
)
shapes <- rep(15:18, 6)
colors <- rep(brewer.pal(n = 11, name = "Paired"), 2)
plot <- ggplot(data, aes(x=x, y=y, group=y, size=9, color=colors))
geom_point(aes(shape=y))
scale_shape_manual(values=shapes)
scale_size(guide="none")
guides(fill="none", color="none")
plot <- plot theme(panel.grid.major=element_line(colour="gray", size=0.2),
panel.grid.minor=element_line(colour="gray", size=0.2))
print(plot)

CodePudding user response:
There are two issues:
I you want to have your colors make use of
scale_color_manualand mapyon thecoloraes as you did with shapeThe reason your legend does not get colored is that you have set
guides(color = "none")
library(ggplot2)
library(ggpubr)
library(RColorBrewer)
theme_set(
theme_pubr()
)
data <- data.frame(
x = c(1:22),
y = as.factor(c(1:22))
)
shapes <- rep(15:18, 6)
colors <- rep(brewer.pal(n = 11, name = "Paired"), 2)
plot <- ggplot(data, aes(x = x, y = y, group = y, size = 9))
geom_point(aes(shape = y, color = y))
scale_shape_manual(values = shapes)
scale_color_manual(values = colors)
scale_size(guide = "none")
guides(fill = "none")
plot theme(
panel.grid.major = element_line(colour = "gray", size = 0.2),
panel.grid.minor = element_line(colour = "gray", size = 0.2)
)

CodePudding user response:
I found a number of things going on. I broke them down one by one here:
- You're including
colorsin theaesbut it's not a part of the data, it's just the color palette, so that shouldn't be included here.aesis telling ggplot what data to show, not how to format it. geom_pointneeds both a color and shapeaesargument so that it can combine them and later tell the legend how it has done so.- To control the color palette used, I added
scale_color_manual, similar to how you were already usingscale_shape_manualto manually adjust formatting - Remove the
guidesline at the end. by settingcolor = "none"it's blocking color from being added to the legend.
Try the segment of code below.
data <- data.frame(
x = c(1:22),
y = as.factor(c(1:22))
)
shapes <- rep(15:18, 6)
colors <- rep(brewer.pal(n = 11, name = "Paired"), 2)
#OLD: plot <- ggplot(data, aes(x=x, y=y, group=y, size=9, color=colors))
plot <- ggplot(data, aes(x=x, y=y, group=y, size=9))
#OLD: geom_point(aes(shape=y))
geom_point(aes(shape=y, color=y))
scale_shape_manual(values=shapes)
#NEW LINE
scale_color_manual(values = colors)
scale_size(guide="none")
# REMOVED guides(fill="none", color="none")
plot <- plot theme(panel.grid.major=element_line(colour="gray", size=0.2),
panel.grid.minor=element_line(colour="gray", size=0.2))
print(plot)
