ggplot中的几个任意图例(直方图)

时间:2021-04-16 20:42:44

标签: r ggplot2 legend

我正在尝试从接下来的两个情节中制作一个情节:

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

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

我想要做的是一个情节,其中我有两组线图例“Intervalos”和“Estadísticas”。这就是我需要的:

enter image description here

1 个答案:

答案 0 :(得分:3)

试试这个:

  1. geom_vline() 函数内部的数据进行转换,以获得具有均值和 0.95 四分位数的整洁数据集。

  2. geom_vline() 美学映射到步骤 1 中的 2x2 数据集。

  3. 编辑线型和色标,添加一条带有公制字符串(¿"Medida"、"Medición"?)的线,以及另一条带有其名称(媒体、普通)的线。不幸的是,恕我直言,这看起来很难看。

     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