2

I'm starting with animated charts and using gganimate package. I've found that when generating a col chart animation over time, values of variables change from original. Let me show you an example:

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  

The chart obtained loks like this:

enter image description here

Values obtained in the Y-axis are between 1 and 6. It seems that the original value of 10 corresponds to a value of 1 in the Y-axis. 15 is 2, 20 is 3 and so on...

Is there a way for keeping the original values in the chart?

Thanks in advance

sertsedat
  • 3,490
  • 1
  • 25
  • 45
Mikelof
  • 21
  • 2

1 Answers1

1

1

  1. Your data changed when you coerced a factor variable into numeric. (see data section how to efficiently define a data.frame)
  2. You were missing a position = "identity" for your bar charts to stay at the same place. I added a fill = Time for illustration.

Code

p <- ggplot(Data, aes(Object, Value, fill = Time)) +
     geom_col(position = "identity") +
     coord_cartesian(ylim = c(0, 40)) +
     transition_time(Time)
p  

Data

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))
Roman
  • 4,744
  • 2
  • 16
  • 58