标签颜色与使用 ggrepel 的气泡填充相同
Label color the same as bubble fill using ggrepel
我有一个气泡图,我正在使用 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")
)
问题是我希望标签与气泡颜色相同,但我得到的是圆形边框的颜色。
你的color
和fill
应该在美学里面aes()
,然后ggrepel
会认出来的。我的意思是 ggrepel
使用 aes
中指定的那个
我稍微重写了您的代码:
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")
我有一个气泡图,我正在使用 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")
)
问题是我希望标签与气泡颜色相同,但我得到的是圆形边框的颜色。
你的color
和fill
应该在美学里面aes()
,然后ggrepel
会认出来的。我的意思是 ggrepel
使用 aes
中指定的那个
我稍微重写了您的代码:
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")