0

I have a slighlty complex looking plot, created with ggplot2; with X axis having names of each point and Y axis having their values. I used geom_point to represent these. I have also added errorbars for each point on the plot.

Now I have superimposed a plot that uses only geom_hline with different linetypes to represent the samples in this data (it is a different data frame than the first, but shares the X and Y axis). I would like to show the deviations on these hlines and I am looking for inspiration as to how to depict the deviations on hlines.

I tried to add errorbars but then they appear as additional samples on the Xaxis, which is not ideal. Is it a feasible idea in the first place?

This is how it looks with the hlines, the errorbars showing up as extra points on X axis. enter image description here

Edit: Snippet of the code if it serves as an inspiration to someone trying something similar

p <- ggplot(df_sample_dots, aes(x=Names, y=Values), show.legend = TRUE) + geom_point(size=5, aes(color=factor(Names))) +
        geom_errorbar(aes(ymin=Values-dev, ymax=Values+dev), width=.2, position=position_dodge(.9), color="black")

p <- p + geom_hline(aes(yintercept=Values, linetype=Names), data=df_sample_hlines, show.legend = TRUE, color="black") + 
        geom_errorbar(aes(ymin=Values-dev, ymax=Values+dev), data = df_sample_hlines, color="thistle4", width=1, size=1)
Syamanthaka
  • 1,227
  • 2
  • 15
  • 23

1 Answers1

1

What about something like this:

library(ggplot2)
library(dplyr)

set.seed(12345)

toPlot <-
  data.frame(
    group = factor(sample(LETTERS[1:10],1000,TRUE))
  ) %>%
  mutate(value = rnorm(1000) + 
           as.numeric(group)/10)

basePlot <-
  ggplot(toPlot) +
  stat_summary(aes(x = group
                   , y = value)
               , fun.data = mean_cl_normal) +
  theme_minimal()

basePlot

lineAdd <-
  data.frame(
    x = range(as.numeric(toPlot$group)) + c(-.5,.5)
    , ymax = 1.2
    , ymin = 0.8
  )

basePlot +
  geom_ribbon(
    data = lineAdd
    , mapping = aes(x = x
                    , ymax = ymax
                    , ymin = ymin)
    , col = "light gray"
    , alpha = 0.2
  ) +
  geom_hline(yintercept = 1)

You still need to manually add each line (and generate the data for the ribbon), but if your input datasets for that are consistent, you could likely automate that as well.

enter image description here

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
  • Thank you, I'm trying this. Ribbon is a good idea. I'll keep you posted. – Syamanthaka Jul 06 '16 at 10:39
  • Mark, I find that in my specific situation, the ribbons are reducing the readability of the graph, since I have a minimum of 3 horizontal lines, sometimes upto 5. I think I'll have to stick to just showing regular errorbars on the hlines and live with the fact that they show up as additional points on the x axis. But thank you for your idea, it has been an inspiration. – Syamanthaka Jul 07 '16 at 08:28
  • I've accepted your answer, since if there were only one horizontal line, geom_ribbon would definitely be the best way to depict the min and max in this case. – Syamanthaka Jul 07 '16 at 11:33
  • 1
    With that many lines, it can be difficult. Your solution may be right. You can adjust the darkness of the ribbon (with `alpha`) -- if you put the graph on a sufficiently light background, that may be a solution. – Mark Peterson Jul 07 '16 at 12:49