r - 过渡期间的 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)),
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$value 已经通过 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))
推荐阅读
- javascript - 抛出某些 Redux 操作时,将 javascript 功能放在哪里?
- azure-active-directory - 希望为 myapps.Microsoft.com 配置自定义 URL
- python - 不和谐机器人问题
- asp.net-core - 运行时是否需要 .NET Core SDK?
- node.js - pubsub 如何知道我在某个时间点发布了多少条消息?
- oracle - 为什么我会遇到 PL/SQL: ORA-00933: SQL command not properly end?
- elasticsearch - Elasticsearch 动态分析器和同义词
- javascript - 当我将我的快速代码从路由文件移动到控制器文件时,api 不起作用。从邮递员那里点击 api 会导致一个无休止的请求
- javascript - 如何删除嵌套在两个数组中的对象?
- flutter - 如何在 Flutter 中获取异步值