Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R gg在过渡期间设置圆值动画_R_Ggplot2_Bar Chart_Rounding_Gganimate - Fatal编程技术网

R gg在过渡期间设置圆值动画

R gg在过渡期间设置圆值动画,r,ggplot2,bar-chart,rounding,gganimate,R,Ggplot2,Bar Chart,Rounding,Gganimate,我想用gganimate包创建一个动画条形图。在每个条的顶部,我想把条的值四舍五入到零位 考虑以下示例: # Example data df <- data.frame(ordering = c(rep(1:3, 2), 3:1, rep(1:3, 2)), year = factor(sort(rep(2001:2005, 3))), value = round(runif(15, 0, 100)),

我想用gganimate包创建一个动画条形图。在每个条的顶部,我想把条的值四舍五入到零位

考虑以下示例:

# Example data
df <- data.frame(ordering = c(rep(1:3, 2), 3:1, rep(1:3, 2)),
                 year = factor(sort(rep(2001:2005, 3))),
                 value = round(runif(15, 0, 100)),
                 group = rep(letters[1:3], 5))

# Create animated ggplot
ggp <- ggplot(df, aes(x = ordering, y = value)) +
  geom_bar(stat = "identity", aes(fill = group)) +
  transition_states(year, transition_length = 2, state_length = 0) +
  geom_text(y = df$value, label = as.integer(round(df$value)))
ggp
#示例数据

df因为df$值已经通过round()四舍五入为零小数,所以在设置标签时可以使用as.character()

> df$value
[1] 29 81 92 50 43 73 40 41 69 15 11 66  4 69 78
> as.character(df$value)
[1] "29" "81" "92" "50" "43" "73" "40" "41" "69" "15" "11" "66" "4"  "69" "78"
结果:

ggp <- ggplot(df, aes(x = ordering, y = value)) +
  geom_bar(stat = "identity", aes(fill = group)) +
  transition_states(year, transition_length = 2, state_length = 0) +
  geom_text(label = as.character(df$value))

ggp
round(runif(15,01000),0)
?不sure@NelsonGon不幸的是,这不起作用。我认为目前还没有这样做的选择,但在这个问题上有一个“手动”解决方案,你可以很容易地适应。@Freguglia感谢你的回答!另一个线程中提供的手动解决方案的问题是,钢筋水平过渡的持续时间会缩短。在建议的线程中没有水平过渡,因此解决方案适用于其他线程的柱状图,但不适用于我的线程。你知道我如何保持水平过渡的持续时间吗?开放问题谢谢你的回答!您的解决方案的问题在于,这些值并没有反映两者之间的移动,而只是反映输入数据中定义的固定点。你知道这些值如何反映过渡运动吗?