Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: / ky.emrah
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Avoid combined legends associated with different but grouped datasets
I am creating a scatter plot using ggplot2 with three groups. I am then adding an additional layer to plot additional data. In both cases, I map the data to group, but in the second case I want to legend to be dissociated from groups and just be a binary black and white linetype legend. Right now the legend merges everything.
ggplot2
Based on other solutions, I've tried scale_linetype_identity() and scale_linetype_manual(), to no avail.
scale_linetype_identity()
scale_linetype_manual()
Reprex:
library(ggplot2)
Data 1
data1 - data.frame(
x = c(1, 2, 3),
y = c(10, 20, 20, 10, 15, 15, 10, 10, 10),
group = factor(c(rep("A", 3), rep("B", 3), rep("C", 3)),
levels = c("A", "B", "C")))
Plot 1
p - ggplot(data1, aes(x = x, y = y, color = group, shape = group)) +
geom_line(show.legend = FALSE) +
geom_point()
p
library(ggplot2)
Data 1
data1 - data.frame(
x = c(1, 2, 3),
y = c(10, 20, 20, 10, 15, 15, 10, 10, 10),
group = factor(c(rep("A", 3), rep("B", 3), rep("C", 3)),
levels = c("A", "B", "C")))
Plot 1
p - ggplot(data1, aes(x = x, y = y, color = group, shape = group)) +
geom_line(show.legend = FALSE) +
geom_point()
p
Data 2
data2 - data.frame(
x = c(1, 2),
y = c(10, 19.3, 10, 12.5, 10, 7.5),
group = data1$group[-c(3, 6, 9)],
condition = c("Y", "Z")
)
p +
geom_line(data = data2, aes(x = x, y = y, color = group),
linetype = "dotted", show.legend = TRUE) +
geom_point(data = data2)
Data 2
data2 - data.frame(
x = c(1, 2),
y = c(10, 19.3, 10, 12.5, 10, 7.5),
group = data1$group[-c(3, 6, 9)],
condition = c("Y", "Z")
)
p +
geom_line(data = data2, aes(x = x, y = y, color = group),
linetype = "dotted", show.legend = TRUE) +
geom_point(data = data2)
Created on 2024-09-15 with reprex v2.1.1
Created on 2024-09-15 with reprex v2.1.1
reprex v2.1.1
And I'm trying to get this legend:
But without the linetype for the group legend, only the shape.
Tags: r,ggplot2,legendSource of the question:
https://stackoverflow.com/questions/7...
Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/
Информация по комментариям в разработке