0

I'm struggling to adjust this code in RStudio so that the geom_text label adjusts to mean average value for each country shown on the crossbar. Looks to me that it happens when running p2 + theme lines but I can't figure out the required adjustment.

soe <- read.csv("soe.csv", stringsAsFactors=F)

means <- aggregate(CTR ~ Audience, soe, mean)

p <- ggplot(soe, aes(x = reorder(Audience, CTR), y = CTR,)) +
     geom_jitter(position = position_jitter(height = 0, width = .1), 
              fill = "#0000FF", 
              colour = "#0000FF",
              alpha = .1) + 
    stat_summary(fun.y = mean, 
                 fun.ymin = mean, 
                 fun.ymax = mean, 
                 geom = "crossbar", 
                 width = 0.4,
                 colour = "#84888E") +
    scale_x_discrete(name = "") + 
    coord_cartesian(ylim = c(0, 1.5)) +
    labs(x = "test",
       y = "",
       title = "Click through rate by audience") +
    theme_ipsum_rc()

  #add angles on x & label mean on strip
  p2 <- p + facet_wrap(vars(Country), ncol=2, scales = "free_y")

  p2 + theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  geom_text(data = means, aes(label = round(CTR, 2), y = CTR +0.2), 
  size = 4, family = "Roboto Condensed", fontface = "bold") +
  geom_text(data = means, aes(label = ("%"), x = Audience, hjust = -1.1, y = CTR +0.2), 
  fontface = "bold") 
Phil
  • 7,287
  • 3
  • 36
  • 66
gzpach
  • 65
  • 6
  • can you provide sample data? – Humpelstielzchen Jun 17 '20 at 07:38
  • Please consider reading [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) post about reproducible example. Sharing your data like this is not a good idea. It is better to use `dput()` for example and even better (in my opinion) to use one of the base R datasets. – Paul Jun 17 '20 at 09:03
  • Try this code, it works on my R: https://pastebin.com/RNsUVTtt – Marco Sandri Jun 17 '20 at 09:18
  • this is great - thank you Marco – gzpach Jun 17 '20 at 09:27

1 Answers1

0

Here is another idea, the trick is to have variables used to facets in the dataset used for labels (I think).

Please find below a reproducible example with some ideas:

library(tidyverse) # packages with ggplot2, dplyr, etc.


df = mtcars %>% group_by(cyl, vs, gear) %>% summarise(meanmpg = mean(mpg))

ggplot(data = mtcars, aes(x = cyl, y = mpg)) +
  facet_wrap(vs~gear) +
  stat_summary(fun.y = mean, 
               fun.ymin = mean, 
               fun.ymax = mean, 
               geom = "crossbar", 
               width = 0.4,
               colour = "#84888E") +
  geom_text(data = df, aes(x = cyl, y = meanmpg, label = meanmpg), vjust = -1.1)

Output:

enter image description here

Note: mtcars dataset is included in package datasets it should be available from get go (just like iris for example)

Paul
  • 2,850
  • 1
  • 12
  • 37
  • 1
    thanks Paul. Apologies - maybe I wasn't clear enough. I'm not looking to change the size of the mean values, more so that the values change according to country and the positioning so that the bold % mean values sit above the crossbar when using facet wrap (i.e. multiple grids) – gzpach Jun 17 '20 at 08:35
  • I have changed my post, is it more in line with what you wan to achieve? – Paul Jun 17 '20 at 09:14