In my previous article, I’ve been providing an introduction to some useful graphical tools available in Plotly, an opensource library which can be used both in Python and R.

Here, I’m going to play a bit more with Plotly’s functionalities, using as input some data about USA exports in 2011. So let’s import and explore our data:

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')

df.head()

So we have a list of states with relative amount of exports of different raw foods (plus the amount of cotton).

The very first thing we might be interested in is inquiring about the amount of total exports (in dollars) for each state. We can do so with a very intuitive visualization tool, which involves a U.S. map and uses colors as an indicator of the amount of exports:

import plotly.graph_objects as go

fig = go.Figure(data=go.Choropleth(
    locations=df['code'], # Spatial coordinates
    z = df['total exports'].astype(float), 
    locationmode = 'USA-states',
    colorscale = 'Blues',
    colorbar_title = "Millions USD of exports",
))

fig.update_layout(
    title_text = '2011 US Agriculture Exports',
    geo_scope='usa', 
)

fig.show()

From the map, we can easily say that California is, by far, the state with the highest amount of exports (indeed, the darker the color, the greater the amount of total exports).

Now let’s analyze some interesting features of those exports.

First, for both fruits and veggies, we have two types: fresh and processed. We might be interested in inquiring about the ratio between those two (that means, which portion of total fruits/veggies exported is processed and which is fresh). We can do so by visualizing a bar chart which, for each state, displays the amount of those features.

So, for veggies, we have the following:

fig = go.Figure()
fig.add_trace(go.Bar(
    x=df['state'],
    y=df['veggies fresh'],
    name='fresh',
    marker_color='blue'
))
fig.add_trace(go.Bar(
    x=df['state'],
    y=df['veggies proc'],
    name='processed',
    marker_color='green'
))

fig.update_layout(barmode='group', xaxis_tickangle=-45)
fig.show()

And we can do the same for fruits:

fig = go.Figure()
fig.add_trace(go.Bar(
    x=df['state'],
    y=df['fruits fresh'],
    name='fresh',
    marker_color='blue'
))
fig.add_trace(go.Bar(
    x=df['state'],
    y=df['fruits proc'],
    name='processed',
    marker_color='green'
))

# Here we modify the tickangle of the xaxis, resulting in rotated labels.
fig.update_layout(barmode='group', xaxis_tickangle=-45)
fig.show()

We can derive two important information from the former graphs:

  • Not only California is the state with highest total exports, but also it is that with the highest fruits and veggies exports
  • In many countries, processed fruits/veggies are more or less twice the fresh ones.

Furthermore, processed and fresh fruits/veggies are positively correlated. More precisely, they exhibit a Pearson correlation coefficient equal to 1:

import seaborn as sns

df_corr = df[['veggies fresh', 'veggies proc']]
sns.heatmap(df_corr.corr(), annot= True)

df_corr = df[['fruits fresh', 'fruits proc']]
sns.heatmap(df_corr.corr(), annot= True)

We can also visualize those correlations together with the amount of total exports of that state. Let’s do it for fruits (the same reasoning holds for veggies):

import plotly.express as px
fig = px.scatter(df, x="fruits fresh", y="fruits proc",
             size="total exports", color="state",
                 hover_name="state", log_x=True, size_max=60)
fig.show()

Here we can visualize even more clearly the gap between California and other countries both in terms of fruits (you can see it in the distance between the labeled bubble and the other ones) and in terms of total exports (you can see it from the size of the bubble).

Now let’s run a similar analysis for the three items beef, pork and poultry, since I want to inquire whether they are, in some ways, correlated. It is a legit question, since one might intuitively think that, as they are different types of meat, they should be positively correlated. So let’s see whether this intuition is true:

df_corr = df[['beef','pork','poultry']]
sns.heatmap(df_corr.corr(), annot= True)

Differently from processed vs fresh fruits/veggies, here there is no relevant correlation between the three types of meats, which might be counterintuitive. Let’s visualize it in a better way, using the same bubble visualization for meat:

fig = px.scatter_3d(df, x='beef', y='pork', z='poultry',
              color='state', size = 'total exports')
fig.show()

As you can see, there is no clear pattern of data, nothing showing that higher exports of one type of meat lead to higher exports of the others.

Nice, now let’s extend our area of interest to all the items exported. We are interested in inquiring about the composition of the export portfolio of the 5 states which the highest total exports.

So once picked our states of interest:

df.nlargest(5, ['total exports'])

We can build, for each country, a pie chart. Namely, for California we will have something like that:

labels = ['beef','pork','poultry','dairy', 'total fruits', 'total veggies', 'corn', 'wheat', 'cotton']
values = df[['beef','pork','poultry','dairy', 'total fruits', 'total veggies', 'corn', 'wheat', 'cotton']].loc[df['state']=='California'].values[0]

fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])
fig.show()

And we can do the same for the remaining four countries:

It emerges that, for all the 5 countries, the main item exported is fruit (both processed and fresh). Particularly, Illinois exhibit a portion of fruit exported of almost 3/4.

The very last thing I want to inquire about, is cotton exports, as it being the only item not edible to be exported. Let’s use again a geographical representation:

fig = go.Figure(data=go.Choropleth(
    locations=df['code'],
    z = df['cotton'].astype(float), # Data to be color-coded
    locationmode = 'USA-states', 
    colorscale = 'Blues',
    colorbar_title = "Millions USD of exports",
))

fig.update_layout(
    title_text = '2011 US Cotton Exports',
    geo_scope='usa', # limite map scope to USA
)

fig.show()

This result is very interesting. First, we see that California is no longer the leading state to export: now the winner is Texas, with more than 2000 millions of USD of cotton exports. Furthermore, it is evident that cotton exports are not even contemplated into northern states, where the amount is equal to 0.

Using graphical tools to size interesting features of your data is a very useful ‘pre-step’ of your data analysis. Indeed, before deciding which features are worth your investigation, you might gather relevant stuff from just having a first glimpse (of course, following a theory you want to prove) of your data.

Advertisements

Published by valentinaalto

I'm a 22-years-old student based in Milan, passionate about everything related to Statistics, Data Science and Machine Learning. I'm eager to learn new concepts and techniques as well as share them with whoever is interested in the topic.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: