0

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!

3
  • Did you look at the "meat" examples towards the bottom of the geom_line docs? I haven't used Python ggplot but that looks similar to what you're trying to achieve.
    – BrenBarn
    Commented Sep 8, 2016 at 18:37
  • @BrenBarn -- Yes, I've looked at the "meat" examples. What is the 'color' in my case? Very confused...
    – equanimity
    Commented Sep 8, 2016 at 20:02
  • I believe the data is already in melted form (i.e. in 'long' form), is it not? Why do I need to use melt?
    – equanimity
    Commented Sep 8, 2016 at 20:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.