11

I have the following code:

library(tidyverse)
data_frame(x = 1:5, x1=x+1, c = c('a','a','a','b','b')) %>% 
      ggplot() +
      geom_curve(aes(x = x, xend= x1, y = 0, yend = 0), curvature = -1.3, alpha=.2) +
    facet_wrap(~ c, ncol=1)

enter image description here but I would like to tweak the limits of the y axis to cut the background area above ~ .1.

I tried to do this:

data_frame(x = 1:5, x1=x+1, c = c('a','a','a','b','b')) %>% 
  ggplot() +
  geom_curve(aes(x = x, xend= x1, y = 0, yend = 0), curvature = -1.3, alpha=.2) +
  facet_grid(c ~ .) +
  ylim(0,.35) +
  facet_wrap(~ c, ncol=1)

but it simply rescales the archs based on the values in ylim. How can I prevent this behavior?

Dambo
  • 3,318
  • 5
  • 30
  • 79
  • 1
    It seems that `geom_curve` calls `curveGrob` .. but the default argument for `default.units` is `"npc"` .. this could be the cause of the rescaling .. unfortunately, I can't find a way to pass this argument through `geom_curve`,so you will have to deal with `curveGrob` directly ... you may be intersted by [this](https://stackoverflow.com/questions/34911084/ggplot2-curvegrob-annotation-custom) and by [this](https://stackoverflow.com/questions/17311917/ggplot2-the-unit-of-size) – MrSmithGoesToWashington Feb 20 '18 at 09:50
  • 2
    Your code excerpt doesn't produce the first pasted plot. Did you have `+ facet_wrap(~c, ncol=1)` or something in there? Also, are you calling `library(tidyverse)` at the top? Being explicit will help answerers – arvi1000 Feb 21 '18 at 17:52
  • @arvi1000 thanks I fixed it – Dambo Feb 23 '18 at 15:38

2 Answers2

4

coord_fixed() has arguments that allow you to control precisely what you would like to have here.

See also http://ggplot2.tidyverse.org/reference/coord_fixed.html for reference.

Unfortunately, it is however not possible to use your x and x1 in a dynamic way inside coord_fixed().

As long as you are fine putting absolute values (0.6 and 6.4 below), you can however do something like this:

data_frame(x = 1:5, x1 = x+1, c = c('a','a','a','b','b')) %>%
  ggplot(.) +
  geom_curve(aes(x = x, xend = x1, y = 0, yend = 0), curvature = -1.3, alpha = .2) +
  facet_grid(c ~ .) +
  coord_fixed(ratio = 7, xlim = c(0.6, 6.4), ylim = c(0, 0.12), expand = FALSE) +
  scale_y_continuous(breaks = c(0, 0.1))

Assuming this looks like what you would want it to look like, note that I set expand = FALSE to start ylim at zero, and added buffers to xlim (0.4) and the upper bound of ylim.
I have modified the default ratio value from 1 to 7, to scale you back down from the 0.7 to 0.1, which is what I understand you would like to have in the end. ratio = 1 would imply that you have the same scale (same distances) on the y-axis as on the x-axis (which is what you refer to as re-scaling I believe).

Finally I had to add the manual breaks for the y-axis (to have fewer ones), such that the grid boxes would be a bit larger, which again is just what I infer as your possible wish.

RolandASc
  • 3,863
  • 1
  • 11
  • 30
3

Does replacing ylim(0,.35) with coord_fixed(ylim=c(0, 0.35)) do what you want?

  • If I use ~.7 instead of .35 it works for me because I am mapping no variable to the y axis, thus it visually does what I need. However, it still rescales the heights of my archs, which is a bitt odd to me. – Dambo Jan 05 '18 at 16:10