jftuga
jftuga

Reputation: 1973

Stacked Area Graph in R

I have studied other similar questions on SO, but can't seem to get this working for my data.

I am aiming for this result:

desired output

This is my data frame:

    Room    Direc   MB
    Alley-10    Rx  1
    Alley-11    Rx  7
    Alley-12    Rx  11
    Alley-10    Tx  23
    Alley-11    Tx  17
    Alley-12    Tx  20

When I run:

ggplot(tp, aes(x=Room,y=MB)) + geom_area(aes(fill=factor(Direc)))

I get this result:

not working

How can I get this working?

Upvotes: 1

Views: 1628

Answers (1)

Justin
Justin

Reputation: 43265

This won't work because the Room variable is treated as a factor and thus doesn't make any sense to have continuous lines connecting.

Plotting:

ggplot(tp, aes(x=1:3, y=MB, fill=Direc)) +
   geom_area()

gives the result I think you're expecting. You can then add:

ggplot(tp, aes(x=1:3, y=MB, fill=Direc)) +
   geom_area() +
   scale_x_discrete(labels=tp$Room)

to fix the labels.

Upvotes: 5

Related Questions