Show legend with custom colors in ggplot2 - r

I have a column with color codes but when I use them I dont see any legend.
Example:
library(ggplot2)
iris$col = rep(c('#b580d1', '#9bb240', '#6a70d7'), each = 50)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = iris$col)) +
geom_line() +
geom_point() +
scale_color_identity()
I would like to have the same legend, but with the different colors as in:
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_line() +
geom_point()

library(ggplot2)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_line() +
geom_point() +
scale_color_manual(values= c('#b580d1', '#9bb240', '#6a70d7'))

Related

Combine and merge legends in ggplot2 with patchwork (discrete/categorical data)

I arranged 3 ggplot2 plots into a single figure by using the functionality of package patchwork. I tried to collect the legends and they appeared one next to the other. But still, they are 3 separate legends and I expected a single legend. So how can I merge the legends that contain identical values of the same factor variable into a single legend?
Notes:
And I do not want to remove the legends of separate plots by using, e.g., theme(legend.position = "none") in case some additional factor level appears. I expect patchwork specific solution.
A similar question was answered in Combine and merge legends in ggplot2 with patchwork but the data was continuous. And in my case, I have categorical data.
The code:
library(ggplot2)
library(patchwork)
iris_1 <-
ggplot(iris, aes(x = Sepal.Length, fill = Species, color = Species)) +
geom_density(alpha = 0.3, adjust = 1.5)
iris_2 <-
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point()
iris_3 <-
ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_boxplot()
(iris_1 + iris_2 + iris_3) + plot_layout(guides = "collect")
Created on 2020-10-14 by the reprex package (v0.3.0)
Update
I tried using the same aesthetic mappings (fill = Species and color = Species) as it was proposed in the comments below but it had no effect:
library(tidyverse)
library(patchwork)
iris_1 <-
ggplot(iris, aes(x = Sepal.Length, color = Species, fill = Species)) +
geom_density(alpha = 0.3, adjust = 1.5)
iris_2 <-
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, fill = Species)) +
geom_point()
iris_3 <-
ggplot(iris, aes(x = Species, y = Sepal.Width, color = Species, fill = Species)) +
geom_boxplot(color = "black")
(iris_1 + iris_2 + iris_3) + plot_layout(guides = "collect")
Created on 2020-10-14 by the reprex package (v0.3.0)
Unfortunately setting the same aes is only one condition. patchwork will merge legends only if they are identical. Therefore we have to ensure that the legends are the same for each plot. To this end I add a guides layer which makes the look of each legend the same by setting color, shape, size and alpha. Additionally we have to choose the same glyph for each geom using argument key_glyph. After these adjustments the three legends get merged into one.
library(ggplot2)
library(patchwork)
g <- guides(fill = guide_legend(override.aes = list(color = scales::hue_pal()(3),
shape = c(16, 16, 16),
size = c(1, 1, 1),
alpha = c(1, 1, 1)),))
iris_1 <-
ggplot(iris, aes(x = Sepal.Length)) +
geom_density(aes(fill = Species, color = Species), key_glyph = "point", alpha = 0.3, adjust = 1.5) +
g
iris_2 <-
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(fill = Species, color = Species), key_glyph = "point") +
g
iris_3 <-
ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_boxplot(aes(fill = Species, color = Species), key_glyph = "point") +
scale_color_manual(values = c("black", "black", "black")) +
g
(iris_1 + iris_2 + iris_3) + plot_layout(guides = "collect")

chose colors for scale_colour_colorblind() in ggthemes

I would like to chose specific colors of the colorblind_pal() from ggthemes
This works:
library(ggplot2)
library(ggthemes)
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg,
colour = factor(gear))) + facet_wrap(~am)
p + theme_igray() + scale_colour_colorblind()
Now I would like to chose specific colors of the colorblind_pal() for my plot. How can I chose them?
I tried following with no success:
my_palette <- palette(c("#000000","#F0E442","#D55E00"))
p + theme_igray() + scale_colour_colorblind(my_palette)
You could use scale_color_manual in order to manually specify the colors to use:
library(ggplot2)
library(ggthemes)
p <- ggplot(mtcars) +
geom_point(aes(x = wt, y = mpg, colour = factor(gear))) +
facet_wrap(~am) +
theme_igray() +
scale_color_manual(values = c("#000000","#F0E442","#D55E00"))
p
Since you already have the colors, you can just use scale_color_manual:
library(ggthemes)
library(ggplot2)
COLS=colorblind_pal()(8)
COLS = COLS[c(1,5,7)]
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg,
colour = factor(gear))) + facet_wrap(~am)
p + theme_igray() + scale_colour_manual(values=COLS))

Combining geom_point() legends

How do I combine the two legends into one "Species" legend in the code below?
library(ggplot2)
data(iris)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(color = "red", size = Species))
Thanks.
I would prefer visualizing the data in this way if you would like to add the information of the sample size per class.
cat_table <- table(iris$Species)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(color =Species)) +
scale_color_manual(breaks=names(cat_table), labels=paste(names(cat_table), ':', cat_table), values=rainbow(n=length(cat_table)))
Just remove "colour" out of the aesthetics. This seems to be what you are after.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(size = Species), colour = "red")
If you want to keep colour as an aesthetic, then
you can manually override the colour in the size legend guide
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(size = Species, colour = "red")) +
guides(colour = FALSE,
size=(guide_legend(override.aes =
list(colour = "red"))))

Adding an extra point in a ggplot2 graph

I have created a plot of the Sepal.Length and the Sepal.Width (using the iris dataset) with ggplot2.
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) + geom_point()
Works fine but now I would like to add a seperate point to the graph with a blue color. So for example:
df = data.frame(Sepal.Width = 5.6, Sepal.Length = 3.9)
Any thoughts on how I can accomplish this?
Add another layer:
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
geom_point() +
geom_point(aes(x=5.6, y=3.9), colour="blue")
Using annotate:
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
geom_point() +
annotate("point", x = 5.6, y = 3.9, colour = "blue")
library('ggplot2')
df = data.frame(Sepal.Width = 5.6, Sepal.Length = 3.9)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
geom_point() +
geom_point(data = df, col = 'blue')

ggplot with full data and subset(s) along x-axis

I'll use violin plots here as an example, but the question extends to many other ggplot types.
I know how to subset my data along the x-axis by a factor:
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_violin() +
geom_point(position = "jitter")
And I know how to plot only the full dataset:
ggplot(iris, aes(x = 1, y = Sepal.Length)) +
geom_violin() +
geom_point(position = "jitter")
My question is: is there a way to plot the full data AND a subset-by-factor side-by-side in the same plot? In other words, for the iris data, could I make a violin plot that has both "full data" and "setosa" along the x-axis?
This would enable a comparison of the distribution of a full dataset and a subset of that dataset. If this isn't possible, any recommendations on better way to visualise this would also be welcome :)
Thanks for any ideas!
Using:
ggplot(iris, aes(x = "All", y = Sepal.Length)) +
geom_violin() +
geom_point(aes(color="All"), position = "jitter") +
geom_violin(data=iris, aes(x = Species, y = Sepal.Length)) +
geom_point(data=iris, aes(x = Species, y = Sepal.Length, color = Species),
position = "jitter") +
scale_color_manual(values = c("black","#F8766D","#00BA38","#619CFF")) +
theme_minimal(base_size = 16) +
theme(axis.title.x = element_blank(), legend.title = element_blank())
gives:

Resources