`ggplot2`图例没有显示添加系列的标签

时间:2014-03-20 11:57:30

标签: r ggplot2

在以下ggplot2中,有两个系列,即美国GDP及其移动平均线,图例并未显示移动平均线系列:

require(xts);require(data.table);require(ggplot2);require(ggthemes)

# US GDP quarterly data
data <- structure(list(ShortName = c("US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)","US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)",  "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)",  "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)","US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)",  "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)","US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)", "US GDP QoQ (Annualised)"), Date = structure(c(10681L, 10772L,10864L, 10956L, 11047L, 11138L, 11230L, 11322L, 11412L, 11503L,11595L, 11687L, 11777L, 11868L, 11960L, 12052L, 12142L, 12233L,12325L, 12417L, 12508L, 12599L, 12691L, 12783L, 12873L, 12964L,13056L, 13148L, 13238L, 13329L, 13421L, 13513L, 13603L, 13694L,13786L, 13878L, 13969L, 14060L, 14152L, 14244L, 14334L, 14425L,14517L, 14609L, 14699L, 14790L, 14882L, 14974L, 15064L, 15155L,15247L, 15339L, 15430L, 15521L, 15613L, 15705L, 15795L, 15886L,15978L, 16070L), class = c("IDate", "Date")), Value = c(3.8,3.4, 5.2, 7.1, 1.2, 7.8, 0.5, 2.1, -1.1, 2.1, -1.2, 1, 3.8, 2.2,1.9, 0.2, 2, 3.8, 6.9, 4.6, 2.4, 3.1, 3.6, 3.4, 4.5, 2.2, 3.3,2.2, 4.9, 1.3, 0.3, 3.2, 0.3, 3.1, 2.7, 1.5, -2.7, 2, -2, -8.3,-5.4, -0.4, 1.3, 3.9, 1.6, 3.9, 2.8, 2.8, -1.3, 3.2, 1.4, 4.9,3.7, 1.2, 2.8, 0.1, 1.1, 2.5, 4.1, 2.4)), .Names = c("ShortName","Date", "Value"), class = c("data.table", "data.frame"), row.names = c(NA,-60L))

ggplot(data, aes_string(x="Date", y="Value", colour="ShortName")) + 
  geom_line(size=1.2) +

  # add 4 quarter moving average series
  geom_line(aes(x=Date, 
                y=rollmean(Value, k=4, fill=NA, align="right"))
            , colour="red") +

  # the economist theme from package::ggthemes
  theme_economist_white(gray_bg=FALSE) + 
  scale_colour_economist() +
  xlab("") + ylab("")

问题: 如何在图例中添加移动平均线系列标签,(文字说&#39; s说&#34;四分之三移动平均值&# 34;

加分问题: 如何从图例中删除ShortName文字?

1 个答案:

答案 0 :(得分:1)

可能为美学指定一些东西

ggplot(data) + 
  geom_line(aes_string(x="Date", y="Value", colour="ShortName"), size=1.2) +
  # add 4 quarter moving average series
  geom_line(aes(x=Date, 
                y=rollmean(Value, k=4, fill=NA, align="right"),
                linetype =  'Moving average'),
            colour="red") +
  scale_colour_manual('', values = "blue") +
  scale_linetype_manual('', values = 1)

和ggthemes

ggplot(data) + 
  geom_line(aes_string(x="Date", y="Value", colour="ShortName"), size=1.2) +
  # add 4 quarter moving average series
  geom_line(aes(x=Date, 
                y=rollmean(Value, k=4, fill=NA, align="right"),
                linetype =  'Moving average'),
            colour="red") +
  scale_colour_economist(name='') +
  #scale_colour_manual('', values = "blue") +
  scale_linetype_manual('', values = 1) +
  theme_economist_white(gray_bg=FALSE)

ggtheme plot

您可以使用

更改图例项目的顺序
plot <- ggplot(data) + 
  geom_line(aes_string(x="Date", y="Value", colour="ShortName"), size=1.2) +
  # add 4 quarter moving average series
  geom_line(aes(x=Date, 
                y=rollmean(Value, k=4, fill=NA, align="right"),
                linetype =  'Moving average'),
            colour="red") +
  scale_colour_economist(name='') +
  #scale_colour_manual('', values = "blue") +
  scale_linetype_manual('', values = 1) +
  theme_economist_white(gray_bg=FALSE)

plot + guides(colour = guide_legend(order = 1), 
              linetype = guide_legend(order = 2))

change legend order