Reputation: 69
I have been trying to do a plot with 2 lines using ggplot, but it says the following: "Aesthetics must be either length 1 or the same as the data (1): x and y".
Here's the dataset I am using: unvoting <- read.csv ("https://raw.githubusercontent.com/umbertomig/intro-prob-stat-FGV/master/datasets/unvoting.csv")
Here's the question: "Examine how the median ideal points of Soviet/post-Soviet countries and all other countries have varied over all the years in the data. Plot these median ideal points by year."
Here's the code that I used so far:
pst_svt <- subset(unvoting, svtunion == 1)
othr_cts <- subset(unvoting, svtunion == 0)
y1 <- tapply(othr_cts$idealpoint, othr_cts$Year, median)
y2 <- tapply(pst_svt$idealpoint,pst_svt$Year, median)
ggplot(pst_svt) +
geom_line(aes(x= Year, y= y1, color="Other Countries")) +
geom_line(aes(x= Year, y=y2, col="Other Countries")) +
scale_color_discrete(name="Legend") +
labs(title="Variation of Median Ideal Points")
Upvotes: 1
Views: 103
Reputation: 19359
I'd do it like this. You can aggregate on Year and svtunion.
Soviet_countries <- c("Estonia", "Latvia", "Lithuania", "Belarus", "Moldova", "Ukraine", "Armenia",
"Azerbaijan", "Georgia", "Kazakhstan", "Kyrgyzstan", "Tajikistan",
"Turkmenistan", "Uzbekistan", "Russia")
library(dplyr)
library(ggplot2)
unvoting <- mutate(unvoting,
svtunion=ifelse(CountryName %in% Soviet_countries, "Soviet", "non-Soviet"))
y <- aggregate(idealpoint~Year+svtunion, FUN=median, data=unvoting)
ggplot(y) +
geom_line(aes(x=Year, y=idealpoint, col=svtunion)) +
scale_color_discrete(name="Legend") +
labs(title="Variation of Median Ideal Points")
Upvotes: 3
Reputation: 615
Below could should do it:
unvoting$svtunion<-ifelse(grepl("Russia|Georgia|Ukraine|Moldova|Belarus|Armenia|Azerbaijan| Kazakhstan|Uzbekistan|Turkmenistan|Kyrgyzstan|Tajikistan", unvoting$CountryName), 1, 0)
pst_svt <- subset(unvoting, svtunion == 1)
othr_cts <- subset(unvoting, svtunion == 0)
y1 <- tapply(othr_cts$idealpoint, othr_cts$Year, median)
y2 <- tapply(pst_svt$idealpoint,pst_svt$Year, median)
y1<-y1 %>% as.data.frame() %>% mutate(Year = rownames(y1),type = "y1")
y2<-y2 %>% as.data.frame() %>% mutate(Year = rownames(y2),type = "y2")
x<-rbindlist(list(y1,y2), use.names = T, fill = T)
colnames(x)[1]<-"median"
ggplot(x) +
geom_line(aes(x= Year, y= median,group = type ,color = type)) +
labs(title="Variation of Median Ideal Points")
Upvotes: 0