Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用符号替换geom_线的ggplot2图例键_R_Ggplot2 - Fatal编程技术网

R 用符号替换geom_线的ggplot2图例键

R 用符号替换geom_线的ggplot2图例键,r,ggplot2,R,Ggplot2,我有三只股票的价格折线图,我通过从我所观察的时期开始的百分比变化将其正常化。这似乎工作得很好,但不是灰色背景上的彩色线,目前构成的图例关键,我想广场或彩色圆圈旁边的关键标签 这在ggplot2中可能吗?任何提示,无论多么简短,都将不胜感激。下面是生成图表的代码 Date <- c("2011-09-19","2011-09-20","2011-09-21","2011-09-22", "2011-09-23","2011-09-26","2011-09-27

我有三只股票的价格折线图,我通过从我所观察的时期开始的百分比变化将其正常化。这似乎工作得很好,但不是灰色背景上的彩色线,目前构成的图例关键,我想广场或彩色圆圈旁边的关键标签

这在ggplot2中可能吗?任何提示,无论多么简短,都将不胜感激。下面是生成图表的代码

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)

Date在
ggplot
中,图例与绘图本身相匹配。因此,要在图例中获得圆或正方形,需要将圆或正方形添加到绘图中

这可以通过
geom_点(shape=…)
实现
shape=1
生成圆,
shape=7
生成正方形

chart1 + geom_point(shape=7)

ggplot
中,图例与绘图本身匹配。因此,要在图例中获得圆或正方形,需要将圆或正方形添加到绘图中

这可以通过
geom_点(shape=…)
实现
shape=1
生成圆,
shape=7
生成正方形

chart1 + geom_point(shape=7)

您可以这样定义新几何图形:

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)

GeomLine2您可以这样定义新的geom:

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)

GeomLine2谢谢,很高兴能确认我已经怀疑的事情。有什么办法可以绕过这个限制吗?谢谢,很高兴能证实我已经怀疑的事情。有没有办法绕过这个限制?