1

我有一个气泡图,我正在使用 ggrepel 来避免标签重叠。

可重现的例子:

library(randomcoloR)
n <- nrow(iris)
palette <- unname(distinctColorPalette(n))


(p <- iris %>% 
  ggplot(aes(x=Sepal.Length,
             y=Sepal.Width,
             label = Species,
             color = palette)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_color_manual(values=palette)
)


(r <-
    p + geom_point(
      aes(size = Petal.Length*Petal.Width),
      pch = 21,
      show.legend = FALSE,
      fill = palette
    ) +
    scale_size_continuous(range = c(2, 30)) +
    geom_text_repel(segment.color = "orange",
                    nudge_y = 0.05,
                    angle        = 0,
                    vjust        = -5,
                    segment.size = 0.2) +
    theme(legend.position = "none")
)

问题是我希望标签与气泡颜色相同,但我得到的是圆形边框的颜色。

在此处输入图像描述

4

1 回答 1

2

Your color and fill should be inside aesthetics aes(), then ggrepel will recognise them. I mean ggrepel use that one which specified in aes I've rewrite your code a bit:

library(randomcoloR)
library(ggrepel)
n <- nrow(iris)
palette <- unname(distinctColorPalette(n))


iris %>% 
    ggplot(aes(x=Sepal.Length,
               y=Sepal.Width)) +
    geom_point(
      aes(size = Petal.Length*Petal.Width,
          fill = palette,
          color = palette),
      alpha = .7,
      pch = 21,
      show.legend = FALSE) +
    scale_size_continuous(range = c(2, 30)) +
    geom_text_repel(aes(label = Species,
                        color = palette),
                    segment.color = "orange",
                    nudge_y = 0.05,
                    angle        = 0,
                    vjust        = -5,
                    segment.size = 0.2) +
    theme(legend.position = "none")

enter image description here

于 2018-11-16T16:08:19.450 回答