在情节中,更小的泡沫会被更大的泡沫所掩盖。如果我使用alpha,它们就会出现。我希望小气泡不使用alpha就能叠加更大的气泡。
library(ggplot2)
library(dplyr)数据集在gapminder库中提供。
library(gapminder)
data <- gapminder %>% filter(year=="2007") %>% dplyr::select(-year)
# Most basic bubble plot
data %>%
arrange(desc(pop)) %>%
mutate(country = factor(country, country)) %>%
ggplot(aes(x=gdpPercap, y=lifeExp, size=pop, color=continent)) +
geom_point(alpha=0.5) +
scale_size(range = c(.1, 24), name="Population (M)")发布于 2022-12-04 11:33:25
小的点已经画在大点上了。你需要的是要点的大纲。您可以通过选择shape = 21并为其整体颜色使用填充美学来做到这一点。他们的轮廓可以是你喜欢的任何颜色,尽管在这里我已经把它们变成了填充颜色的一个更深的版本,它给出了一个更微妙的轮廓:
library(ggplot2)
library(dplyr)
library(gapminder)
library(colorspace)
data <- gapminder %>% filter(year=="2007") %>% dplyr::select(-year)
data %>%
arrange(desc(pop)) %>%
mutate(country = factor(country, country)) %>%
ggplot(aes(gdpPercap, lifeExp, size = pop, fill = continent,
color = after_scale(darken(fill, 0.3)))) +
geom_point(shape = 21) +
scale_size(range = c(.1, 24), name = "Population (M)") +
scale_x_continuous("GDP per Capita", labels = scales::dollar) +
ylab("Life Expectancy") +
theme_minimal(base_size = 20) +
scale_fill_brewer(palette = "Pastel1") +
ggtitle("Average life expectancy 2007") +
guides(size = "none",
fill = guide_legend(override.aes = list(size = 6))) +
theme(panel.background = element_rect(fill = "gray95", color = NA),
legend.title = element_text(size = 25, face = 2),
legend.text = element_text(size = 25, face = 2))

发布于 2022-12-04 14:12:04
以下是@Allan Cameron解决方案的另一个版本:
# Libraries
library(ggplot2)
library(dplyr)
library(viridis)
library(gapminder)
gapminder %>%
filter(year=="2007") %>%
select(-year) %>%
arrange(desc(pop)) %>%
mutate(country = factor(country, country)) %>%
ggplot(aes(x=gdpPercap, y=lifeExp, size=pop, fill=continent)) +
geom_point(alpha=0.5, shape=21, color="black") +
scale_size(range = c(.1, 24), name="Population (M)", guide="none")+
scale_fill_viridis(discrete=TRUE, option="A") +
theme_bw() +
theme(legend.position="bottom") +
ylab("Life Expectancy") +
xlab("Gdp per Capita")

https://stackoverflow.com/questions/74674746
复制相似问题