Bar Chart on ggplot with Python

2.6k views Asked by At

I try to create a chart in Python using ggplot library.

My data is in this format:

id total
1  3
1  4
1  7
2  3
2  2
2  5

I want to create a bar chart where every id has it's own bar. The y will be the average of total column for the specific id and also add error area with min and max for each bar.

I am new on ggplot. I have worked with scatter plots and line graph but not with bar chart.

I found that bar charts can be created with

gg = ggplot(mydata, aes(....)) + geom_bar()

But I cannot figure what to add on aes.

1

There are 1 answers

0
LondonRob On

According to the original R ggplot2 docs (I've added some bold):

The heights of the bars commonly represent one of two things: either a count of cases in each group, or the values in a column of the data frame. By default, geom_bar uses stat="bin". This makes the height of each bar equal to the number of cases in each group, and it is incompatible with mapping values to the y aesthetic. If you want the heights of the bars to represent values in the data, use stat="identity" and map a value to the y aesthetic.

This also works in the amazing Python port:

ggplot(mydata, aes(x='id', y='total')) + geom_bar(stat='identity')

Which looks like: Simple ggplot bar chart

The x ticks are obviously a bit weird in this case, but I'll leave that for another question!