如何在ggplot2中更改图例文本的大小?

时间:2016-02-08 04:28:51

标签: r ggplot2 legend

我使用下面的数据和代码得到了这个情节

enter image description here

我希望能够更改图例文字(A,B,M1,M3)的大小。我尝试使用

legend.text=element_text(size=0.5)

但它并没有改变。任何建议如何减少legend.text的大小?

代码

ggplot(df.trial1, aes(x=Date, y= A, color="A"))+
      geom_line(linetype=3, size=0.2)+
      geom_point(aes(x=Date, y=B, color="B"), shape = 16, size =1, alpha=0.5)+
      geom_point(aes(x=Date, y=Value, color=Method), size =1, alpha=0.5)+
      scale_colour_manual(name=" ", values=cols,
                      labels=c("A", "B", 'M1', "M2", "M3"))+
      scale_linetype_manual(values = c("dashed")) +
      scale_x_date(breaks = date_breaks("1 month"), labels = date_format("%d-%b-%y"))+
  guides(color=guide_legend(override.aes=list(shape=c(NA,16, 16, 16, 16),
                                          linetype=c(3,0, 0, 0, 0)),
                        direction = "vertical", title.position = "top",
                        label.position="right", label.hjust = 0, label.vjust = 0.5,
                        label.theme = element_text(angle = 0)))+
  labs(x=expression(Date), y=expression(Value))+
  theme(legend.text=element_text(size=0.5))

1 个答案:

答案 0 :(得分:4)

从指南中删除label.theme = element_text(angle = 0)似乎可以解决此问题。

guides(color=guide_legend(override.aes=list(shape=c(NA,16, 16, 16, 16),
                                        linetype=c(3,0, 0, 0, 0)),
                      direction = "vertical", title.position = "top",
                      label.position="right", label.hjust = 0, label.vjust = 0.5
                      #,label.theme = element_text(angle = 0)
                      )) +
theme(legend.text=element_text(size=10))

enter image description here