27

A picture says more than a thousand words. As you can see, my fill is based on the variable variable.

Within each bar there is however multiple data entities (black borders) since the discrete variable complexity make them unique. What I am trying to find is something that makes each section of the bar more distinguishable than the current look. Preferable would be if it was something like shading.

http://s12.postimage.org/ltgs677ul/snapshot2.png

Here's an example (not the same dataset, since the original was imported):

dat <- read.table(text = "Complexity Method Sens Spec MMC
1 L Alpha 50 20 10
2 M Alpha 40 30 80
3 H Alpha 10 10 5
4 L Beta 70 50 60
5 M Beta 49 10 80
6 H Beta 90 17 48
7 L Gamma 19 5 93
8 M Gamma 18 39 4
9 H Gamma 10 84 74", sep = "", header=T)

library(ggplot2)
library(reshape)
short.m <- melt(dat)
ggplot(short.m, aes(x=Method, y= value/100 , fill=variable)) + 
    geom_bar(stat="identity",position="dodge", colour="black") + 
    coord_flip()
joran
  • 169,992
  • 32
  • 429
  • 468
user1476968
  • 307
  • 1
  • 5
  • 10

3 Answers3

36

This is far from perfect, but hopefully a step in the right direction, as it's dodged by variable, but still manages to represent Complexity in some way:

ggplot(short.m, aes(x=Method, y=value/100, group=variable, fill=variable, alpha=Complexity,)) + 
  geom_bar(stat="identity",position="dodge", colour="black") +
  scale_alpha_manual(values=c(0.1, 0.5, 1)) +
  coord_flip()

enter image description here

Marius
  • 58,213
  • 16
  • 107
  • 105
  • That is absolutely brilliant! Precisely what I needed to continue. – user1476968 Jun 25 '12 at 07:15
  • The only problem with this solution is that bars obscure eachother. In Alpha Spec there's actually a H bar, but it's completely hidden since it was drawn first (H comes before L and M). Since alpha values also add to eachother one can't tell which one is which (see Beta Spec). Obviously we know the middle line is for H, but the reader most likely won't. – user1476968 Jun 25 '12 at 14:04
4

Adding alpha=complexity might work:

ggplot(short.m, aes(x=Method, y= value/100 , fill=variable, alpha=complexity)) +
geom_bar(stat="identity",position="dodge", colour="black") + coord_flip()
MERose
  • 4,048
  • 7
  • 53
  • 79
Dan M.
  • 1,526
  • 1
  • 12
  • 17
  • Ill try to add in a reproducible example this monday. – user1476968 Jun 24 '12 at 13:59
  • The problem with using alpha (much like using colour) is that the bars will be dodged from eachother. What I'm ideally is trying to get is stacked alpha (Complexity) and dodged variable (which is melted MMC, Sens and Spec) – user1476968 Jun 25 '12 at 06:47
1

You might need to separate your Method and variable factors. Here are two ways to do that:

Use facet_wrap():

    ggplot(short.m, aes(x=variable, y=value/100, fill=Complexity)) + 
    facet_wrap(~ Method) + geom_bar(position="stack", colour="black") +
    scale_alpha_manual(values=c(0.1, 0.5, 1)) + coord_flip()

Use both on the x-axis:

    ggplot(short.m, aes(x=Method:variable, y=value/100, group=Method, fill=variable, alpha=Complexity,)) + 
    geom_bar(stat="identity", position="stack", colour="black") +
    scale_alpha_manual(values=c(0.1, 0.5, 1)) + coord_flip()
Dan M.
  • 1,526
  • 1
  • 12
  • 17
  • I'll try it first thing tomorrow morning and see how it works out. ;) – user1476968 Jun 25 '12 at 16:54
  • The issue with this solution is that it doesn't really stack the bars, it just adds them on top of each other. As such two equally long Spec MC may appear different lengths since they aren't positioned parallel. – user1476968 Jun 26 '12 at 07:35
  • Ok, I think I now understand what you were trying to do. In ggplot, "stack" means add them on top of each other. To my knowledge, the kind of stack you want is not possible in ggplot. I think this is your best option: `ggplot(short.m, aes(x=variable, y=value/100, fill=Complexity)) + facet_wrap(~ Method) + geom_bar(position="dodge", colour="black") + coord_flip()` or maybe `ggplot(short.m, aes(x=Method:variable, y=value/100, fill=Complexity)) + geom_bar(position="dodge", colour="black") + coord_flip()` – Dan M. Jun 26 '12 at 12:56