使用ggrepel将平均值显示为文本标签

时间:2018-02-10 15:30:32

标签: r ggplot2 data-visualization tidyverse ggrepel

我正在创建一个我希望显示平均值的图。我已经设法显示平均值和相应的值,但是我发现这些图表太杂乱了,所以我想用ggrepel::geom_label_repel来显示文本标签形式的方法。从数据点稍微移开一点。我尝试了一些不起作用的东西,如果有人可以帮我弄清楚如何能得到理想的结果,我会很感激。感谢。

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.4.3
library(ggrepel)
#> Warning: package 'ggrepel' was built under R version 3.4.2


# function to plot mean
fun_mean <- function(x) {
  return(data.frame(
    y = as.numeric(as.character(mean(x, na.rm = TRUE))),
    label = as.numeric(as.character(mean(x, na.rm = TRUE)))
  ))
}

# preparing the basic plot
plot <-
  ggplot2::ggplot(data = iris,
                  mapping = aes(x = Species, y = Sepal.Length)) +
  geom_point(
    position = position_jitterdodge(
      jitter.width = NULL,
      jitter.height = 0.2,
      dodge.width = 0.75
    ),
    alpha = 0.5,
    size = 3,
    aes(color = factor(Species))
  ) +
  geom_violin(width = 0.5,
              alpha = 0.2,
              fill = "white") +
  geom_boxplot(
    width = 0.3,
    alpha = 0.2,
    fill = "white",
    outlier.colour = "black",
    outlier.shape = 16,
    outlier.size = 3,
    outlier.alpha = 0.7,
    position = position_dodge(width = NULL)
  ) +
  theme(legend.position = "none")


# add the mean label to the plot
plot <- plot +
  stat_summary(
    fun.y = mean,
    geom = "point",
    colour = "darkred",
    size = 5
  ) +
  stat_summary(
    fun.data = fun_mean,
    geom = "text",
    vjust = -1.0,
    size = 5
  )

# see the plot
plot

# adding geom_label_repel
plot <-
  plot +
  ggrepel::geom_label_repel(
    mapping = aes(label = mean),
    fontface = 'bold',
    color = 'black',
    inherit.aes = FALSE,
    max.iter = 3e2,
    box.padding = 0.35,
    point.padding = 0.5,
    segment.color = 'grey50',
    force = 2
  )

plot # doesn't work :(
#> Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
#> Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 0, 150

reprex package创建于2018-02-10(v0.1.1.9000)。

1 个答案:

答案 0 :(得分:1)

错误在于mapping = aes(label = mean)中的ggrepel::geom_label_repel()行。

您可以尝试以下操作:首先创建一个数据集,其中包含变量Species的每个Sepal.Length的平均值。

mean_dat <- aggregate(data = iris[, c(1, 5)], . ~Species, FUN = mean)
names(mean_dat) <- c("Species", "mean_label") # it is not necessary to rename the columns but it might avoid confusion
mean_dat
#     Species mean_label
#1     setosa      5.006
#2 versicolor      5.936
#3  virginica      6.588

我们会将此数据集中的mean_label列用作label中的geom_label_repel(..., mapping = aes(label = mean_label))参数,因此我们需要将mean_dat传递给geom_label_repel作为{{1}参数。

data

enter image description here