Reputation:
I have generated the following graph:
http://i47.tinypic.com/s3dd0m.png
I have a fair amount of long data (that can be downloaded here: http://www.sendspace.com/file/lfd31r), and the data looks like:
head(data)
-10:130 variable value
1 -10 Utilities 0.001680609
2 -9 Utilities 0.004652453
3 -8 Utilities -0.002441692
4 -7 Utilities -0.018639594
5 -6 Utilities -0.007587632
6 -5 Utilities 0.004526066
The code I used to generate this graphic:
ggplot(data=data,
aes(x=-10:130, y=value, colour=variable)) +
geom_line()
I want something that looks like the following graphic:
with the legend gone, but the acronym of the category displayed right at the end of each line in the same colour as the line itself. This will be necessary because there's too many lines and colours for the reader to understand what's what. Once you geniuses help me figure out how to solve this, I will then make a 4 panel plot (using facet_grid), each with 10 lines.
Thank you :)
Upvotes: 2
Views: 537
Reputation: 1196
To remove the legend you can use
+ opts(legend.position = 'none')
To add text to a plot you can use
+ annotate("text",x=XPOSITION,y=YPOSITION,label="TEXTFORPLOT",size=3.5)
A quick dirty attempt at solving your problem
library(ggplot2)
## Read in the data from your link. You will have to change this.
dat <- read.csv("~/Downloads/demo.csv")
head(dat)
## Get the y values - turns out they are all 130
label_summary <- aggregate(dat[,2], list(dat$variable), max)
## A quick method to reduce the names, by grabbing the first 3 characters
label_names <- substr(label_summary[,1],1,3)
## get the x values of each variable
label_x <- label_summary[,2]
# A method to get the last y value of each variable
label_y <- sapply(1:length(label_x), function(i) dat[with(dat, dat[, 2]==label_x[i]&dat[, 3]==label_summary[i,1]),"value"])
# Make the plot without legend and text
p <- ggplot(data=dat,aes(x=-10:130, y=value, colour=variable)) + geom_line() + opts(legend.position = 'none')
p
# Use an sapply function to add the labels one by one to the. This is needed because the label option within the annotate function only allow one label.
p + sapply(1:length(label_x), function(i) annotate("text",x=label_x[i]+10,y=label_y[i],label=label_names[i],size=3.5))
Upvotes: 4