4

I am using ggplot in python and just trying to make a basic bar chart. For reasons I don't understand, the bar heights are corresponding to counts of variable names, rather than the actual variables.

Simple example

pattern = pd.Series(['standard', 'woolly', 'brown', 'spotted', 'red', 'wheat', 'grey'], dtype = 'category')
population = pd.Series([12, 2, 7, 3, 2, 4,5])
patternCount = pd.DataFrame({'color':pattern, 'population':population})

ggplot(aes(x = 'attribute', y = 'population'), data = animalCounts) +\
geom_bar(stat = "identity")

Gives me a bar chart that looks like this. Barplot that shows counts rather than values

I know these are counts, rather than just the number one, since if I have duplicate of any of these names, that variable shows as "2".

I presume I am making some really simple mistake here. Thanks for any help.

Edit: As per Ron Norris's request, here is the same figure, but scaled to 12, rather than 1.

as above but scaled differently

ohnoplus
  • 1,205
  • 1
  • 17
  • 29

1 Answers1

6

Apparently I need to specify

weight = 'population'

rather than

y = 'population

Thus the correct code is

p = ggplot(aes(x = 'color', weight = 'population'),data = patternCount) +\
geom_bar(stat='identity')

And gives a figure that looks like bar chart that behaves correctly

ohnoplus
  • 1,205
  • 1
  • 17
  • 29