如何用transition_states()显示叠加图的过渡?
How to use transition_states() to show the transition of superimposed plot?
如何使用transition_states()显示叠加图的过渡?
I wish to highlight particular data points from the data using the package of `gganimate`.
这是我尝试的一个非常简单的例子。我创建了第一个数据集和仅包含 2 个数据点的绘图。
#create df
df1= data.frame(x=c(1,2),y=c(2,3))
#plot
p1<- ggplot(df1,aes(x,y))+geom_point()
然后,我想用不同的颜色突出显示一个数据点 (x=2,y-3),所以我创建了第二个数据集并叠加在第一个图上以显示差异。
df2= data.frame(x=2,y=3)
p1+ geom_point(data=df2,color="red")
是否可以使用 transition_states()
来显示从 df1 到 df2 的转换?
p1+transition_states(geom_point(data=df2,color="red"))
Error in order(ind) : argument 1 is not a vector
非常感谢任何建议!
为此,您可以构建一个 'long' data.frame
,其中包含所有数据,最重要的是包含指定时间或状态的列。所以第一步是将 df1
和 df2
合并或绑定到一个长数据帧 df
.
library(dplyr)
df1= data.frame(x=c(1,2),y=c(2,3), color = "black", time = 1, stringsAsFactors = F)
df2= data.frame(x=2,y=3, color = "red", time = 2, stringsAsFactors = F)
df <- bind_rows(df1, df2)
> df
x y color time
1 1 2 black 1
2 2 3 black 1
3 2 3 red 2
我也已经添加了颜色列。我从这里创建动画:
ggplot(df, aes(x = x, y = y, color = color)) +
geom_point(size = 10, show.legend = F) +
scale_color_manual(values = c("black", "red")) +
transition_states(time)
由于这是一个相当 'static' 的动画,因此还可以通过添加额外的步骤 (df3
) 更清楚地显示数据的转换:
df1= data.frame(x=c(1,2),y=c(2,3), color = "black", time = 1, stringsAsFactors = F)
df2= data.frame(x=2,y=3, color = "black", time = 2, stringsAsFactors = F)
df3= data.frame(x=2,y=3, color = "red", time = 3, stringsAsFactors = F)
df <- bind_rows(df1, df2, df3)
ggplot(df, aes(x = x, y = y, color = color)) +
geom_point(size = 10, show.legend = F) +
scale_color_manual(values = c("black", "red")) +
transition_states(time)
既然你想叠加你的情节,你可以使用shadow_mark
命令:
ggplot(df, aes(x = x, y = y, color = color)) +
geom_point(size = 10, show.legend = F) +
scale_color_manual(values = c("black", "red")) +
transition_states(time) +
shadow_mark()
Here are some additional information.
如何使用transition_states()显示叠加图的过渡?
I wish to highlight particular data points from the data using the package of `gganimate`.
这是我尝试的一个非常简单的例子。我创建了第一个数据集和仅包含 2 个数据点的绘图。
#create df
df1= data.frame(x=c(1,2),y=c(2,3))
#plot
p1<- ggplot(df1,aes(x,y))+geom_point()
然后,我想用不同的颜色突出显示一个数据点 (x=2,y-3),所以我创建了第二个数据集并叠加在第一个图上以显示差异。
df2= data.frame(x=2,y=3)
p1+ geom_point(data=df2,color="red")
是否可以使用 transition_states()
来显示从 df1 到 df2 的转换?
p1+transition_states(geom_point(data=df2,color="red"))
Error in order(ind) : argument 1 is not a vector
非常感谢任何建议!
为此,您可以构建一个 'long' data.frame
,其中包含所有数据,最重要的是包含指定时间或状态的列。所以第一步是将 df1
和 df2
合并或绑定到一个长数据帧 df
.
library(dplyr)
df1= data.frame(x=c(1,2),y=c(2,3), color = "black", time = 1, stringsAsFactors = F)
df2= data.frame(x=2,y=3, color = "red", time = 2, stringsAsFactors = F)
df <- bind_rows(df1, df2)
> df
x y color time
1 1 2 black 1
2 2 3 black 1
3 2 3 red 2
我也已经添加了颜色列。我从这里创建动画:
ggplot(df, aes(x = x, y = y, color = color)) +
geom_point(size = 10, show.legend = F) +
scale_color_manual(values = c("black", "red")) +
transition_states(time)
由于这是一个相当 'static' 的动画,因此还可以通过添加额外的步骤 (df3
) 更清楚地显示数据的转换:
df1= data.frame(x=c(1,2),y=c(2,3), color = "black", time = 1, stringsAsFactors = F)
df2= data.frame(x=2,y=3, color = "black", time = 2, stringsAsFactors = F)
df3= data.frame(x=2,y=3, color = "red", time = 3, stringsAsFactors = F)
df <- bind_rows(df1, df2, df3)
ggplot(df, aes(x = x, y = y, color = color)) +
geom_point(size = 10, show.legend = F) +
scale_color_manual(values = c("black", "red")) +
transition_states(time)
既然你想叠加你的情节,你可以使用shadow_mark
命令:
ggplot(df, aes(x = x, y = y, color = color)) +
geom_point(size = 10, show.legend = F) +
scale_color_manual(values = c("black", "red")) +
transition_states(time) +
shadow_mark()
Here are some additional information.