Hi,
I am trying to create ggplot animation with geom_point and geom_line.
The problem is that the groups have only one observation for each year that is only one observation for each group-year pair.
I want to have a line that traces the movement of the point for each group.
How can i achieve that by changing the aesthetics or the dataframe.
Any help is appreciated.
library(haven)
master_data_clean <- read_dta("G:/replication/output/master-data-clean.dta")
library(tidyverse)
library(psych)
#>
#> Attaching package: 'psych'
#> The following objects are masked from 'package:ggplot2':
#>
#> %+%, alpha
data7= master_data_clean %>%
select(c("latrine", "mboy", "mgirl", "wealth", "hhsize", "agehead", "educhead", "boys", "girls", "sexratio", "haryana", "post","year"))
data7= as.data.frame(data7)
data8= as.data.frame( subset(data7, data7$haryana==1 & data7$post==0))
describe_data8=describe(data8, skew = FALSE, ranges = FALSE, IQR = FALSE)
describe_data8= as.data.frame(describe_data8)
data9= as.data.frame( subset(data7, data7$haryana==1 & data7$post==1))
describe_data9=describe(data9, skew = FALSE, ranges = FALSE, IQR = FALSE)
describe_data9= as.data.frame(describe_data9)
data10= as.data.frame( subset(data7, data7$haryana==0 & data7$post==0))
describe_data10=describe(data10, skew = FALSE, ranges = FALSE, IQR = FALSE)
describe_data10= as.data.frame(describe_data10)
data11= as.data.frame( subset(data7, data7$haryana==0 & data7$post==1))
describe_data11=describe(data11, skew = FALSE, ranges = FALSE, IQR = FALSE)
describe_data11= as.data.frame(describe_data11)
data_meta1= cbind(describe_data8[,3], describe_data9[,3], describe_data10[,3], describe_data11[,3])
colnames(data_meta1)= c("haryana2004", "harayana2008", "control 2004", "control2008")
rownames(data_meta1)= c("latrine", "mboy", "mgirl", "wealth", "hhsize", "agehead", "educhead",
"boys", "girls", "sexratio", "haryana", "post","year")
data_meta1= t(data_meta1)
data_meta1= as.data.frame(data_meta1)
data_meta1[,14]= c("haryana", "haryana", "control", "control")
data_meta1[,15]= c(2004,2008,2004,2008)
data_meta1[1:2, 16]= c(1,1)
data_meta1[3:4, 16]= c(2,2)
data_meta1= as.data.frame(data_meta1)
colnames(data_meta1)= c("latrine", "mboy", "mgirl", "wealth", "hhsize", "agehead", "educhead", "boys", "girls",
"sexratio", "haryana", "post","year", "haryana2", "year2", "new")
data_meta1= as.data.frame(data_meta1)
library(ggplot2)
library(gganimate)
theme_set(theme_bw())
p2 <- ggplot(data_meta1, aes(year, latrine, color = as.factor( haryana2), frame = year2)) +
geom_point() + geom_line(aes(group= new))
# Notice we added frame = year and saved the plot as p.
# We then display it as an animation with the gg_animate function:
gganimate(p2)
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
Created on 2018-04-10 by the reprex package (v0.2.0).