get legend for two different data sets in ggplot - r

I am trying to create a legend from two different data sets. An example as below
ggplot(data = mtcars,aes(x = mpg, y = wt))+
geom_point(aes(colour = drat))+
geom_line(aes(colour = drat))+
geom_point(data = iris, aes(x = Sepal.Width, y = Petal.Length))+
geom_line(data = iris, aes(x = Sepal.Width, y = Petal.Length),linetype = "dashed")+
scale_color_gradientn(colours=c('red','yellow','green'))
In the first data (mtcars) the color is according to a certain column value (drat). But in the second data (iris) it is just x and y points. How can I get line as the two lines as legend in addition to the color legend which is already present.

Easiest way is to pass names to linetype within the aes() calls. The names become the legend labels. You can then use scale_linetype_manual to set linetypes there:
library(ggplot2)
ggplot(data = mtcars,aes(x = mpg, y = wt))+
geom_point(aes(colour = drat))+
geom_line(aes(colour = drat, linetype = "Cars"))+
geom_point(data = iris, aes(x = Sepal.Width, y = Petal.Length))+
geom_line(data = iris, aes(x = Sepal.Width, y = Petal.Length,
linetype = "Iris"))+
scale_color_gradientn(colours=c('red','yellow','green')) +
scale_linetype_manual(values = c(
"Cars" = "solid",
"Iris" = "dotted"
))

Related

R ggplot geom_label rotate label

I have a plot with labels. I wanted the bounding background box on the label so I switched from using geom_text to geom_label. When I was using the geom_text option I had my angle = 90. Is there a way to do that with geom_label? Or an alternative method to labeling with a background?
Here is some sample code to play with:
ggplot(data = iris,
aes(x = Sepal.Width, y = Petal.Length)) + geom_point(size = 10)+
geom_label(data = iris,
aes(x = Sepal.Width, y = Petal.Length, label = Species),alpha=0.5)
According to the docs
Currently geom_label() does not support the check_overlap argument or the angle aesthetic.
An alternative would be to use ggtext::geom_richtext:
library(ggplot2)
ggplot(
data = iris,
aes(x = Sepal.Width, y = Petal.Length)
) +
geom_point(size = 10) +
ggtext::geom_richtext(aes(label = Species), alpha = 0.5, angle = 90)

How to add a legend manually by ggplots?

For a task I have to make a plot with ggplots with a correct legend.
Well my plot is good, but my legend is not as I want.
I want in my legend a specifing that the triangle is for Sepal, and the plus for Petal.
Can someone help me?
Below is my script.
ggplot(data=iris)+
geom_point(aes(x = Sepal.Length, y = Sepal.Width, color=group), shape=2)+
geom_point(aes(x = Petal.Length, y = Petal.Width, color=group), shape=3)+
xlab("lengte (cm)")+
ylab("breedte (cm)")+
theme(legend.justification=c("left","top"))
I had to make a group variable, but here is an example:
iris$group <- as.factor(sample(c(0,1), nrow(iris), replace=TRUE))
ggplot(data=iris)+
geom_point(aes(x = Sepal.Length, y = Sepal.Width, color=group, shape="Sepal"))+
geom_point(aes(x = Petal.Length, y = Petal.Width, color=group, shape="Petal"))+
xlab("lengte (cm)")+
ylab("breedte (cm)")+
theme(legend.justification=c("left","top"))+
scale_shape_manual(values = c(2,3))

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")

How do i manually add a legend to a ggplot and geom_point?

I am plotting 2 sets of data on the same plot using ggplot. I have specified the colour for each data set, but there is no legend that comes out when the dot plot is generated.
What can i do to manually add a legend?
# Create an index to hold values of m from 1 to 100
m_index <- (1:100)
data_frame_50 <- data(prob_max_abs_cor_50)
data_frame_20 <- data.frame(prob_max_abs_cor_20)
library(ggplot2)
plot1 <- ggplot(data_frame_50, mapping = aes(x = m_index,
y = prob_max_abs_cor_50),
colour = 'red') +
geom_point() +
ggplot(data_frame_20, mapping = aes(x = m_index,
y = prob_max_abs_cor_20),
colour = 'blue') +
geom_point()
plot1 + labs(x = " Values of m ",
y = " Maximum Absolute Correlation ",
title = "Dot plot of probability")
First, I would suggest neatening your ggplot code a little. This is equivalent to your posted code;
ggplot() +
geom_point(data = data_frame_50, aes(x = m_index, y = prob_max_abs_cor_50,
colour = 'red')) +
geom_point(data = data_frame_20, aes(x = m_index, y = prob_max_abs_cor_20,
colour = 'blue')) +
labs(x = " Values of m ", y = " Maximum Absolute Correlation ",
title = "Dot plot of probability")
You won't get a legend here, because you are plotting different datasets with only one category in each. You need to have a single dataset with a column grouping your data (i.e. 20 or 50). So using some example data, this is the equivalent of what you are plotting and ggplot won't provide a legend;
ggplot() +
geom_point(data = iris, aes(x = Sepal.Length, y = Petal.Width), colour = 'red') +
geom_point(data = iris, aes(x = Sepal.Length, y = Petal.Length), colour = 'blue')
If you want to colour by category, include a colour argument inside the aes call;
ggplot() +
geom_point(data = iris, aes(x = Sepal.Length, y = Petal.Width,
colour = factor(Species)))
Have a look at the iris dataset to get a sense of how you need to shape your data. It's hard to give precise advice, because you haven't provided an idea of what your data look like, but something like this might work;
df.20 <- data.frame("m" = 1:100, "Group" = 20, "Numbers" = prob_max_abs_cor_20)
df.50 <- data.frame("m" = 1:100, "Group" = 50, "Numbers" = prob_max_abs_cor_50)
df.All <- rbind(df.20, df.50)

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"))))

Resources