繁体   English   中英

使用线型在 ggplot 上添加一个简单的图例

[英]add a simple legend on ggplot with linetype

如何向此 ggplot 添加图例? 我到处搜索,但找不到与我当前代码匹配的简单方法。 我有三个变量,它们都有相同的颜色和不同的线型。

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

另一种方法是data.table解决方案。 当然,您首先必须安装和加载data.table包。

install.packages("data.table")
library(data.table)

我已经为您的情况创建了这个虚拟数据,并将 'Year' 的字符类型转换为日期格式,并将整个数据转换为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()

我将 'OECD'、'World' 和 'Switzerland' 的这些列融合到一列 'variable' data.table ' melt()函数中,使用 'Year' 列作为 ID 列:

datapop_melted <- melt(datapop, id.vars = "Year")

然后,我简单地使用ggplot2包绘制了这些数据。 aes()部分中,我给出了linetype = variable参数,以便它自己根据linetype = variable创建图例:

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

您可以在链接中看到结果图。

更新

这是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))

例如,我会执行以下操作。 要调整您的linetype您应该使用scale_linetype_manual() 参数name用于图例的标题,而values通过在geom_line()调用您在原始aes()使用的名称来控制线型。

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

这是您应该在不使用任何其他包而不是ggplot2 的情况下执行此操作的方式。

现在看起来更好了吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM