0

I want my corr-values to be positioned as close to the facet as possible. Now, some labels are positioned lower than other. enter image description here

ggplot(df, aes(x, y)) +
    geom_point(alpha=0.8) + stat_cor(method = "pearson",size=2.5) +
    facet_wrap(~ miRNA + SYMBOL,ncol=4, scales="free") +
    theme_bw(base_size = 8)  +
    geom_smooth(method = lm, fill = "lightgray")
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
user2300940
  • 2,355
  • 1
  • 22
  • 35
  • 1
    your example data `df` is missing. – Roman Oct 31 '18 at 12:40
  • Its too large to be added – user2300940 Oct 31 '18 at 12:41
  • 1
    You can always find a way to make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example): post a representative sample of your data if it's too large, or use a commonly available dataset that reproduces the issue closely enough – camille Oct 31 '18 at 13:12
  • 1
    You should also mention where functions are coming from: this is a question about `stat_cor`, but you forgot to mention that comes from `ggpubr`, i.e. it isn't a function folks necessarily have on hand – camille Oct 31 '18 at 13:14

1 Answers1

2

You can try label.*.npc

ggplot(mtcars, aes(cyl , disp)) +
  geom_point(alpha=0.8) + 
  stat_cor(label.y.npc="top", label.x.npc = "left", method = "pearson",size=2.5) +
  facet_wrap(~ vs ,scales="free") +
  geom_smooth(method = lm, fill = "lightgray")

enter image description here

Roman
  • 17,008
  • 3
  • 36
  • 49
  • 1
    This didn't work for me, but I have struggled with the same issue and found that inherit.aes = FALSE solves it. The problem seems to be when you have colors mapped to shape/fill/etc. – Jeff Jan 24 '23 at 03:03
  • For me I had custom color mapping. If I defined my mapping within the ggplot call (i.e. `ggplot(aes(x,y, color = custom_color)))`) then adding stat_cor would fail. However, if I defined it in the the geom_point call (i.e. `ggplot(aes(x,y)) +geom_point(color = custom_color)`) then it works fine. – John Hadish Jan 31 '23 at 18:48