R:如何重新排序ggplot2线图中的图例键以匹配每个系列的最终值? [英] R: how to reorder legend key in ggplot2 line plot to match the final values in each series?

查看:1867
本文介绍了R:如何重新排序ggplot2线图中的图例键以匹配每个系列的最终值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot2来绘制折线图,​​显示多个仪器的随时间变化的价格。我成功地获得了多条线,并添加了显示价格最近变化的值。我想要做的(还没有实现的)是重新排序传奇关键词,使得涨幅最大的价格系列位于传奇的顶端,其次是价格系列的第二大关键字,等等。



在下面的图表中,图例按字母顺序显示密钥。我想要做的是按照DDD,AAA,CCC和BBB的顺序显示图例关键条目,这是最近日期的性能顺序。





最小代码如下所示。

  require(ggplot2 )
要求(比例)
要求(gridExtra)
要求(润滑)
要求(重塑)

#创建假价格数据
set.seed(123)
monthsback < - 15
date < - as.Date(paste(year(now()),month(now()),1,sep = - )) - months(monthsback)
mydf< - data.frame(mydate = seq(as.Date(date),by =month,length.out = monthsback),
aaa = month(backback,min = 600,max = 800),
bbb = runif(monthsback,min = 100,max = 200),
ccc = runif(monthsback,min = 1400,max = 2000) ),
ddd = runif(monthsback,min = 50,max = 120))

#函数来计算变化
change_from_start< - function(x){
(x - x [1])/ x [1]
}

#f或适当的列(即没有日期),用价格
mydf [,2:5]< - lapply(mydf [,2:5],function(myparam){change_from_start(myparam)})

#获取最新值并重塑
myvals< - mydf [mydf $ mydate == mydf $ mydate [nrow(mydf)],]
myvals< - melt(myvals, id = c('mydate'))

#绘制多行
p < - ggplot(data = mydf)+
geom_line(aes(x = mydate,y = aaa ,颜色=AAA),size = 1)+
geom_line(aes(x = mydate,y = bbb,color =BBB),size = 1)+
geom_line = $ mydate,y = ccc,color =CCC),size = 1)+
geom_line(aes(x = mydate,y = ddd,color =DDD),size = 1)+
scale_colour_manual(,values = c(AAA=red,BBB=black,CCC=blue,DDD=green))+
scale_y_continuous label = percent_format())+
geom_text(data = myvals,aes(x = mydate + 30,y = value,label = sprintf(%+ 1.1f %%,myvals $ value * 100)), size = 4,color =grey50)+
o pts(axis.title.y = theme_blank())+
opts()

#和输出
print(p)

 

解决方案

code> mydf < - melt(mydf,id.var = 1)
mydf $ variable < - factor(mydf $ variable,levels = rev(myvals $ variable [order(myvals $ value)] ),ordered = TRUE)

#绘制多行
p < - ggplot(data = mydf)+
geom_line(aes(x = mydate,y = value,color =变量,组=变量),size = 1)+
scale_colour_manual(,values = c(aaa=red,bbb=black,ccc=blue dd =green))+
scale_y_continuous(label = percent_format())+
geom_text(data = myvals,aes(x = mydate + 30,y = value,label = sprintf( ),
size = 4,color =grey50)+
opts(axis.title.y = theme_blank())+
opts()

#和输出
print(p)


我将您的完整数据集合化,以节省您绘制代码的几行内容。和往常一样,关键是确保变量是一个有序因素。



为了解决注释中出现的问题,您可以将任何标签传递给出现在图例本身,只要你得到正确的订单:

  ggplot(data = mydf)+ 
geom_line(aes(x = mydate,y = value,color = variable,group = variable),size = 1)+
scale_colour_manual(,values = c(aaa=red,bbb ='black','ccc'=blue,ddd=green),labels = c('公司D','公司A','公司C','公司B'))+
scale_y_continuous(label = percent_format())+
geom_text(data = myvals,aes(x = mydate + 30,y = value,label = sprintf(%+ 1.1f %%,myvals $ value * 100)),
size = 4,color =grey50)+
opts(axis.title.y = theme_blank())+
opts()



注意:自0.9.2版本开始 opts 已被主题所取代,例如:

  + theme(axis.title.y = element_blank())


I am plotting line charts showing the change in price over time for multiple instruments, using ggplot2. I have succeeded in getting multiple lines on the plot and adding values showing the most recent change in price. What I want to do (and have not yet achieved) is to reorder the legend key so that the price series that has risen the most is at the top of the legend, followed by the key of the price series that rose second-most and so on.

In the plot below, the legend shows the key in alphabetical order. What I would like it to do is to show the legend key entries in the order DDD, AAA, CCC then BBB, which is the order of performance as of the most recent date. How can I do this?

Minimal-ish code follows.

require(ggplot2)
require(scales)
require(gridExtra)
require(lubridate)
require(reshape)

# create fake price data
set.seed(123)
monthsback <- 15
date <- as.Date(paste(year(now()), month(now()),"1", sep="-")) - months(monthsback)
mydf <- data.frame(mydate = seq(as.Date(date), by = "month", length.out = monthsback),
                      aaa = runif(monthsback, min = 600, max = 800),
                      bbb = runif(monthsback, min = 100, max = 200),
                      ccc = runif(monthsback, min = 1400, max = 2000),
                      ddd = runif(monthsback, min = 50, max = 120))

# function to calculate change
change_from_start <- function(x) {
   (x - x[1]) / x[1]
}

# for appropriate columns (i.e. not date), replace fake price data with change in price
mydf[, 2:5] <- lapply(mydf[, 2:5], function(myparam){change_from_start(myparam)})

# get most recent values and reshape
myvals <- mydf[mydf$mydate == mydf$mydate[nrow(mydf)],]
myvals <- melt(myvals, id = c('mydate'))

# plot multiple lines
p <- ggplot(data = mydf) +
    geom_line( aes(x = mydate, y = aaa, colour = "AAA"), size = 1) +
    geom_line( aes(x = mydate, y = bbb, colour = "BBB"), size = 1) +
    geom_line( aes(x = mydate, y = ccc, colour = "CCC"), size = 1) +
    geom_line( aes(x = mydate, y = ddd, colour = "DDD"), size = 1) +
    scale_colour_manual("", values = c("AAA" = "red", "BBB" = "black", "CCC" = "blue", "DDD" = "green")) +
    scale_y_continuous(label = percent_format()) +
    geom_text(data = myvals, aes(x = mydate + 30, y = value, label = sprintf("%+1.1f%%", myvals$value * 100)), size = 4, colour = "grey50") +
    opts(axis.title.y = theme_blank()) +
    opts()

# and output
print(p)

解决方案

Try this:

mydf <- melt(mydf,id.var = 1)
mydf$variable <- factor(mydf$variable,levels = rev(myvals$variable[order(myvals$value)]),ordered = TRUE)

# plot multiple lines
p <- ggplot(data = mydf) +
    geom_line(aes(x = mydate,y = value,colour = variable,group = variable),size = 1) +
    scale_colour_manual("", values = c("aaa" = "red", "bbb" = "black", "ccc" = "blue", "ddd" = "green")) +
    scale_y_continuous(label = percent_format()) +
    geom_text(data = myvals, aes(x = mydate + 30, y = value, label = sprintf("%+1.1f%%", myvals$value * 100)), 
                size = 4, colour = "grey50") +
    opts(axis.title.y = theme_blank()) +
    opts()

# and output
print(p)

I melted your full data set to save you several lines for plotting code. The key, as usual, is to make sure the variable is an ordered factor.

To address the issue that arose in the comments, you can pass whatever labels you like to appear in the legend itself, as long as you get the order correct:

ggplot(data = mydf) +
    geom_line(aes(x = mydate,y = value,colour = variable,group = variable),size = 1) +
    scale_colour_manual("", values = c("aaa" = "red", "bbb" = "black", "ccc" = "blue", "ddd" = "green"),labels = c('Company D','Company A','Company C','Company B')) +
    scale_y_continuous(label = percent_format()) +
    geom_text(data = myvals, aes(x = mydate + 30, y = value, label = sprintf("%+1.1f%%", myvals$value * 100)), 
                size = 4, colour = "grey50") +
    opts(axis.title.y = theme_blank()) +
    opts()

Note: Since version 0.9.2 opts has been replaced by theme, e.g.:

+ theme(axis.title.y = element_blank())

这篇关于R:如何重新排序ggplot2线图中的图例键以匹配每个系列的最终值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆