使用gganimate在气泡图中随时间绘制R^2值

huangapple go评论51阅读模式
英文:

Plot R^2 Value over time in bubble chart using gganimate

问题

我正在尝试使用gganimate和ggpubr来随着时间的推移以气泡图的形式动画显示趋势线的R^2,代码如下:

ggraph1 = ggplot(dataset, aes(x = bbcarshare, y = WirelessShare, size = carcount)) +
  geom_point() +
  labs(title = 'Date:{closest_state}') +
  geom_smooth(method = lm, se = FALSE) +
  stat_cor(aes(label = ..rr.label..)) +
  transition_states(Date)

但是出现了错误:Error in $<-.data.frame(*tmp*, "group", value = "") :
replacement has 1 row, data has 0

看起来这种方法可能不可行,是否有另一种方法可以显示R^2随时间变化以及geom_smooth线性回归?

英文:

I am trying to animate R^2 of a trendline over time with the help of gganimate and ggpubr, in a bubble chart like so below:

ggraph1= ggplot(dataset, aes(x=bbcarshare, y=WirelessShare, size=carcount))+
  geom_point()+
  labs(title='Date:{closest_state}')+
  geom_smooth(method=lm, se=FALSE)+
  stat_cor(aes(label=..rr.label..))+
  transition_states(Date)

however this returns an error: Error in $<-.data.frame(*tmp*, "group", value = "") :
replacement has 1 row, data has 0

It seems this method may be impossible, is there another method that could show R^2 over time along with the geom_smooth linear regression?

答案1

得分: 4

这是您要翻译的内容:

"一种选择是自己编写代码。在这里,我计算了每个观测深度值的 r^2 值,然后显示了每种情况下的第一个值。(否则文本将被多次覆盖,看起来会很糟糕,渲染速度也会变慢。)

library(tidyverse)
library(gganimate)
diamonds %>%
  mutate(rr = cor(carat, price)^2, .by = depth) %>%
  ggplot(aes(carat, price)) +
  geom_point() +
  geom_text(aes(label = round(rr, 3)), x = 3.5, y = 2000, size = 5,
            data = . %>% slice(1, .by = depth)) +
  geom_smooth(method = lm, se = FALSE) +
  transition_states(depth)

使用gganimate在气泡图中随时间绘制R^2值
"

英文:

One option would be to spin your own. Here I calculate r^2 for each observed value of depth, then display the first one in each case. (otherwise the text will get overwritten dozens or hundreds of times and will look bad and render slower.)

library(tidyverse)rr
library(gganimate)
diamonds |>
  mutate(rr = cor(carat, price)^2, .by = depth) %>%
  ggplot(aes(carat, price)) +
  geom_point() +
  geom_text(aes(label = round(rr, 3)), x = 3.5, y = 2000, size = 5,
            data = . %>% slice(1, .by = depth)) +
  geom_smooth(method = lm, se = FALSE) +
  transition_states(depth)

使用gganimate在气泡图中随时间绘制R^2值

huangapple
  • 本文由 发表于 2023年7月20日 12:06:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定