r - 使用 gganimate 将文本保留在每个构面窗口内
问题描述
我希望我的刻面标签在图中,所以我使用了 geom_text()。情节很好,但是当我添加时transition_reveal(Year)
出现以下错误:错误:along data must be the same class in all layers
。我删除了 geom_text,它工作正常。有没有办法让它工作并在每个面板内添加文本?
阴谋
ggplot(data=df,aes(colour=Element, x=Year,y=Value))+
geom_point()+
geom_line()+
facet_grid(Element~.,scales = "free_y")+
scale_x_continuous(breaks = c(seq(1975,2015, by=5),2019))+
geom_text(data = df_text,aes(label=lab,y=value2),colour="black",vjust="right",hjust="left")+
scale_colour_jco()+
theme(panel.background = element_rect(fill = NA),
panel.border = element_rect(linetype = "solid",fill = NA),
axis.text.y=element_text(size=13,margin=margin(5,8,5,5,"pt"), colour = "black"),
axis.text.x= element_text(size=13,margin=margin(5,5,5,5,"pt"), colour = "black"),
axis.title =element_blank(),
axis.ticks.length=unit(-0.15, "cm"),
strip.text = element_blank(),
legend.position = "none")
数据
df<-structure(list(Year = c(1975L, 1978L, 1980L, 1985L, 1988L, 1990L,
1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L,
2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L,
2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L,
2018L, 1975L, 1978L, 1980L, 1985L, 1988L, 1989L, 1990L, 1991L,
1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L,
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L,
2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2018L
), Value = c(29.07, 38.43, 48.65, 82.28, 142.87, 180.77, 205.65,
261.35, 371.9, 510.81, 631.45, 713.6, 802.59, 901.19, 1018.97,
1191.25, 1368.55, 1583.51, 1869.4, 2270.16, 2687.46, 3183.18,
3750.16, 4401.56, 4853.87, 5749.02, 6725.76, 7424.32, 8147.32,
8850.54, 9472.4, 10193.29, 11037.28, 12001.52, 5742206, 5853321,
5961129, 6267223, 6516920, 6571597, 6666482, 6709277, 6731072,
6753497, 6785291, 6846346, 6902677, 6954391, 6995666, 7029707,
7066481, 7104850, 7156537, 7206806, 7311228, 8195500, 8294200,
8386700, 8456100, 8500300, 8715100, 8868500, 8868500, 8964100,
9046200, 9097000, 9204000, 9290500, 9394800), Element = c("GDP",
"GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "GDP",
"GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "GDP",
"GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "GDP",
"GDP", "GDP", "GDP", "GDP", "GDP", "GDP", "Population", "Population",
"Population", "Population", "Population", "Population", "Population",
"Population", "Population", "Population", "Population", "Population",
"Population", "Population", "Population", "Population", "Population",
"Population", "Population", "Population", "Population", "Population",
"Population", "Population", "Population", "Population", "Population",
"Population", "Population", "Population", "Population", "Population",
"Population", "Population", "Population")), class = "data.frame", row.names = c(NA,
-69L))
文本数据
df_text<-structure(list(Year = c(1975, 1975), value2 = c(8455320, 10801.368
), Value = c(9e+06, 11000), Element = c("Population", "GDP"),
lab = c("Population (Persons)", "Gross Primary Product (100 million RMB)"
)), class = "data.frame", row.names = c(NA, -2L))
我想要动画的情节:
解决方案
Year
两个数据框中的列之间存在细微差别。df$Year
is of classinteger
而df_text$Year
is of class numeric
。
所以如果你这样做
df_text$Year <- as.integer(df_text$Year)
然后运行您的绘图代码:
library(ggplot2)
library(gganimate)
library(ggsci)
ggplot(data = df, aes(colour = Element, x = Year, y = Value)) +
geom_point() +
geom_line() +
facet_grid(Element~., scales = "free_y")+
scale_x_continuous(breaks = c(seq(1975, 2015, by = 5), 2019)) +
scale_y_continuous(labels = scales::comma) +
geom_text(data = df_text, aes(label = lab, y = value2),
colour = "black", vjust = "right", hjust = "left") +
scale_colour_jco() +
theme(panel.background = element_rect(fill = NA),
panel.border = element_rect(linetype = "solid", fill = NA),
axis.text.y = element_text(size = 13,
margin = margin(5, 8, 5, 5, "pt"),
colour = "black"),
axis.text.x = element_text(size = 13,
margin = margin(5, 5, 5, 5, "pt"),
colour = "black"),
axis.title = element_blank(),
axis.ticks.length = unit(-0.15, "cm"),
strip.text = element_blank(),
legend.position = "none") +
transition_reveal(Year)
你会得到:
推荐阅读
- typescript - 错误:无效的 json:.↵ 在 XMLHttpRequest 贝宝沙箱角度 8
- python - 在 Python 中使用 schedule() 时,我们如何找出特定时间活动的作业数量?
- javascript - 获取对象Javascript中的元素
- powershell - 如何建立从本地计算机到虚拟机的 powershell 会话
- c# - C#分页问题
- javascript - 如何为android webview实现@JavascriptInterface
- assembly - 条件跳转未按 NASM 的预期工作
- javascript - 使用谷歌应用脚本在电子表格中滚动到今天的日期
- powershell - 在 PowerShell 中重用正在运行的 PuTTY 代理(选美)
- python - 有没有办法在python中加快这个速度?