문제

I have data that looks like this:

Subcategory    Title   Value
Sub1           Name1   2
Sub1           Name2   5
Sub2           Name3   4
Sub2           Name4   1
Sub3           Name5   2
Sub3           Name6   7
Sub4           Name1   7
Sub4           Name2   5
Sub5           Name3   4
Sub5           Name4   3
Sub6           Name5   9
Sub6           Name6   1

Adding Title to the Row field and Sum(Value) to the Size shelf, I get a horizontal bar chart where the length of the bar is equivalent to the sum of the values for a given title. For example, a bar that is labeled Name1 and is 9 units long, another that is labeled Name2 and is 10 units long, etc. I want to color the bar such that the contribution of a given subcategory to the bar length is painted on the bar. So the Name1 bar would have a blue bar that is 2 units long stacked on a red bar that is 7 units long. The Name 2 bar would have a blue bar 5 units long stacked on an orange bar that is 5 units long.

However, when I add Subcategory to the color shelf, the scale changes so that the length of the bar is now equal to the highest value of the subcategory and the other values are not stacked, but superimposed. Instead of getting a 2 unit blue bar stacked on a 7 unit red bar for a total length of 9, I get a bar of 7 total units with 2 units painted blue.

This changes the interpretation from "What's the category with the highest values (and how do the subcategories contribute to this)?" to "What's the category with the highest subcategory?" because now the Name1 bar is now 7 units whereas the Name2 bar is now only 5 units long and you can't see the other color.

Please let me know if there's a way to do this in Tableau, or if you're a ggplot2 wizard, I'll happily take R code to do the same.

도움이 되었습니까?

해결책

Check this, but I am not sure if the coloring is right:

library("ggplot2")
p <- ggplot(data=dat, aes(x=Title, y=Value, fill=Subcategory)) +
       geom_bar(position="stack", stat="identity") +
       coord_flip()
print(p)

ggplot example

다른 팁

This should do what you want in ggplot:

ggplot(dat, aes(x = Title,y = Value, fill = Subcategory)) + 
  geom_bar(stat = "identity") +
  coord_flip()

In Tableau, put Title on Rows, Value on Columns and Subcategory on Color.

I put an example up on Tableau Public -- http://public.tableausoftware.com/views/QuantitativeStackedBars/Sheet1?:embed=y

Here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top