0
$\begingroup$

I am trying to show the correlations between life expectancy age and state pension age for men and women, per country.

I have the following data here (working example):

library(xml2)
library(rvest)
library(stringr)

urlLifeExpectancy <- "https://en.wikipedia.org/wiki/List_of_countries_by_life_expectancy"

extractedLifeData = urlLifeExpectancy %>%
 read_html() %>%
 html_node(xpath = '//*[@id="mw-content-text"]/div/table[1]') %>%
 html_table(fill = TRUE)

urlPensionAge <- "https://en.wikipedia.org/wiki/Retirement_age#Retirement_age_by_country"

extractedPensionData = urlPensionAge %>%
read_html() %>%
html_node(xpath = '//*[@id="mw-content-text"]/div/table[3]') %>%
html_table(fill = TRUE)

combinedData <- merge(extractedLifeData[c(1, 5, 7)], extractedPensionData[1:3], by.y = "Country", by.x = "Country and regions")

Does it make sense to use a scatter graph in this example?

$\endgroup$
0
$\begingroup$

To represent this data in a visual way a scatter plot may be useful but first, you need to work a little bit with the data. One problem is that the state pension age for women and men in the different countries is not always a numeric value, instead, you have 3 other values: range values (for example: 60-65), years with months (for example: 60y10months) and one country with an exception for university deans. I take the months to decimal years, the range values I take the average, and the exception I dismiss it. Then, doing that you can plot a scatter-plot of state pension age vs life expectancy, and plot with different colors the gender. If you want to add the countries, the graphic is not good. But it can be plot discriminating by continent. In this case, I would rather plot continents with color and gender with shape. This code will make these two graphics, with all the pre-processing.

    library(xml2)
    library(rvest)
    library(stringr)
    library(countrycode)

    urlLifeExpectancy <- "https://en.wikipedia.org/wiki/List_of_countries_by_life_expectancy"

    extractedLifeData = urlLifeExpectancy %>%
      read_html() %>%
      html_node(xpath = '//*[@id="mw-content-text"]/div/table[1]') %>%
      html_table(fill = TRUE)

    urlPensionAge <- "https://en.wikipedia.org/wiki/Retirement_age#Retirement_age_by_country"

    extractedPensionData = urlPensionAge %>%
      read_html() %>%
      html_node(xpath = '//*[@id="mw-content-text"]/div/table[3]') %>%
      html_table(fill = TRUE)

    combinedData <- merge(extractedLifeData[c(1, 5, 7)], extractedPensionData[1:3], by.y = "Country", by.x = "Country and regions")



    names_var <- colnames(combinedData)
    colnames(combinedData)[1:3] <- c('country','Female_Life_Expect','Male_Life_Expect')

    index <- nchar(combinedData$Men) == 5

    combinedData$Men_New <- as.numeric(combinedData$Men)
    combinedData$Men_New[index] <- (as.numeric(substr(combinedData$Men[index],1,2)) + as.numeric(substr(combinedData$Men[index],4,5)))/2

    index <- substr(combinedData$Men,3,3) == 'y' 
    combinedData$Men_New[index] <- as.numeric(substr(combinedData$Men[index],1,2)) + as.numeric(substr(combinedData$Men[index],4,4))/12

    index <- is.na(combinedData$Men_New)
    combinedData$Men_New[index] <- as.numeric(substr(combinedData$Men[index],1,2))

    index <- nchar(combinedData$Women) == 5
    combinedData$Women_New <- as.numeric(combinedData$Women)
    combinedData$Women_New[index] <- (as.numeric(substr(combinedData$Women[index],1,2)) + as.numeric(substr(combinedData$Women[index],4,5)))/2

    index <- substr(combinedData$Women,3,3) == 'y' 
    combinedData$Women_New[index] <- as.numeric(substr(combinedData$Women[index],1,2)) + as.numeric(substr(combinedData$Women[index],4,4))/12

    index <- is.na(combinedData$Women_New)
    combinedData$Women_New[index] <- as.numeric(substr(combinedData$Women[index],1,2))

    colnames(combinedData)[1:3] <- c('country','Female_Life_Expect','Male_Life_Expect')
    aux_data <- combinedData[,c(3,6)]
    data <- combinedData[,c(2,7)]
    colnames(data) <- c('v1','v2')
    colnames(aux_data) <- c('v1','v2')
    data <- rbind(data,aux_data)
    data$Gender <- c(rep('Female',nrow(combinedData)),rep('Male',nrow(combinedData)))
    colnames(data)[1:2] <- c('Life_Exp','Pension_Age')
    ggplot(data=data,aes(x=Life_Exp,y=Pension_Age,col=Gender)) + geom_point()


    combinedData$Continent <- countrycode(sourcevar = combinedData$country,
                                           origin = "country.name",
                                           destination = "continent")

    data$Continent <- rep(combinedData$Continent,2)
    #Graphic with continents
    ggplot(data=data,aes(x=Life_Exp,y=Pension_Age,col= Continent, shape=Gender)) + geom_point()
| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.