Trying to use ggplot to plot multiple lines into one graph, but not sure how to do so with my dataset. Not sure whether I need to change the datastructure or not (transpose?)
Data looks like this:
Company 2011 2013
Company1 300 350
Company2 320 430
Company3 310 420
I also tried it transposed:
Year Company1 Company2 Company3
2011 300 320 310
2013 350 430 420
And for this I can plot 1 of the values using;
ggplot(data=df, aes(x=Year, y=Company1)) + geom_line(colour="red") + geom_point(colour="red", size=4, shape=21, fill="white")
But I don't know how to combine all the companies as I don't have an object 'Company' anymore to group on. Any suggestions?
You should bring your data into long (i.e. molten) format to use it with ggplot2
:
library("reshape2")
mdf <- melt(mdf, id.vars="Company", value.name="value", variable.name="Year")
And then you have to use aes( ... , group = Company )
to group them:
ggplot(data=mdf, aes(x=Year, y=value, group = Company, colour = Company)) +
geom_line() +
geom_point( size=4, shape=21, fill="white")