Stacked bar graphs in ggplot

RedEyeUser
ggplot(aushealth, aes(x=condition, y=Population, fill=year)) +
+ geom_bar(stat="identity", position=position_dodge())

Hoping to have the years side by side, but they appear on top of each other.

Condition     Year   Population
Asthma        2001   10.0
Asthma        2017   13.1
Back Issue    2001   7.5
Back Issue    2017   6.3

What am I doing wrong?

RLave

You had some errors in the code:

tt = "Condition Year Population
Asthma 2001 10.0
Asthma 2017 13.1
Back_Issue 2001 7.5
Back_Issue 2017 6.3"

dat <- read.table(text = tt, header = T)

ggplot(dat, aes(x=Condition, y=Population, fill=as.factor(Year))) + 
  geom_bar(stat="identity", position=position_dodge())

Condition wasn't with capital C, also use as.factor on Year. That was the main issue.

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related