本文介绍了`ggplot2`图例没有显示添加系列的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在下面的 ggplot2 中有两个系列,美国GDP和它的移动平均线,图例并没有显示移动平均线系列: require(xts); require(data.table); require(ggplot2); require(ggthemes) #美国GDP季度数据(美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化) ,美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP (年化),美国GDP季度(年化),美国GDP季度(年化),美国GDP季度(年化),美国GDP季度(年化),美国GDP季度(年化) ,美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ (年化),美国GDP季度(年化),美国GDP季度(年化),美国GDP季度(年化),美国GDP季度(年化),美国G美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化) ,美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP (年化),美国GDP季度(年化),美国GDP季度(年化),美国GDP季度(年化),美国GDP季度(年化),美国GDP季度(年化) ,美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ (年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ 美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ(年化),美国GDP QoQ年化),美国GDP QoQ(年化)),日期=结构(c(10681L,10772L,10864L,10956L,11047L,1 1138L,11230L,11322L,11412L,11503L,11595L,11687L,11777L,11868L,11960L,12052L,12142L,12233L,12325L,12417L,12508L,12599L,12691L,12783L,12873L,12964L,13056L,13148L,13238L, 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.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,color =ShortName))+ geom_line(size = 1.2) + #添加4美元移动平均线系列 geom_line(aes(x =日期,y = rollmean(值,k = 4,fill = NA,align =right)),color =red )+ #来自包的经济学家主题:: ggthemes theme_economist_white(gray_bg = FALSE)+ scale_colour_economist()+ xlab()+ ylab ( ) 的问题: 如何将移动平均数系列标签添加到图例中(文字说让我们说四分之三移动平均值?) $ b $ p 附加问题: 如何从图例中移除 ShortName 文字? ggplot(data)+ geom_line(aes_string(x =Date ,y =Value,color =ShortName),size = 1.2)+ #添加4个季度移动平均系列 geom_line(aes(x = Date,y = rollmean ,k = 4,fill = NA,align =right), linetype ='Moving average'), color =red)+ scale_colour_manual('',values =blue)+ scale_linetype_manual('',values = 1) 和与ggthemes ggplot(data)+ geom_line(aes_string(x =Date,y =Value,color =ShortName),size = 1.2)+ # (aes(x = Date,y = rollmean(Value,k = 4,fill = NA,align =right), linetype ='移动平均数'), color =red)+ scale_colour_economist(name ='')+ #scale_colour_manual('',values =blue)+ scale_linetype_manual('',values = 1)+ theme_economist_white(gray_bg = FALSE) 您可以使用 $更改图例项目的顺序b $ b plot geom_line(aes_string(x =Date,y =Value,color =ShortName),size = 1.2)+ #添加4个季度移动平均系列 geom_line(aes(x = Date,y = rollmean(Value,k = 4,fill = NA,align =right), linetype ='移动平均数'), color = 'red')+ scale_colour_economist(name ='')+ #scale_colour_manual('',values =blue)+ scale_linetype_manual('',values = 1)+ theme_economist_white(gray_bg = FALSE) 绘图+指南(color = guide_legend(order = 1), linetype = guide_legend(order = 2)) In the following ggplot2 with two series, US GDP and its moving average, the legend doesn't display the moving average series:require(xts);require(data.table);require(ggplot2);require(ggthemes)# US GDP quarterly datadata <- 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("")Question: How to add the moving average series label to the legend, (text saying let's say "4-quarters moving avg"?Bonus question: How to remove ShortName text from the legend? 解决方案 Maybe specifying something to aestheticsggplot(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)and with ggthemesggplot(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)You may change the order of legend items withplot <- 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)) 这篇关于`ggplot2`图例没有显示添加系列的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-09 19:10