Home > front end >  R: Does data need restructuring to create an area chart ggplot2
R: Does data need restructuring to create an area chart ggplot2

Time:11-13

I am fairly new to using ggplot for data visualisation in R and I want to understand whether the following dataframe df needs restructuring so that there is only one metric Spend to create an area chart. For this area chart I want the x-axis to be Days, the y-axis to be Spend and for it to be split by person A and person B.

df<-data.frame(Days=c(1,2,3,4,5,6,7),
PersonASpend=c(100,0,90,20,5,8,10),
PersonBSpend=c(30,20,120,20,55,20,0))

  Days PersonASpend PersonBSpend
1    1          100           30
2    2            0           20
3    3           90          120
4    4           20           20
5    5            5           55
6    6            8           20
7    7           10            0

i.e. does it need to be structured as follows or is there another way to do this?

  Days       Person         Spend
1    1            A          100
2    2            A            0
3    3            A           90
4    4            A           20
5    5            A            5
6    6            A            7
7    7            A           10
8    1            B           30
9    2            B           20
10   3            B          120
11   4            B           20
12   5            B           55
13   6            B           20
14   7            B            0

CodePudding user response:

Yes, you can use pivot_longer from tidyr, which is part of the tidyverse:

library(tidyverse)

df %>%
  pivot_longer(-1, names_to = "Person", values_to = "Spend") %>%
  ggplot(aes(Days, Spend, fill = Person))  
  geom_area(alpha = 0.4, position = "identity")

The alternative is to plot each column as a separate layer:

df %>%
  ggplot(aes(Days))  
  geom_area(aes(y = PersonASpend, fill = "A"), alpha = 0.4)  
  geom_area(aes(y = PersonBSpend, fill = "B"), alpha = 0.4)  
  labs(y = "Spend", fill = "Person")

Created on 2022-11-13 with reprex v2.0.2

CodePudding user response:

df %>% 
  rename(A = PersonASpend) %>% 
  rename(B = PersonBSpend) %>% 
  pivot_longer(c(A, B)
               ,names_to = "Person" 
               ,values_to = "Spend") %>% 
  arrange(Person) 

# A tibble: 14 × 3
    Days Person Spend
   <dbl> <chr>  <dbl>
 1     1 A        100
 2     2 A          0
 3     3 A         90
 4     4 A         20
 5     5 A          5
 6     6 A          8
 7     7 A         10
 8     1 B         30
 9     2 B         20
10     3 B        120
11     4 B         20
12     5 B         55
13     6 B         20
14     7 B          0
  • Related