How do I add a legend to this ggplot? I've searched everywhere but couldn't find a simple way that matches my current code. I've got three variable, they all have the same colour with different linetypes.
ggplot()+geom_line(data=datapop, aes(Year, OECD),size = 0.7, color="#69b3a2") +
geom_line(data=datapop, aes(Year, World),size = 0.7, color="#69b3a2", linetype="dashed")+
geom_line(data=datapop, aes(Year, Switzerland),size = 0.7, color="#69b3a2", linetype="twodash")+
xlab("Years")+ ylab("Aging") +theme_minimal()+ labs(color="Legend text")+
scale_x_date(date_breaks = "2 years",date_labels = "%Y")+
theme(axis.text.x=element_text(angle=60, hjust=1))
Another approach would be the data.table
solution. Naturally, you first have to install and load the data.table
package.
install.packages("data.table")
library(data.table)
I have created this dummy data for your situation, and converted the character type of 'Year' to the Date format, and converted whole data to a data.table
:
datapop <- data.frame(Year = c("1980", "1982", "1984"), OECD = c(2,3,4), World = c(3,5,8), Switzerland = c(2,2.5,3)) %>% mutate(Year = as.Date(Year, format = "%Y")) %>% as.data.table()
I melted the these columns of 'OECD', 'World' and 'Switzerland' to a single column of 'variable' data.table
's melt()
function, using 'Year' column as the ID column:
datapop_melted <- melt(datapop, id.vars = "Year")
Then, I simply plotted this data using ggplot2
package. In the aes()
section I have given linetype = variable
argument, so that it creates the legend according to the line types by itself:
ggplot(data=datapop_melted) +
geom_line(aes(x = Year, y = value, linetype = variable)) +
labs(title="Years vs Aging", x ="Years", y = "Aging", linetype = "Location") +
scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
theme(axis.text.x=element_text(angle=60, hjust=1)) +
theme_minimal()
You can see the resulting plot in the link .
UPDATE
Here is a solution with mtcars
.
data("mtcars")
ggplot( data = mtcars) +
geom_line(aes(gear, mpg, linetype = "mpg"), color = "royalblue", size = 0.7 ) +
geom_line(aes(gear, drat, linetype = "drat" ), color = "royalblue", size = 0.7 ) +
geom_line(aes(gear, qsec, linetype = "qsec" ), color = "royalblue", size = 0.7 )+
xlab("gear") + ylab("Outcomes") +
scale_linetype_manual( name = "Legend text",
values = c( "mpg" = "solid",
"drat" = "dashed",
"qsec" = "twodash" ) ) +
theme_minimal() +
theme(axis.text.x=element_text(angle=60, hjust=1))
For you example, I would do the following. To adjust your linetype
you should use scale_linetype_manual()
. The argument name
is for the legend's title, whereas values
controls the linetype by calling the names you used in the original aes()
within geom_line()
.
ggplot( data = datapop)+
geom_line(aes(Year, OECD, linetype = "OECD" ), color = "royalblue", size = 0.7 ) +
geom_line(aes(Year, World, linetype = "World" ), color = "royalblue", size = 0.7 ) +
geom_line(aes(Year, Switzerland, linetype = "Switzerland" ), color = "royalblue", size = 0.7 ) +
xlab("Year") + ylab("Aging") +
scale_color_manual( name = "Legend text",
values = c( "OECD" = "solid",
"World" = "dashed",
"Switzerland" = "twodash" ) ) +
theme_minimal() +
theme(axis.text.x=element_text(angle=60, hjust=1))
This is the way you should do it without using any other package rather than ggplot2 .
Does it look better now?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.