我从动画图表开始,并使用gganimate软件包。我发现,随着时间的推移生成col图表动画时,变量的值与原始值会发生变化。让我给你看一个例子:
Data <- as.data.frame(cbind(c(1,1,1,2,2,2,3,3,3),
c("A","B","C","A","B","C","A","B","C"),
c(20,10,15,20,20,20,30,25,35)))
colnames(Data) <- c("Time","Object","Value")
Data$Time <- as.integer(Data$Time)
Data$Value <- as.numeric(Data$Value)
Data$Object <- as.character(Data$Object)
p <- ggplot(Data,aes(Object,Value)) +
stat_identity() +
geom_col() +
coord_cartesian(ylim = c(0,40)) +
transition_time(Time)
p
该图表获得了以下类似信息:
在Y轴上获得的值在1到6之间。似乎原始值10对应于Y轴上的值1。 15是2,20是3,依此类推...
有没有办法在图表中保留原始值?
预先感谢
答案 0 :(得分:1)
data
部分如何有效地定义data.frame)position = "identity"
来让条形图停留在同一位置。我添加了一个fill = Time
进行说明。p <- ggplot(Data, aes(Object, Value, fill = Time)) +
geom_col(position = "identity") +
coord_cartesian(ylim = c(0, 40)) +
transition_time(Time)
p
Data <- data.frame(Time = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
Object = c("A", "B", "C", "A", "B", "C", "A", "B", "C"),
Value = c(20, 10, 15, 20, 20, 20, 30, 25, 35))