增加 geom_point 中的气泡大小,而不是标签。使用 ggplot2 + gganimate + ggrepel
increase bubble size in geom_point, not label. Using ggplot2 + gganimate + ggrepel
我有一个使用 gganimate 的非常简单的动画,geom_point 显示了两个类别每年的增长。
我的问题是我希望气泡大小随时间增加,但标签保持相同大小。
可重现的示例代码:
df <- tibble::tribble(
~year, ~total, ~amount, ~type, ~cumtotal, ~cumamount,
2016L, 14.3, 28.6, "A", 14.3, 28.6,
2017L, 153, 39.8, "A", 167, 68.4,
2018L, 25.2, 48.2, "A", 192, 117,
2011L, 0.2, 2.3, "B", 192, 119,
2012L, 17.8, 32, "B", 210, 151,
2013L, 11.9, 78, "B", 222, 229,
2014L, 10.7, 158, "B", 233, 387,
2015L, 16.8, 174, "B", 250, 562,
2016L, 20, 114, "B", 270, 676,
2017L, 58.7, 305, "B", 328, 980,
2018L, 33.8, 836, "B", 362, 1817
)
library(randomcoloR)
n <- length(df$type %>% unique())
palette <- unname(distinctColorPalette(n))
ggplot(df, aes(cumtotal, cumamount, size = cumtotal, colour = type, label = type)) +
geom_point(alpha = 0.75, show.legend = FALSE) +
scale_colour_manual(values = palette) +
scale_size_continuous(range = c(2, 20)) + # added this because I need the bubble to have a minimal size
scale_y_log10() +
geom_text_repel(segment.color = "slategrey",
nudge_y = 0.05,
angle = 0,
vjust = -5,
segment.size = 0.2) +
labs(title = 'Year: {frame_time}', x = 'Total', y = 'Freq') +
transition_time(year) +
ease_aes('linear')
似乎气泡大小没有移动,此外,标签很大(大小也没有移动)但主要问题是将标签固定为通常大小并沿动画保持这种方式。
期望的结果应该是这样的:
将size = cumtotal, colour = type
放入geom_point
。不知道 distinctColorPalette()
来自哪里。
library(ggrepel)
library(gganimate)
ggplot(df, aes(cumtotal, cumamount, group = type, label = type)) +
geom_point(aes(colour = type, size = cumtotal), alpha = 0.75, show.legend = FALSE) +
# scale_colour_manual(values = palette) +
scale_size_continuous(range = c(2, 20)) + # added this because I need the bubble to have a minimal size
scale_y_log10() +
geom_text_repel(segment.color = "slategrey",
nudge_y = 0.05,
angle = 0,
vjust = -5,
segment.size = 0.2) +
labs(title = 'Year: {frame_time}', x = 'Total', y = 'Freq') +
transition_time(year) +
ease_aes('linear')
我有一个使用 gganimate 的非常简单的动画,geom_point 显示了两个类别每年的增长。
我的问题是我希望气泡大小随时间增加,但标签保持相同大小。
可重现的示例代码:
df <- tibble::tribble(
~year, ~total, ~amount, ~type, ~cumtotal, ~cumamount,
2016L, 14.3, 28.6, "A", 14.3, 28.6,
2017L, 153, 39.8, "A", 167, 68.4,
2018L, 25.2, 48.2, "A", 192, 117,
2011L, 0.2, 2.3, "B", 192, 119,
2012L, 17.8, 32, "B", 210, 151,
2013L, 11.9, 78, "B", 222, 229,
2014L, 10.7, 158, "B", 233, 387,
2015L, 16.8, 174, "B", 250, 562,
2016L, 20, 114, "B", 270, 676,
2017L, 58.7, 305, "B", 328, 980,
2018L, 33.8, 836, "B", 362, 1817
)
library(randomcoloR)
n <- length(df$type %>% unique())
palette <- unname(distinctColorPalette(n))
ggplot(df, aes(cumtotal, cumamount, size = cumtotal, colour = type, label = type)) +
geom_point(alpha = 0.75, show.legend = FALSE) +
scale_colour_manual(values = palette) +
scale_size_continuous(range = c(2, 20)) + # added this because I need the bubble to have a minimal size
scale_y_log10() +
geom_text_repel(segment.color = "slategrey",
nudge_y = 0.05,
angle = 0,
vjust = -5,
segment.size = 0.2) +
labs(title = 'Year: {frame_time}', x = 'Total', y = 'Freq') +
transition_time(year) +
ease_aes('linear')
似乎气泡大小没有移动,此外,标签很大(大小也没有移动)但主要问题是将标签固定为通常大小并沿动画保持这种方式。
期望的结果应该是这样的:
将size = cumtotal, colour = type
放入geom_point
。不知道 distinctColorPalette()
来自哪里。
library(ggrepel)
library(gganimate)
ggplot(df, aes(cumtotal, cumamount, group = type, label = type)) +
geom_point(aes(colour = type, size = cumtotal), alpha = 0.75, show.legend = FALSE) +
# scale_colour_manual(values = palette) +
scale_size_continuous(range = c(2, 20)) + # added this because I need the bubble to have a minimal size
scale_y_log10() +
geom_text_repel(segment.color = "slategrey",
nudge_y = 0.05,
angle = 0,
vjust = -5,
segment.size = 0.2) +
labs(title = 'Year: {frame_time}', x = 'Total', y = 'Freq') +
transition_time(year) +
ease_aes('linear')