首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ggrepel将均值显示为文本标签。

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

Stack Overflow用户
提问于 2018-02-10 15:30:32
回答 1查看 1.2K关注 0票数 0

我正在创建一个图,我想在其中显示平均值。我成功地同时显示了平均值及其对应的值,但是我发现图中的平均值太混乱了,所以我想使用ggrepel::geom_label_repel以文本标签的形式显示方式,而文本标签离数据点有点远。我尝试了一些不起作用的东西,如果有人能帮我找到我想要的结果,我会很感激。谢谢。

代码语言:javascript
复制
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

代码语言:javascript
复制
# 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)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-10 20:18:03

错误位于ggrepel::geom_label_repel()中的行ggrepel::geom_label_repel()中。

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

代码语言:javascript
复制
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作为geom_label_repel(..., mapping = aes(label = mean_label))中的label参数,因此需要将mean_dat作为data参数传递给geom_label_repel

代码语言:javascript
复制
plot +
 stat_summary(
  fun.y = mean,
   geom = "point",
   colour = "darkred",
   size = 5
 ) +
 ggrepel::geom_label_repel(
  data = mean_dat,
  mapping = aes(label = mean_label),
  fontface = 'bold',
  color = 'black',
 #inherit.aes = FALSE, #would result in "error: geom_label_repel requires the following missing aesthetics: x, y"
  max.iter = 3e2,
  box.padding = 0.35,
  point.padding = 0.5,
  segment.color = 'grey50',
  force = 2
 )

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48722378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档