Stacked bar chart with continuous Y variables

ggplot2
#1

*Updated with an example
Hello,
I am trying to make a stacked bar chart where the x-axis is categorical, by the y-axis is continuous. As it is, my code produces a bar chart where instead of a stacked chart, the bars are colored to designate the value for the second y-value. I want to produce a typical stacked chart where the color changes where the first y-variable ends. Here is my current code:

library(ggplot2)
Bar <- ggplot(iris,aes(Species,Sepal.Width,fill=Sepal.Length))+
  geom_col()
Bar

Created on 2019-02-05 by the reprex package (v0.2.1)
Thanks!

0 Likes

#2

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

1 Like

#3

I think I accomplished that. Thanks!

0 Likes

#4

A stacked bar chart is intended usually with color variables that are factor variables rather than continuous. Your code is mapping the fill aesthetic to a continuous variable, Sepal.Length, which is why ggplot2 is using colors in this way. If you want a continuous y-axis and want a stacked bar chart, the variable you map onto the fill aesthetic needs to be a factor variable. You can see some example plots/code here:

2 Likes

#5

You were very close on your reprex, the idea was to use a small sample of your own data, but if you want to use the iris data set, there is no need for the df_paste() call, since iris comes with R by default.

0 Likes

#6

I don't understand what you're going for. Could you share a drawing of your goal?

0 Likes

#7

Nice, art by accident!
:slight_smile:

0 Likes

#8

How do you expect this to work? I honestly don't even know what it means to "stack a continuous variable"

0 Likes

closed #9

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes