英文:
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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论