Reputation: 13
I have panel data with ID=1,2,3... year=2007,2008,2009... and a factor foreign=0,1, and a variable X.
I would like to create a time series plot with x-axis=year, y-axis=values of X that compares the average (=mean) development of each factor over time. As there are 2 factors, there should be two lines, one solid and one dashed.
I assume that the first step involves the calculation of the means for each year and factor of X, i.e. in a panel setting. The second step should look something like this:
ggplot(data, aes(x=year, y=MEAN(X), group=Foreign, linetype=Foreign))+geom_line()+theme_bw()
Many thank.
Upvotes: 1
Views: 3549
Reputation: 1364
Using dplyr
to calculate the means:
library(dplyr)
# generate some data (because you didn't provide any, or any way or generating it...)
data = data.frame(ID = 1:200,
year = rep(1951:2000, each = 4),
foreign = rep(c(0, 1), 100),
x = rnorm(200))
# For each year, and seperately for foreign or not, calculate mean x.
data.means <- data %>%
group_by(year, foreign) %>%
summarize(xmean = mean(x))
# plot. You don't need group = foreign
ggplot(data.means, aes(x = year, y = xmean, linetype = factor(foreign))) +
geom_line() +
theme_bw()
Upvotes: 3