I have the following data in a Pandas data frame:
Company Date Revenue Profit
ABC 08/21/16 00:00:00 500 300
ABC 08/22/16 00:00:00 600 200
ABC 08/23/16 00:00:00 650 400
ABC 08/24/16 00:00:00 625 450
ABC 08/25/16 00:00:00 675 500
ABC 08/26/16 00:00:00 680 410
XYZ 08/21/16 00:00:00 300 290
XYZ 08/22/16 00:00:00 510 400
XYZ 08/23/16 00:00:00 490 450
XYZ 08/24/16 00:00:00 475 270
XYZ 08/25/16 00:00:00 610 500
XYZ 08/26/16 00:00:00 400 250
I'm interested in building a line plot to show how the 'Revenue' and 'Profit' variables for companies ABC and XYZ evolve over time using a facet grid, as follows:
df = pd.read_csv('C:\\Users\\Desktop\\Files\\Projections', sep = ',')
df.columns = ['Company',
'Date',
'Revenue',
'Profit']
df['Date'] = pd.to_datetime(df['Date'], errors = 'coerce')
ggplot(df, aes(x='Date')) + \
geom_line(aes(y='Revenue'), color='blue') + \
geom_line(aes(y='Profit'), color='red') + \
xlab("date") + ylab("value") + ggtitle("Projections")) + \
scale_x_date() + \
facet_wrap('Company', scales='free')
Firstly, is it even possible to use facet_wrap in this case?
If so, then I understand that I have to use melt
(presumably because the data has to be in 'long' format). But, at this point, I don't understand how or why (even after reviewing this issue: Is it possible to plot multiline chart on Python ggplot?).
I thought that it would be possible to just use:
geom_line(aes(y='Revenue'), color='blue') + \
geom_line(aes(y='Profit'), color='red')
to plot the 'revenue' and 'profit' variables. But, that results in the following error message.
GgplotError: u'geom_line requires the following missing aesthetics: y'
Does anyone know how to resolve this without using melt
? Or, if it's absolutely necessary to use melt
, then perhaps set me straight on exactly how to do so in the example I provided above?
I've been banging my head against the wall trying to solve this (even tried to use Seaborn, but there is a bug in x-axis dates there!). Any help is much appreciated.
I'd even be open to just using Matplotlib for this...
Thanks in advance!
geom_line
docs? I haven't used Python ggplot but that looks similar to what you're trying to achieve.