1

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")
4
  • svtunion is not in that dataset. Commented May 14, 2020 at 4:03
  • 1
    how did you create the svtunion column that you're subsetting by? Commented May 14, 2020 at 4:11
  • I have used this code for svtunion: svtunion <- c("Estonia", "Latvia", "Lithuania", "Belarus", "Moldova", "Ukraine", "Armenia", "Azerbaijan", "Georgia", "Kazakhstan", "Kyrgyzstan", "Tajikistan", "Turkmenistan", "Uzbekistan", "Russia") Commented May 14, 2020 at 4:25
  • Note that the Soviet Union had/has many countries, not just Russia. So your pst_svt data, which contains only 1 record, is incorrect. Commented May 14, 2020 at 4:25

2 Answers 2

3

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")

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

elegant Answer !!
0

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")

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.