EDIT: Solved by Haboryme in comments; the problem was my use of xlab and ylab instead of x and y as the names of keyword arguments to labs() (explaining the graph labels), and a redundant use of colour= in the second call to aes() (explaining the persistence of the original legend).
I'd like to make a stacked-area chart from some CSV data with R and ggplot2. For example:
In file "test.csv":
Year,Column with long name 1,Column with long name 2
2000,1,1
2001,1,1.5
2002,1.5,2
I run this code (imitating the answer to this GIS.SE question):
library(ggplot2)
library(reshape)
df <- read.csv('test.csv')
df <- melt(df, id="Year")
png(filename="test.png")
gg <- ggplot(df,aes(x=as.numeric(Year),y=value)) +
# Add a new legend
scale_fill_discrete(name="Series", labels=c("Foo bar", "Baz quux")) +
geom_area(aes(colour=variable,fill=variable)) +
# Change the axis labels and add a title
labs(title="Test",xlab="Year",ylab="Values")
print(gg)
dev.off()
The result, in file "test.png":
Problems: my attempt to change the axis labels was ignored, and my new legend (with code borrowed from the R Cookbook's suggestions) was added to, not substituted for, the (strangely recolored) default one. (Other solutions offered by the R Cookbook, such as calling guides(fill=FALSE), do more or less the same thing.) I'd rather not use the workaround of editing my dataframe (e.g. stripping the periods that read.csv() substitutes for spaces in column headers) so that the default labels turn out correct. What should I do?
+labs(title="Test",x="Year",y="Values")
instead, and drop colour in the aes() it doesn't bring anything to the plot. – Haboryme