import plotnine as p9
import pandas as pd
surveys_complete = pd.read_csv('data/surveys.csv')
surveys_complete = surveys_complete.dropna()
(p9.ggplot(data=surveys_complete,
mapping=p9.aes(x='weight', y='hindfoot_length'))
+ p9.geom_point())
(p9.ggplot(data=surveys_complete,
mapping=p9.aes(x='plot_id'))
+ p9.geom_bar()
)
Adapt the bar plot of the previous exercise by mapping the sex variable to the color fill of the bar chart. Change the scale of the color fill by providing the colors blue and orange manually (see API reference to find the appropriate function).
(p9.ggplot(data=surveys_complete,
mapping=p9.aes(x='plot_id',fill='sex'))
+ p9.geom_bar()
+ p9.scale_fill_manual(["blue", "orange"])
)
(p9.ggplot(data=surveys_complete,
mapping=p9.aes(x='species_id',y='weight'))
+ p9.geom_violin()
)
(p9.ggplot(data=surveys_complete,
mapping=p9.aes(x='species_id',y='weight'))
+ p9.geom_violin()
+ p9.scale_y_log10()
)
(p9.ggplot(data=surveys_complete,
mapping=p9.aes(x='species_id',y='weight',color='factor(plot_id)'))
+ p9.geom_jitter(alpha=0.2)
+ p9.geom_boxplot()
+ p9.scale_y_log10()
)
df = surveys_complete.groupby(['year', 'species_id'])['weight'].mean()
df
df_res = df.reset_index(name='weights')
df_res
(p9.ggplot(data=df_res,mapping=p9.aes(x='year',y='weights'))
+ p9.geom_line()
+ p9.facet_wrap("species_id")
)
df2 = surveys_complete.groupby(['sex', 'year', 'species_id'])['weight'].mean().reset_index()
df2
(p9.ggplot(data=df2,mapping=p9.aes(x='year',y='weight',color='species_id'))
+ p9.geom_line()
+ p9.facet_wrap('sex')
)
(p9.ggplot(data=df2,mapping=p9.aes(x='year',y='weight',color='species_id'))
+ p9.geom_line(size=1.2)
+ p9.facet_wrap('sex')
)
(p9.ggplot(data=df2,mapping=p9.aes(x='year',y='weight',color='species_id'))
+ p9.geom_line(size=1.5)
+ p9.facet_wrap('sex')
+ p9.labs(color='Species')
)
(p9.ggplot(data=df2,mapping=p9.aes(x='year',y='weight',color='species_id'))
+ p9.geom_line(size=1.5)
+ p9.facet_wrap('sex')
+ p9.labs(color='Species')
+ p9.scales.scale_color_brewer(type='qual', palette=1)
)