English 中文(简体)
2. 重置地平线钥匙,编号
原标题:Replacing ggplot2 legend key for geom_line with symbol
  • 时间:2011-10-03 11:18:41
  •  标签:
  • r
  • 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.

是否甚至有可能在四舍五入中? 任何要点,无论多么简短,都受到赞赏。 编制图表的编码如下。

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)
最佳回答

你们可以确定这样的新地理:

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)

勿庸置疑,这不会在下一版的聚合物中发挥作用。

“entergraph

问题回答

In ggplot the legend matches the plot itself. So, to get circles or squares in the legend you need to add circles or squares to the plot.

可以用<代码>geom_point(shape=...)进行。

chart1 + geom_point(shape=7)

“enterography





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签