【问题标题】:Align labels in a geom_boxplot在 geom_boxplot 中对齐标签
【发布时间】:2020-03-17 17:04:38
【问题描述】:

我在使用以下代码时遇到了一些问题:

ggplot(data = sb11.20194, aes(y = PROMLECTURACRITICA, x = año)) +
  geom_boxplot(fill = "#3AAA35", color = "#3AAA35",outlier.color = "#95C11F",
               outlier.size = 5) + 
  ylab("Puntajes promedio de Lectura Crítica") +
  stat_boxplot(geom = "errorbar", colour = "#006633",
               width = 0.6) +
  stat_summary(geom = "crossbar", width=1.5, fatten=0, 
               color="white", 
               fun.data = function(x){ return(c(y=median(x), 
                                                ymin=median(x), 
                                                ymax=median(x))) }) +
  theme(
    panel.background = element_rect(fill = "white", colour = rgb(198,
                                                                 198,
                                                                 198, 
                                                                 maxColorValue = 255),
                                    size = 1, linetype = "solid"),    

    panel.grid.minor = element_line(size = 0.1, linetype = 'dashed',
                                    colour = rgb(198,198,198,
                                                 maxColorValue = 255)),
    axis.ticks.x = element_blank(),
    axis.text.x = element_blank(),
    axis.title.x = element_blank(),
    axis.text.y = element_text(family = "Montserrat"),
    axis.title.y = element_text(family = "Montserrat")
  ) + geom_text(data = num, aes(label = num, y = num),
                color = "#575756", hjust = -8,
                family = "Montserrat")

它给出了以下情节:

我想对齐标签。有谁知道我该怎么做?

【问题讨论】:

    标签: r ggplot2 boxplot geom-text


    【解决方案1】:

    你没有提供样本数据集,所以我自己做了一些。您可以在geom_text 中使用两个参数:nudge_xhjust。您可以按照当前在代码中使用hjust 的方式使用nudge_x。然后,我们可以使用hjust 来对齐标签。

    library(tidyverse)
    set.seed(123)
    # generate sample data and calculate quantiles
    dat <- tibble(x = rnorm(1000))
    dat_summary <- tibble(quants = quantile(dat$x))
    
    ggplot(dat, aes(x = 1, y = x))+
        geom_boxplot() +
        geom_text(data = dat_summary, aes(x = 1.5, y = quants, 
                                          label = round(quants, 2)),
                  hjust = 'outward', nudge_x = 0.1)
    

    【讨论】:

      猜你喜欢
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多