Quinn
Quinn

Reputation: 439

ggplot2 with two geoms: remove space between axis/plot area for ONE geom only (or equivalent)

I am producing a plot with 4 facets.

I thought I would attempt to produce just a plot of one part of the data first, and then facet it.

But I am having issues setting up the plot that I want. I think this is primarily because my x-axis was as a factor, but there are issues I cannot get around after converting it to numeric.

The data has a placeholder name right now, HOLD (columns transformation and replicate are factors:

   transformation replicate calibration validation difference      X1    X2      X3   X4 x1min x1max x2min x2max x3min x3max x4min x4max
1             NSE         1       0.847      0.794      0.053  185.67  0.53 1063.31 1.02   100  1200    -5     3    20   300   1.1   2.9
2             NSE         2       0.758      0.760     -0.002  552.53  0.95  235.70 1.05   100  1200    -5     3    20   300   1.1   2.9
3             NSE         3       0.813      0.817     -0.004  953.37  0.65  225.88 1.01   100  1200    -5     3    20   300   1.1   2.9
4             NSE         4       0.916      0.802      0.114 1232.67  0.86  141.11 1.01   100  1200    -5     3    20   300   1.1   2.9
5             NSE         5       0.787      0.799     -0.012  888.91  1.29  239.85 0.99   100  1200    -5     3    20   300   1.1   2.9
6             NSE         6       0.846      0.760      0.086  996.63  1.93  201.67 0.95   100  1200    -5     3    20   300   1.1   2.9
7            sqrt         1       0.864      0.817      0.047  190.57  0.57 1064.22 1.00   100  1200    -5     3    20   300   1.1   2.9
8            sqrt         2       0.793      0.763      0.030  482.99  1.07  284.29 1.03   100  1200    -5     3    20   300   1.1   2.9
9            sqrt         3       0.820      0.829     -0.009  862.64  0.71  244.69 1.01   100  1200    -5     3    20   300   1.1   2.9
10           sqrt         4       0.922      0.805      0.117 1195.74  0.88  146.52 1.02   100  1200    -5     3    20   300   1.1   2.9
11           sqrt         5       0.805      0.807     -0.002  862.64  1.49  270.43 0.96   100  1200    -5     3    20   300   1.1   2.9
12           sqrt         6       0.855      0.751      0.104  915.67  2.40  248.72 0.93   100  1200    -5     3    20   300   1.1   2.9
13            log         1       0.870      0.802      0.068  192.48  0.49 1085.72 0.99   100  1200    -5     3    20   300   1.1   2.9
14            log         2       0.817      0.734      0.083  186.41 -1.19  746.40 1.03   100  1200    -5     3    20   300   1.1   2.9
15            log         3       0.808      0.812     -0.004  820.57  0.70  247.15 1.02   100  1200    -5     3    20   300   1.1   2.9
16            log         4       0.912      0.780      0.132 1224.15  0.77  130.32 1.03   100  1200    -5     3    20   300   1.1   2.9
17            log         5       0.812      0.793      0.019  828.82  1.66  298.87 0.95   100  1200    -5     3    20   300   1.1   2.9
18            log         6       0.857      0.718      0.139  787.60  2.86  296.08 0.92   100  1200    -5     3    20   300   1.1   2.9
19            inv         1       0.854      0.659      0.195  202.73  0.24 1135.53 0.98   100  1200    -5     3    20   300   1.1   2.9
20            inv         2       0.765      0.622      0.143  186.83 -0.03  689.33 0.97   100  1200    -5     3    20   300   1.1   2.9
21            inv         3       0.689      0.684      0.005  962.95  0.27  175.91 0.98   100  1200    -5     3    20   300   1.1   2.9
22            inv         4       0.867      0.670      0.197 1436.55  0.44   91.84 0.92   100  1200    -5     3    20   300   1.1   2.9
23            inv         5       0.781      0.683      0.098  743.07  1.78  364.78 0.94   100  1200    -5     3    20   300   1.1   2.9
24            inv         6       0.773      0.626      0.147  711.62  2.78  285.22 0.92   100  1200    -5     3    20   300   1.1   2.9

Code for plots:

ggplot(data = HOLD, aes(x = as.numeric(replicate))) +

  geom_ribbon(aes(ymin = x1min-1, ymax = x1max+1), alpha = 0.25) +
  geom_jitter(aes(y = X1, color = transformation), size = 3, width = 0.125, height = 0) +

  scale_x_continuous(breaks = 1:6) +
  theme(panel.grid.minor = element_blank())

The plots are essentially x = replicate and y = X#. I'm representing this using geom_jitter, with the colouration from the factor transformation. This all works fine

However, I need to plot over the 80% confidence interval range of these X values; these are in the columns labelled with min and max. I was told that geom_hline() isn't clear enough so I opted to use geom_ribbon(). I'm aware that ribbon only works for a continuous variable so I have converted my replicate factor into numeric.

This does work, but there are gaps at the side. I know I can get rid of them by using expand() but then my values on the jitter geom will be at the edges. Is there some way I can have the ribbon go to the edges of the plot, but not the jitter? Or is there an alternative to using geom_ribbon? I have added some images to illustrate below...

enter image description here

Upvotes: 0

Views: 864

Answers (2)

erc
erc

Reputation: 10131

You can use geom_rect instead and set xmin and xmax to -Inf/Inf, but as lots of rectangles will be plotted on top of each other (one for each row), you need to decrease alpha to get the transparency.

ggplot(data = HOLD, aes(x = as.numeric(replicate))) +
  geom_rect(aes(ymin = x1min-1, ymax = x1max+1, xmin = -Inf, xmax = Inf), alpha = 0.01) +
  geom_jitter(aes(y = X1, color = transformation), size = 3, width = 0.125, height = 0) +
  scale_x_continuous(breaks = 1:6) +
  theme(panel.grid.minor = element_blank())

enter image description here

Upvotes: 2

konvas
konvas

Reputation: 14366

You can probably try to get geom_ribbon to work, if you do some transformation to the x-axis coordinates, but the easiest way to achieve your result is to use geom_rect, because it understands the xmin and xmax aesthetics. Setting xmin = -Inf and xmax = Inf ensures that the rectangle will span the whole x-axis.

As your x1min and x1max variables are equal in all rows of the dataset, you only need to draw a single rect, so it's best to add annotate("rect", ...) than geom_rect(...) to your plot.

So all you have to do is change the geom_ribbon line to

 annotate("rect", ymin = HOLD$x1min[1]-1, ymax = HOLD$x1max[1]+1, 
          xmin = -Inf, xmax = Inf, alpha = .25)

Result:

enter image description here

Upvotes: 1

Related Questions