0

I'm trying to make a single plot out of the next two:

datos <- rnorm(100000)
datos <- data.frame(boot=datos)
ggplot(datos, aes(x=boot)) + 
  geom_histogram(aes(y=..density..),
                 colour="white",
                 fill="steelblue")+
  geom_density(alpha=.2, fill="gold",col="gold",lwd=0.75) +
  geom_vline(aes(xintercept = mean(datos$boot),color="Media"),
             lwd=0.75)+
  geom_vline(aes(xintercept = quantile(datos$boot,0.95)),
             lwd=0.75,linetype="dashed")+
  geom_hline(yintercept = 0,col="black",lwd=1)+
  scale_color_manual(name = "Estadísticas",
                     values = c(Media = "red"))+
  labs(title=latex2exp::TeX("Figura 1: Histograma De las $R^2_{adj}$ obtenidas por \\textit{bootstrap}"),
       subtitle="Experimento realizado con 10,000 muestras independientes",
       y="Densidad estimada", x=latex2exp::TeX("$R^2_{adj}$"))

enter image description here

And

datos <- rnorm(100000)
datos <- data.frame(boot=datos)
ggplot(datos, aes(x=boot)) + 
  geom_histogram(aes(y=..density..),
                 colour="white",
                 fill="steelblue")+
  geom_density(alpha=.2, fill="gold",col="gold",lwd=0.75) +
  geom_vline(aes(xintercept = mean(datos$boot)),
             lwd=0.75)+
  geom_vline(aes(xintercept = quantile(datos$boot,0.95),color="Normal"),
             lwd=0.75,linetype="dashed")+
  geom_hline(yintercept = 0,col="black",lwd=1)+
  scale_color_manual(name = "Intervalos",
                     values = c(Normal = "green"))+
  labs(title=latex2exp::TeX("Figura 1: Histograma De las $R^2_{adj}$ obtenidas por \\textit{bootstrap}"),
       subtitle="Experimento realizado con 10,000 muestras independientes",
       y="Densidad estimada", x=latex2exp::TeX("$R^2_{adj}$"))

enter image description here

What I want to do is a single plot in which I have two groups of line legends "Intervalos" and "Estadísticas". This is what I need:

enter image description here

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Apr 16 '21 at 20:45
  • Possible duplicate: https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot – MrFlick Apr 16 '21 at 20:45
  • I've made it simpler now – Edgar Alarcón Apr 16 '21 at 20:52
  • Not really MrFlick... I need two groups of plot – Edgar Alarcón Apr 16 '21 at 20:57
  • Then maybe something like this instead: https://stackoverflow.com/questions/46683956/create-two-legends-for-one-ggplot2-graph-and-modify-them. ggplot doesn't like two make two legends for the same aesthetic so you're going to have to do some hacking. – MrFlick Apr 16 '21 at 21:00
  • Or another guide here: https://www.r-bloggers.com/2015/11/multiple-legends-for-the-same-aesthetic-2/ – MrFlick Apr 16 '21 at 21:01
  • None of this work for me, since I want to add lines to a histogram MrFlick, not just lines... – Edgar Alarcón Apr 16 '21 at 21:06

1 Answers1

3

Try this:

  1. Transform the data inside the geom_vline() function to obtain a tidy dataset with the mean and the 0.95 quartile.

  2. Map the geom_vline() aesthetics to the 2x2 dataset from step 1.

  3. Edit the linetype and colour scales to add a line with the metric string (¿"Medida", "Medición"?), and another one with its name (media, normal). Unfortunately, IMHO, this looks ugly.

     ggplot(datos, aes(x=boot)) + 
     geom_histogram(aes(y=..density..),
                    colour="white",
                    fill="steelblue")+
     geom_density(alpha=.2, fill="gold",col="gold",lwd=0.75) +
     geom_vline(data = (. %>% summarise(media = mean(boot), 
                                       normal = quantile(boot, 0.95)) %>% 
                              gather(key = Medida, value = valor)),
              aes(xintercept = valor, color = Medida, linetype = Medida),
              lwd=0.75) +
      geom_hline(yintercept = 0,col="black",lwd=1) +
      scale_color_manual(name = "Medida",
                      values = c("media" = "red", "normal" = "green"),
                      labels = c("ESTADÍSTICAS\nmedia", "INTERVALOS\nnormal" )) +
      scale_linetype_manual(name = "Medida",
                         values = c("media" = 1, "normal" = 2),
                         labels = c("ESTADÍSTICAS\nmedia", "INTERVALOS\nnormal" )) +
     labs(title=latex2exp::TeX("Figura 1: Histograma De las $R^2_{adj}$ obtenidas por \\textit{bootstrap}"),
          subtitle="Experimento realizado con 10,000 muestras independientes",
          y="Densidad estimada", 
          x=latex2exp::TeX("$R^2_{adj}$"))
    

enter image description here

Nicolás Velasquez
  • 5,623
  • 11
  • 22