0

I am trying to change the style settings of this kind of chart and hope you can help me.

R code:

set_theme(theme_bw)

cglac$pred2<-as.factor(cglac$pred)

ggplot(cglac, aes(x=depth, colour=pred2))
    + geom_bar(aes(y=..density..),binwidth=3, alpha=.5, position="stack")
    + geom_density(alpha=.2)
    + xlab("Depth (m)")
    + ylab("Counts & Density")
    + coord_flip()
    + scale_x_reverse()
    + theme_bw()

which produces this graph:

enter image description here

Here some points:

  1. What I want is to have the density line as black and white lines separated by symbols rather than colour (dashed line, dotted line etc).

  2. The other thing is the histogram itself. How do I get rid of the grey background in the bars?

  3. Can I change the bars also to black and white symbol lines (shaded etc)? So that they would match the density lines?

  4. Last but not least I want to add a second x or in this case y axis, because of flip_coord(). The one I see right now is for the density. The other one I need would then be the count data from the pred2 variable.

Thanks for helping.

Best, Moritz

CSchulz
  • 10,882
  • 11
  • 60
  • 114
Moritz Schmid
  • 395
  • 6
  • 16

1 Answers1

0
  1. Have different line types: inside aes(), put linetype = pred2. To make the line color black, inside geom_density, add an argument color = "black".

  2. The "background" of the bars is called "fill". Inside geom_bar, you can set fill = NA for no fill. A more common approach is to fill in the bars with the colors, inside aes() specify fill = pred2. You might consider faceting by your variable, + facet_wrap(~ pred2, nrow = 1) might look very nice.

  3. Shaded bars in ggplot? No, you can't do that easily. See the answers to this question for other options and hacks.

  4. Second y-axis, similar to the shaded symbol lines, the ggplot creator thinks a second y-axis is a terrible design choice, so you can't do it at all easily. Here's a related question, including Hadley's point of view:

I believe plots with separate y scales (not y-scales that are transformations of each other) are fundamentally flawed.

It's definitely worth considering his point of view, and asking yourself if those design choices are really what you want.

Different linetypes for densities

Here's my built-in data version of what you're trying to do:

ggplot(mtcars, aes(x = hp,
                   linetype = cyl,
                   group = cyl,
                   color = cyl)) +
    geom_histogram(aes(y=..density.., fill = cyl),
                   alpha=.5, position="stack") +
    geom_density(color = "black") +
    coord_flip() +
    theme_bw()

And what I think you should do instead. This version uses facets instead of stacking/colors/linetypes. You seem to be aiming for black and white, which isn't a problem at all in this version.

ggplot(mtcars, aes(x = hp,
                   group = cyl)) +
    geom_histogram(aes(y=..density..),
                   alpha=.5) +
    geom_density() +
    facet_wrap(~ cyl, nrow = 1) +
    coord_flip() +
    theme_bw()
Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thanks Gregor. To point 4. Well, in same cases I am sure one should not have data that needs more than one axis. But what's wrong with having the histogram/bars together with a density function? – Moritz Schmid Oct 21 '14 at 23:59
  • If your total counts are the same for the three groups and just the depth distribution is different, then it is a simple multiplicative transformation, which is fine! (Though still difficult to implement in `ggplot`.) But if your total counts are different for the three groups, not so good. – Gregor Thomas Oct 22 '14 at 00:09
  • Gregor, I don't have a geom_line. I assume you mean geom_density? Can you show me how to do it with my geom_density? When I try adding aes into it it reduces my density line to just one line. – Moritz Schmid Oct 22 '14 at 00:19