Two geom_points add legend

I will build a graph with two geometers with the following code:

source("http://www.openintro.org/stat/data/arbuthnot.R") library(ggplot2) ggplot() + geom_point(aes(x = year,y = boys),data=arbuthnot,colour = '#3399ff') + geom_point(aes(x = year,y = girls),data=arbuthnot,shape = 17,colour = '#ff00ff') + xlab(label = 'Year') + ylab(label = 'Rate') 

I just want to know how to add a legend on the right side. With the same shape and color. The pink triangle must have the legend "woman" and the blue circle of the legend "man". It seems pretty simple, but after many trials I couldn't do it. (I start with ggplot).

enter image description here

+14
r ggplot2 legend legend-properties
source share
4 answers

If you rename your columns in the original data frame and then melt it into a long format using reshape2::melt , it is much easier to handle it in ggplot2. By specifying the aesthetics of color and shape in the ggplot command and specifying the scale for colors and shapes manually, a legend will appear.

 source("http://www.openintro.org/stat/data/arbuthnot.R") library(ggplot2) library(reshape2) names(arbuthnot) <- c("Year", "Men", "Women") arbuthnot.melt <- melt(arbuthnot, id.vars = 'Year', variable.name = 'Sex', value.name = 'Rate') ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex))+ geom_point() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) + scale_shape_manual(values = c('Women' = 17, 'Men' = 16)) 

enter image description here

+16
source share

Here is a way to do this without using reshape :: melt. reshape :: melt works, but you can enter the binding if you want to add other things to the chart, such as segments. The code below uses the original data organization. The key to changing the legend is to make sure that the arguments to scale_color_manual (...) and scale_shape_manual (...) are identical, otherwise you will get two legends.

 source("http://www.openintro.org/stat/data/arbuthnot.R") library(ggplot2) library(reshape2) ptheme <- theme ( axis.text = element_text(size = 9), # tick labels axis.title = element_text(size = 9), # axis labels axis.ticks = element_line(colour = "grey70", size = 0.25), panel.background = element_rect(fill = "white", colour = NA), panel.border = element_rect(fill = NA, colour = "grey70", size = 0.25), panel.grid.major = element_line(colour = "grey85", size = 0.25), panel.grid.minor = element_line(colour = "grey93", size = 0.125), panel.margin = unit(0 , "lines"), legend.justification = c(1, 0), legend.position = c(1, 0.1), legend.text = element_text(size = 8), plot.margin = unit(c(0.1, 0.1, 0.1, 0.01), "npc") # c(bottom, left, top, right), values can be negative ) cols <- c( "c1" = "#ff00ff", "c2" = "#3399ff" ) shapes <- c("s1" = 16, "s2" = 17) p1 <- ggplot(data = arbuthnot, aes(x = year)) p1 <- p1 + geom_point(aes( y = boys, color = "c1", shape = "s1")) p1 <- p1 + geom_point(aes( y = girls, color = "c2", shape = "s2")) p1 <- p1 + labs( x = "Year", y = "Rate" ) p1 <- p1 + scale_color_manual(name = "Sex", breaks = c("c1", "c2"), values = cols, labels = c("boys", "girls")) p1 <- p1 + scale_shape_manual(name = "Sex", breaks = c("s1", "s2"), values = shapes, labels = c("boys", "girls")) p1 <- p1 + ptheme print(p1) 

output results

+8
source share

Here is an answer based on the tidyverse package. Where can I use pipe, %>% to bind functions together. The plot creation continues in one way, eliminating the need for temporary variables. You can read more about pipe in this post. What does the% & gt;% function in R mean?

As far as I know, legends in ggplot2 are based only on aesthetic variables. Thus, to add a discrete legend, use the category column and change the aesthetics to suit the category. In ggplot, this is done, for example, aes(color=category) .

Thus, to add two (or more) different data frame variables to the legends, you need to convert the data frame so that we have a category column telling us which column (variable) is displayed, and the second column that actually contains the Value. The tidyr::gather function, which was also loaded by tidyverse , does just that.

You can then create a legend by simply specifying which aesthetics variables should be different. In this example, the code will look like this:

 source("http://www.openintro.org/stat/data/arbuthnot.R") library(tidyverse) arbuthnot %>% rename(Year=year,Men=boys,Women=girls) %>% gather(Men,Women,key = "Sex",value = "Rate") %>% ggplot() + geom_point(aes(x = Year, y=Rate, color=Sex, shape=Sex)) + scale_color_manual(values = c("Men" = "#3399ff","Women"= "#ff00ff")) + scale_shape_manual(values = c("Men" = 16, "Women" = 17)) 

Note that the tidyverse package tidyverse also automatically loaded into the ggplot2 package. An overview of installed packages can be found on their website tidyverse.org .

In the above code, I also used the dplyr::rename function (also loaded by tidyverse ) to first rename the columns to the desired labels. Because the legend automatically accepts labels equal to category names.

There is a second way to rename legend labels, which involves explicitly specifying labels in the scale_aesthetic_manual functions using the labels = argument. See examples in the cookbook of legends . But not recommended, as it quickly gets confused with a lot of variables.

0
source share

This is the trick I usually use. Add the colour argument to aes and use it as an indicator for label names.

 ggplot() + geom_point(aes(x = year,y = boys, colour = 'Boys'),data=arbuthnot) + geom_point(aes(x = year,y = girls, colour = 'Girls'),data=arbuthnot,shape = 17) + xlab(label = 'Year') + ylab(label = 'Rate') 

enter image description here

0
source share

All Articles