-4

I've been trying for the last couple of weeks to set up a graph that i need. I want to try to have multiple variables (V2, V5, V6) on the "x" axis and how they differ by another condition (V4).

My data looks like this:

        v1              v2            v3             v4           v5                  v6
        2                1            214             2            3                   1
        2                1            214             2            4                   1
        3                1            214             2            2                   1
        4                1            214             2            3                   1
        1                1            372             1            1                   2
        3                1            552             1            2                   1

The chart i have been albe to make looks like the one on the bottom here but i would like to try to make one like on the top. https://i.stack.imgur.com/Dc8o7.jpg

So far my bar chart code is like this:

    ggplot(oll_subset, aes(factor(v2), fill =     factor(v4))) +
geom_bar(position = "dodge") 

ggplot(oll_subset, aes(factor(v4), fill = factor(v2))) +
geom_bar(position = "dodge") 

#NOT WORKING
#graph <- ggplot(oll_subset, aes(v2, v4))
#graph + geom_bar()
#graph + geom_bar(factor(x=v4))

g <- ggplot(oll_subset, aes(v2))
g + geom_bar()

g + geom_bar(aes(fill = v4))
hugstari
  • 1
  • 3
  • Anything you've already tried yourself? Why did it not work? Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This includes the code you already tried. – Heroka Feb 09 '16 at 12:48
  • Updated the original post with a little bit more information. I will try to update it more. – hugstari Feb 09 '16 at 13:13

1 Answers1

0

The problem is not your plotting, but the format of your data.

Currently, your data is in wide format, meaning you have one column for each variable.

ggplot though demands for a long format. This format can be produced using the melt function from reshape2 package.

If you use the default id variable melt will generate something similar to:

oll.subset.melt <- melt(oll.subset)

| variable | value |
|----------|-------|
| v1       | 2     |
| v1       | 2     |
| v1       | 2     |
[...]
| v5       | 1     |
| v5       | 2     |

Plot the data and they should look similar to what you want:

ggplot(data=oll.subset.melt, aes(value, fill=variable)) + geom_bar(position="dodge", stat="count")

In your data set + scale_x_log10() may be a good idea as the values are very different from each other.

sargas
  • 538
  • 1
  • 5
  • 12