How to merge color, line style and shape legends in ggplot
change legend title ggplot2
r ggplot combine color and shape legend
scale shape manual legend
ggplot combine color and size legend
ggplot2 color and shape same legend
ggplot custom legend
Suppose I have the following plot in ggplot:
It was generated using the code below:
x <- seq(0, 10, by = 0.2) y1 <- sin(x) y2 <- cos(x) y3 <- cos(x + pi / 4) y4 <- sin(x + pi / 4) df1 <- data.frame(x, y = y1, Type = as.factor("sin"), Method = as.factor("method1")) df2 <- data.frame(x, y = y2, Type = as.factor("cos"), Method = as.factor("method1")) df3 <- data.frame(x, y = y3, Type = as.factor("cos"), Method = as.factor("method2")) df4 <- data.frame(x, y = y4, Type = as.factor("sin"), Method = as.factor("method2")) df.merged <- rbind(df1, df2, df3, df4) ggplot(df.merged, aes(x, y, colour = interaction(Type, Method), linetype = Method, shape = Type)) + geom_line() + geom_point()
I would like to have only one legend that correctly displays the shapes, the colors and the line types (the interaction(Type, Method) legends is the closest to what I would like, but it does not have the correct shapes/line types).
I know that if I use scale_xxx_manual and I specify the same labels for all legends they will be merged, but I don't want to have to set the labels manually: if there are new Methods or Types, I don't want to have to modify my code: a want something generic.
As pointed in answers below, there are several ways to get the job done in this particular case. All proposed solutions require to manually set the legend line types and shapes, either by using
scale_xxx_manual functions or with
However, the proposed solutions still don't work in the general case: for instance, if I add a new data frame to the data set with a new "method3" Method, it does not work anymore, we have to manually add the new legend shapes and line types:
y5 <- sin(x - pi / 4) df5 <- data.frame(x, y = y5, Type = as.factor("sin"), Method = as.factor("method3")) df.merged <- rbind(df1, df2, df3, df4, df5) override.shape <- c(16, 17, 16, 17, 16) override.linetype <- c(1, 1, 3, 3, 4) g <- ggplot(df.merged, aes(x, y, colour = interaction(Type, Method), linetype = Method, shape = Type)) + geom_line() + geom_point() g <- g + guides(colour = guide_legend(override.aes = list(shape = override.shape, linetype = override.linetype))) g <- g + scale_shape(guide = FALSE) g <- g + scale_linetype(guide = FALSE) print(g)
Now the question is: how to automatically generate the
Note that the vector size is 5 because we have 5 curves, while the
interaction(Type, Method) factor has size 6 (I don't have data for the cos/method3 combination)
The R Cookbook section on Legends explains:
If you use both colour and shape, they both need to be given scale specifications. Otherwise there will be two two separate legends.
In your case you need specifications for
It was important to have the same data creating the shapes colors and lines, I combined your interaction phase by defining the column directly. Instead of
scale_linetype_discrete to create the legend, I used
scale_linetype_manual to specify the values since they will take on four different values by default.
If you would like a detailed layout of all possible shapes and line types, check this R Graphics site to see all of the number identifiers:
df.merged$int <- paste(df.merged$Type, df.merged$Method, sep=".") ggplot(df.merged, aes(x, y, colour = int, linetype=int, shape=int)) + geom_line() + geom_point() + scale_colour_discrete("") + scale_linetype_manual("", values=c(1,2,1,2)) + scale_shape_manual("", values=c(17,17,16,16))
ggplot2: Is it possible to combine color/fill and size legends , ggplot2: Is it possible to combine color/fill and size legends? p + geom_point( aes(size = qsec, fill = qsec), shape = 21, alpha = 0.7) + and the size guide is a " legend" (NB: a legend in ggplot2 is a type of guide that shows as it makes no difference whether one is using R GUI, command line, or RStudio. How to merge color, line style and shape legends in ggplot. Ask Question Asked 3 years, 6 months ago. Combine legends for color and shape into a single legend. 682.
labs() and set the same value for all aesthetics defining the appearance of geoms.
library('ggplot2') ggplot(iris) + aes(x = Sepal.Length, y = Sepal.Width, color = Species, linetype = Species, shape = Species) + geom_line() + geom_point() + labs(color = "Guide name", linetype = "Guide name", shape = "Guide name")
Combine related legends into one in ggplot2, a <- rep(1:5,each=2) b <- rep(c(“Before”,”After”),5) p3 <- ggplot(data=df,aes(x=factor(Subject),y=Value)) + geom_point(aes(shape=Exposure,fill=Exposure)) + p4 <- ggplot(data=df,aes(x=factor(Subject),y=Value)) + geom_point(aes(shape=Exposure,fill=Exposure)) + p5 <- ggplot(data=df,aes(x=factor(Subject),y=Value)) + How to merge color, line style and shape legends in ggplot. Ask Question Asked 3 years, 7 months ago. Combine legends for color and shape into a single legend. 684.
One just need to name both guides the same. For example:
g+ scale_linetype_manual(name="Guide1",values= c('solid', 'solid', 'dotdash'))+ scale_colour_manual(name="Guide1", values = c("blue", "green","red"))
combine shape, size, and color in one ggplot legend � GitHub, combine shape, size, and color in one ggplot legend. testplot.r. library(tibble). library(ggplot2). set.seed(1). df <- data_frame(direction� Changing line colors with ggplot() Controlling line color and line type in ggplot legend ; Combine legends for color and shape into a single legend ; How can we make xkcd style graphs? Turning off some legends in a ggplot
12 Scales, Before diving into the details for each scale type, Section 12.1 provides an But limits also apply to scales that have legends, like colour, size, and shape, and by providing redundant mappings to other aesthetics like size, line type or shape. type linetype ggplot geom_line force combine color change and r ggplot2 How to merge color, line style and shape legends in ggplot English
The code below results in the desired legend, if I understand your question, but I'm not sure I understand the label issue, so let me know if this isn't what you were looking for.
p = ggplot(df.merged, aes(x, y, colour=interaction(Type, Method), linetype=interaction(Type, Method), shape=interaction(Type, Method))) + geom_line() + geom_point() p + scale_shape_manual(values=rep(16:17, 2)) + scale_linetype_manual(values=rep(c(1,3),each=2))
Legend of mixed line and point plots, ggplot(data, aes(Index)) + geom_point(aes(y=F1, colour="F1")) + I also tried to merge the data manually to suppress the line for F1 and the point shape for F2. there any way I could get the default line type settings from ggplot2? Hi @RuReady!Normally, guides (a catch-all term for legends, colour bars and other visual demonstrations of scale on the plot) combine automatically, and you can nudge things along by explicitly giving their associated scales the same name.
Mix geom_point and geom_abline, legend problem, shapes. i also add 2 horizontal lines via geom_abline. my problem is the legend. it seems p <- ggplot(data,aes(idx,c,colour=type,group=type, shape= type)) + j'ai eu ce problème l'autre jour. La R livre de recettes de la section Légendes explique:. si vous utilisez à la fois la couleur et la forme, il faut leur donner une échelle spécification.
Controlling legend appearance in ggplot2 with override.aes, To increase the size of the points in the color legend of my plot, the Note that I have to list the line type for every group, not just the I use linetype , shape , and color in the override.aes list within scale_alpha_manual() . Also there is still no size legend, and note that the size of the line items in the COLOR legend are size = 2. To recap, I would love to display the background color of confidence intervals, the linetype, and colors of lines (possibly other options like shapes) all in one legend.
Set guides for each scale — guides • ggplot2, Show colorbar guide for colour. # All these examples below have a same effect. p + guides(colour = "colorbar", size = "legend", shape = "legend"). #> Warning:� The behavior you're seeing is due to how ggplot2 deals with data that are outside the axis ranges of the plot. You can change this behavior depending on whether you use scale_y_continuous (or, equivalently, ylim) or coord_cartesian to set axis ranges, as explained below.
- Your color scale is redundant to the combined shape and linetype scales. You should use only one of these.
- Yes, its redundant. At first, I had colors mapped to Type and linetype mapped to Method. But then, when curves were too close to each other, it was hard to tell them apart. Thus the redundancy
- It is often appropriate to have redundant shape/color group definitions. In many scientific publications, color is the most visually effective way to distinguish groups, but you also know that a large fraction of readers will be printing black and white copies of the paper, and so you also want to include a visual cue that isn't dependent on color.
- Dear @Ben I know this question is old, but did you get any new answers on your GENERAL question, here?
- I believe my answer was as close as it gets to the general answer... I didn't look further anyway
- Well this would be the same as diasbling legends for linetype and shape but I think the OP wants 4 legend symbols with different colour/linetype/shape combined in each
- Well, that's almost what I'm looking for: you have one legend, true. But I could get the same result with scale_shape(guide = FALSE) and scale_linetype(guide = FALSE). And with your solution, I don't have the solid/dashed lines in the legend, nor the shape.
- I see. It's very similar to my solution and the one proposed by @eipi10. In your case however, the shape and linetype scale values are set manually.
- Yes I think that we all reached the right conclusion. The line types and shapes must be picked specifically since they do not take on a unique value for each.
- This is by far the simplest option which allows merging of legend types.
- This only works if the same variable is used for each aesthetic, unlike OP's question?
- This is one way of doing it indeed. I'll post another solution I found. IMHO, both are not very simple and I wished there was a better way to handle this case.