Search code examples
rggplot2aeslinegraph

ggplot how to "read" data vertically instead of horizontally in R


The data frame currently looks like this:

EDIT: structure

library(data.table)
library(dplyr)
library(tibble)

But I get the following error: "Each group consists of only one observation".

If so, how can I get a line graph that plots each columns value by month?

Also, I am not sure how to select more than one region in the ggplot aes() bit. I tried using c() to no avail. Any help and newbie-friendly advice would be greatly appreciated!


Solution

  • The issue is that your x-axis variable is a character or categorical variable. In that case ggplot by default uses this variable for the grouping of the data, i.e. there is only one observation per group. In that case you have to tell ggplot about the desired grouping which could be done by group=1 which means that ggplot2 should treat all observations as belonging to one group which for simplicity we call 1.

    To get a line plot for all your regions it's best to reshape your data to long format using e.g. tidy::pivot_longer which gives us two new cols, one with the names of the categories or regions and one with the corresponding values. After reshaping you could map the values on y and group by regions using group=name.

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    df <- structure(list(Date = c("01-2019", "02-2019", "03-2019"), `North East` = c(
      5.05625777763551,
      5.58119346747183, 5.41295614949722
    ), London = c(
      4.2102766429572,
      4.45850956493638, 4.36960549219723
    ), `West Midlands` = c(
      5.0708122696351,
      5.20425572086481, 5.07463979478007
    )), row.names = c(NA, 3L), class = "data.frame")
    
    df_long <- df %>%
      pivot_longer(-Date)
    
    ggplot(df_long, aes(Date, value, color = name, group = name)) +
      geom_line() +
      labs(x = "Date", y = "Region")