Search code examples
rggplot2line-plot

How to create a line plot by using specific rows from a .xls file with R?


I want to create a line plot with two specific rows and first row as the x-axis from a .xls file.

Until now, I tried ggplot which I could not find what should I pass to places which marked with the question marks.

dataset <- read_excel("inflation.xls")
p1 <- ggplot() + geom_line(aes(y = ?, x = ?), data = dataset) 

Here is the expected result and sample of my data.

Expected Result

Sample Data


Solution

  • You should probably try to convert your data into a tidy format (each row is an observation and columns are variables) before plotting.

    In this case,

    library(dplyr)
    target <- c("Turkey", "Germany")
    dataset <- gather(dataset, key = "Year", value = "Inflation", -1) %>% # -1 here to gather all columns except the first 
               filter(`Country Name` %in% target) #filter the relevant countries
    

    Your dataset should then have a Year column with each year, and an Inflation column with the inflation values.

      Country Name  Year Inflation
    1  Turkey      1960         1
    2 Germany      1960         2
    3  Turkey      1961         2
    4 Germany      1961         3
    5  Turkey      1962         3
    6 Germany      1962         4
    

    From here it should be clear that Year is the x value and Inflation is the Y value, you want to group by Country so that each country gets its own line.

    ggplot() +
      geom_line(data = dataset, aes(x = Year, y = Inflation, color = Country, group = Country))