Search code examples
rggplot2visualization

How to create a multi-line graph in R transposed (i.e., the other way) using ggplot2?


In most multi-line graphs, it graphs multiple entities of one column, however in my case I want to graph multiple columns of a single entity.

In more simple terms a multi-line graph might show life expectancy over time across different countries. What I want is a graph that shows the number of EMS calls in Pittsburgh overtime across different call reasons.

Most multi-line graphs look like this

However I want my graph to look like this (made in Excel):

this

The y-axis is number of calls and of course I graphed horizontal lines for convenience and simplicity.

Here are the current packages I'm using. I am open to using other packages though.

library(tidyverse)
library(reshape2)

Here is my condensed data as a tibble (the full dataset has 30 different call reasons)

City_name <- (rep("PITTSBURGH", 6))
year_quarter <- c("2015Q1", "2015Q2", "2015Q3", "2015Q4", "2016Q1", "2016Q2")
ABDOMINAL_PAIN <- rep(10, 6)
ALLERGIES <- rep(12, 6)
ASSAULT <- rep(14, 6)
BACK_PAIN <- rep(16, 6)


df_0 <- tibble(City_name, year_quarter,ABDOMINAL_PAIN, ALLERGIES, ASSAULT, BACK_PAIN)

print(df_0)

# A tibble: 6 × 6
# City_name  year_quarter ABDOMINAL_PAIN ALLERGIES ASSAULT BACK_PAIN
# <chr>      <chr>                 <dbl>     <dbl>   <dbl>     <dbl>
# 1 PITTSBURGH 2015Q1                   10        12      14        16
# 2 PITTSBURGH 2015Q2                   10        12      14        16
# 3 PITTSBURGH 2015Q3                   10        12      14        16
# 4 PITTSBURGH 2015Q4                   10        12      14        16
# 5 PITTSBURGH 2016Q1                   10        12      14        16
# 6 PITTSBURGH 2016Q2                   10        12      14        16
# > 

I also have a vector of the column names themselves

vec_col.names <- c("ABDOMINAL_PAIN", "ALLERGIES", "ASSAULT", "BACK_PAIN")

I am having trouble as most sources like this one that show the first way. I have not been able to find other resources and using the code provided by the source above.

north_big <- gapminder %>%
  filter(continent == "Americas", country %in% c("United States", "Canada", "Mexico"))

ggplot(north_big, aes(x = year, y = lifeExp, group = country)) +
  geom_line(aes(color = country), size = 2)

Solution

  • THe easiest way to do this is this:

    library(tidyverse)
    
    City_name <- rep("PITTSBURGH", 6)
    year_quarter <- c("2015Q1", "2015Q2", "2015Q3", "2015Q4", "2016Q1", "2016Q2")
    ABDOMINAL_PAIN <- rep(10, 6)
    ALLERGIES <- rep(12, 6)
    ASSAULT <- rep(14, 6)
    BACK_PAIN <- rep(16, 6)
    
    df_0 <- tibble(City_name, year_quarter, ABDOMINAL_PAIN, ALLERGIES, ASSAULT, BACK_PAIN)
    
    df_long <- df_0 %>%
      pivot_longer(cols = ABDOMINAL_PAIN:BACK_PAIN, names_to = "call_reason", values_to = "num_calls")
    
    ggplot(df_long, aes(x = year_quarter, y = num_calls, color = call_reason, group = call_reason)) +
      geom_line(size = 1.2) +
      labs(title = "Number of EMS Calls in Pittsburgh",
           x = "Quarter",
           y = "Number of Calls",
           color = "Call Reason") +
      theme_minimal()
    
    

    which returns

    enter image description here