2

I have some data which look like this:

value direction
2.2 UP
2.3 DOWN
2.4 UP
2.4 DOWN
2.5 DOWN
2.5 UP
2.5 DOWN
2.5 UP
2.6 DOWN

... etc.

I'm using ggplot2 in R and looking to make a back-to-back histogram. I can make both separately using + scale_y_reverse() .... But it is not what I need.

I need to make a back-to-back graph so that all the "DOWN" values are on the negative y axis and UP values on the positive y axis. The x axis is the "value" column and y axis is a count of the number of UP or DOWN in that range.

Can anyone help with this? So that DOWN values are counted in negative direction..

Any help is appreciated. Thanks.

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
Clare G.
  • 23
  • 1
  • 3
  • 1
    Convert all the DOWN values to negative? `data$value <- ifelse(data$direction=="DOWN",-1*value, value)` – ytk Oct 14 '16 at 16:13
  • Any updates on this question, or a piece missing from the answer below? – Mark Peterson Oct 20 '16 at 11:27
  • Sorry of the late reply. I was able to create a code that works with the help of the analyst who created the example graph I was looking to replicate. Basically, I created 3 columns in excel- one with all values, one with UP values, and one with DOWN values. From there I created breaks using the column with all values, an UP histogram, and DOWN histogram with DOWN counts being (-DOWN.hist$counts) and UP being (UP.hist$counts). Using the plot function then combined the two graphs very efficiently. – Clare G. Nov 02 '16 at 15:34
  • If anyone is interested and would like to see the code, let me know. It was a long process of trial and error. Thanks all for the help! -Clare G. – Clare G. Nov 02 '16 at 15:36

1 Answers1

3

I think the most straightforward way to do this is to calculate the bins yourself, then reverse the sign for those.

First, some sample data that are large enough to demonstrate the effect:

testData <-
  data.frame(
    value = rnorm(1000, 10, 2)
    , direction = sample(c("UP","DOWN")
                         , 1000
                         , TRUE)
  )

Then, cut the data into bins of your choosing, and modify the values to be negative for one of the groups. (using dplyr and magrittr)

toPlot <-
  testData %>%
  mutate(bin = cut(value, pretty(value, 20))) %$%
  table(bin, direction) %>%
  as.data.frame() %>%
  mutate(plotVal = ifelse(direction == "DOWN"
                          , -1*Freq
                          , Freq))

Then, pass that data.frame to ggplot and you should get what you want. (Note, this is using the dev version of ggplot2, you may need to use geom_bar(stat = "identity") instead of geom_col if you are using the stable release).

toPlot %>%
  ggplot(aes(x = bin
             , y = plotVal
             , fill = direction)) +
  geom_col()

enter image description here

Obviously, clean up labels and colors to your interests.

As a final note, depending on your use case, you may want to consider whether this "back-to-back" approach is really what you want. It makes it very hard to compare the heights of the bars.

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
  • Sorry of the late reply. I was able to create a code that works with the help of the analyst who created the example graph I was looking to replicate. Basically, I created 3 columns in excel- one with all values, one with UP values, and one with DOWN values. From there I created breaks using the column with all values, an UP histogram, and DOWN histogram with DOWN counts being (-DOWN.hist$counts) and UP being (UP.hist$counts). Using the plot function then combined the two graphs very efficiently. – Clare G. Nov 02 '16 at 15:39
  • If anyone is interested and would like to see the code, let me know. It was a long process of trial and error. Thanks all for the help! -Clare G. – Clare G. Nov 02 '16 at 15:40
  • Is there something about the answer I posted that *didn't* work for you? Just curious to see where this came up short. – Mark Peterson Nov 02 '16 at 15:51
  • No, there was no problem with your answer. It was thorough and quite helpful to understand. I did try using ggplot with your answer in mind, but found hist and plot to be simpler. If I have more time in the near future, I plan to try with ggplot again. – Clare G. Nov 08 '16 at 15:32
  • Thank you for your answer! Do you whether it would be possible to vertically shift the two plots (i.e., how to add a vertical space between the two), to make a figure such as Figure 1 from this paper (https://link.springer.com/article/10.1007/s00426-018-1112-6)? – Ladislas Nalborczyk Aug 28 '20 at 10:11