如何调整图例属性以显示2个特征?

时间:2016-12-09 19:00:52

标签: r ggplot2 legend legend-properties

我在同一个图中绘制两个变量,每个变量按年分组。我的图例显示了按年份划分的颜色差异,但我无法弄清楚如何在图例中添加线型。理想情况下,图例中会有4行:pink = 2015,blue = 2016,虚线= Var1,实线= Var2

这是我的样本df:

year <- c(2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016)
month <-c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12)
Var1 <- sample(30:60, 24, rep=TRUE)
Var2 <- sample(70:90, 24, rep=TRUE)
df <- data.frame(year,month,Var1, Var2)

情节:

plot <- ggplot(df)+
  geom_line(aes(x=as.factor(month), y=Var1, 
            group=as.factor(year), color=as.factor(year)), linetype=2, size=0.9)+
  geom_point(aes(x=as.factor(month), y=Var1, group=as.factor(year)))+
  geom_line(aes(x=as.factor(month), y=Var2, 
            group=as.factor(year), color=as.factor(year)), linetype=1, size=0.9)+
  geom_point(aes(x=as.factor(month), y=Var2, group=as.factor(year)))+
  theme_tufte()+
  theme(legend.title=element_blank())+
  labs(x=NULL,y="",title="Var1 vs. Var2")+
  scale_x_discrete(expand=c(0,0.2),
               labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
plot

我尝试过更改geom_line中的aes,以及添加&amp;配置一些scale_xxx_yyyy,包括scale_linetype_manual。到目前为止都没有成功。理想情况下,我不必单独绘制我的图例,但能够从ggplot中引导它。

2 个答案:

答案 0 :(得分:2)

为此,我们将不得不改变您的数据集。首先,我们将收集(从tidyr包中)你的var1和var2,这样我们就有了两个新变量,一个名为var,它将具有值&#34; var1&#34;和&#34; var2&#34;和一个名为n的变量,其值为var1和var2。

df <- data.frame(year,month,Var1,Var2) %>%
        gather("var", "n", 3:4)

然后为&#34;年&#34;的每个组合; X&#34; var&#34;我们将创建一个段变量,指示我们将绘制哪一行。

  • 1年份== 2015年&amp; var == var1
  • 2年== 2016年&amp; var == var1
  • 3年== 2015年&amp; var == var2
  • 4年== 2016年&amp; var == var2
像这样懒散地完成了

df$segment <- rep(1:4, each = 12)

现在的目标是根据细分逐个绘制线条。这将通过以下循环

完成
gg <- ggplot()
for (i in 1:4) gg <- gg + 
  geom_line(data = subset(df, segment == i), 
            aes(x = as.factor(month), y = n, linetype = var,
                group = as.factor(year), color = as.factor(year))) +
  geom_point(data = subset(df, segment == i), 
            aes(x = as.factor(month), y = n, group = as.factor(year)))

注意这个和你的之间的区别是geom_line有一个根据段的子集化数据集(我们只需要我们绘制的线的数据)。 y = n根据我们之前所做的聚集,现在我们设置了一个新的美学,这是我们的var。这循环4次,与段数相同。

最后添加其他主题和实验

gg <- gg + theme(legend.title = element_blank())+
    labs(x = NULL,y = "", title = "Var1 vs. Var2")+
    scale_x_discrete(expand = c(0,0.2),
                     labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
gg

我们有结果

enter image description here

答案 1 :(得分:1)

year <-   c(2015,2015,2015,2015,2015,2015,2015,2015,2015,
2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016)
month <-c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12)
Var1 <- sample(30:60, 24, rep=TRUE)
Var2 <- sample(70:90, 24, rep=TRUE)
df <- data.frame(year,month,Var1, Var2)

如果您稍微修改data.frame的格式,并在ggplot2中使用交互来组合2个变量。干得好。我使用data.table,因为我不再知道如何将data.frame转换为long。

library(data.table)
library (ggplot2)
ggplot(melt(as.data.table(df),id.vars=c("year","month")))+   
geom_line(aes(x=as.factor(month), 
y=value, group=interaction(variable,  as.factor(year)),
color=as.factor(year),linetype=variable), size=0.9) + 
labs(x = NULL,y = "", title = "Var1 vs. Var2")+
 scale_x_discrete(expand = c(0,0.2),
                  labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))

enter image description here