用符号替换geom_line的ggplot2图例 [英] Replacing ggplot2 legend key for geom_line with symbol

查看:480
本文介绍了用符号替换geom_line的ggplot2图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个股票的价格线图,我通过从我看到的期间开始时的百分比变化来标准化。这似乎工作正常,但不是在当前构成图例的灰色背景上的彩色线条,我想在标签旁边显示正方形或圆圈。



这在ggplot2中甚至可能吗?任何指针,无论简短,赞赏。

 日期<  -  c(2011-09-19,2011-09 -20,2011-09-21,2011-09-22,
2011-09-23,2011-09-26,2011-09-27,2011 -0-9-28,2011-09-29,2011-09-30)
CoA <-c(100,100,95,93,88,91,98,109,115,106)
CoB< ; c(16.5,16.8,17.2,17,17.5,16.5,16,15.5,16.1,16.3)
CoC <-c(3.2,3.18,3.15,3.12,3.15,3.1,3.08,3.11, 3.35,3.42)
价格< - data.frame(日期,CoA,CoB,CoC)
变化< - as.data.frame(矩阵(nrow = nrow(价格),ncol = ncol (价格)))
changes [,1] = prices [,1]
for(i in 2:ncol(价格)){#计算价格变化
changes [,i] =(价格[,i] -prices [,i] [1])/ prices [,i] [1]
}
colnames(变更)< - colnames(价格)
变化< - melt(changes,id =Date)
变化$ Date< - as.Date(as.character(变更$ Date))
chart1< - ggplot(data = changes ,aes(x =更改$日期,y =更改$值,color =更改$变量))
chart1< - chart1 + geom_line(lwd = 0.5)+ y实验室(价格变化(%))+ xlab(日期)+
实验室(color =公司)
打印(图表1)


$ b <$ p


解决方案

geomLine2 < - proto(GeomLine,{
objname< - line2
guide_geom< - function(。)polygon
default_aes < - 函数(。)aes(color =black,size = 0.5,linetype = 1,alpha = 1,fill =grey20)
})
geom_line2< - GeomLine2 $ build_accessor ()

chart1< - ggplot(data = changes,aes(x = Date,y = value,color = variable,fill = variable))
chart1< - chart1 + geom_line2 (lwd = 0.5)+ ylab(价格变化(%))+ xlab(日期)+
实验室(color =Company,fill =Company)
print(chart1 )

不确定,但请注意,这在ggplot2的下一个版本中不起作用。




I have a line plot of prices for three stocks, which I normalise by taking the percentage change from the beginning of the period I am looking at. This seems to work fine, but instead of the coloured lines on a grey background that currently make up the legend key, I would like squares or circles of colour next to the key label.

Is this even possible in ggplot2? Any pointers, however brief, appreciated. Code to produce the chart follows below.

Date <- c("2011-09-19","2011-09-20","2011-09-21","2011-09-22",
                "2011-09-23","2011-09-26","2011-09-27","2011-09-28","2011-09-29","2011-09-30")
CoA <- c(100,100,95,93,88,91,98,109,115,106)
CoB <- c(16.5,16.8,17.2,17,17.5,16.5,16,15.5,16.1,16.3)
CoC <- c(3.2,3.18,3.15,3.12,3.15,3.1,3.08,3.11,3.35,3.42)
prices <- data.frame(Date,CoA,CoB,CoC)
changes <- as.data.frame(matrix(nrow=nrow(prices),ncol=ncol(prices)))
changes[,1]=prices[,1]
for(i in 2:ncol(prices)){ # calculate changes in price
    changes[,i]= (prices[,i]-prices[,i][1])/prices[,i][1]
}
colnames(changes) <- colnames(prices)
changes <- melt(changes, id = "Date")
changes$Date <- as.Date(as.character(changes$Date))
chart1 <- ggplot(data=changes,aes(x=changes$Date,y=changes$value,colour=changes$variable))
chart1 <- chart1 + geom_line(lwd=0.5) + ylab("Change in price (%)") + xlab("Date") +
    labs(colour="Company")
print(chart1)

解决方案

You can define new geom like this:

GeomLine2 <- proto(GeomLine, {
    objname <- "line2"
    guide_geom <- function(.) "polygon"
    default_aes <- function(.) aes(colour = "black", size=0.5, linetype=1, alpha = 1, fill = "grey20")
     })
geom_line2 <- GeomLine2$build_accessor()

chart1 <- ggplot(data=changes,aes(x=Date, y=value, colour=variable, fill = variable))
chart1 <- chart1 + geom_line2(lwd=0.5) + ylab("Change in price (%)") + xlab("Date") +
  labs(colour="Company",  fill = "Company")
print(chart1)

Not sure but note that this will not work in the next version of ggplot2.

这篇关于用符号替换geom_line的ggplot2图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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