【问题标题】:increase bubble size in geom_point, not label. Using ggplot2 + gganimate + ggrepel增加 geom_point 中的气泡大小,而不是标签。使用 ggplot2 + gganimate + ggrepel
【发布时间】:2019-04-22 20:18:06
【问题描述】:

我有一个使用 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 移动到geom_point 中的aes
  • 库(randomcoloR)

标签: r ggplot2 gganimate ggrepel


【解决方案1】:

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') 

【讨论】:

  • 库(randomcoloR)
  • 为什么标签消失并跑回A点,然后又跑回B点? IMGine 超过 10 分。一场标签舞
  • 你的答案导致了一个微妙的错误图表。标签放弃了来回标记的气泡
  • @userRj 在ggplot() 中添加了group = type
猜你喜欢
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-29
  • 2016-10-24
  • 2018-03-30
  • 1970-01-01
相关资源
最近更新 更多