Create multiple-line graphs

This is the table:

Year Arcades (non-remote) Betting (non-remote) Bingo (non-remote) Casino (non-remote) Betting (remote) Bingo (remote) Casino (remote) Lotteries (remote and non-remote) The National Lottery (remote and non-remote)**
Apr 15 - Mar 16 418.06 3'318.19 693.11 999.38 1'743.91 122.41 2'364.44 379.77 3'416.80
Apr 16 - Mar 17 424.93 3'310.84 684.28 1'163.54 1'952.99 161.15 2'661.04 442.43 2'978.60
Apr 17 - Mar 18 424.81 3'268.26 680.01 1'180.72 2'251.61 163.93 2'931.13 502.29 3'007.80
Apr 18 - Mar 19 446.50 3'261.86 674.05 1'058.82 2'020.86 176.19 3'108.43 540.59 3'079.30
Apr 19 - Mar 20 430.93 2'415.45 576.31 1'017.59 2'325.81 177.06 3'232.15 612.73 3'399.21
Apr 20 - Mar 21 261.94 1'035.45 246.31 116.65 2'639.52 188.95 4'014.12 635.01 3'531.65
Apr 21 - Mar 22 412.15 2'145.14 441.72 691.78 2'285.81 183.94 3'853.98 666.50 3'483.24
Apr 22 - Mar 23* 572.16 2'475.62 591.82 810.42 2'288.17 173.64 4'037.79 682.47 3'490.12

in millions GBP

I want to create the line graphs about Betting(non-remote) and Betting(remote) in one graph, with different color.
How to do that?

Hi,

Welcome to the RStudio community!

We are happy to help you out on this forum when you are stuck on a particular piece of code, but we won't just write a solution to a question for you without you having a go at it first :slight_smile:

Try to read up on some ways to create these graphs yourself. I suggest starting at the R Graph Gallery where they have a lot of examples with code for you available.

Finally, if you want to share some dummy data / code in future, please provide us a minimal reproducible example where you provide a minimal (dummy) dataset and code that can recreate the issue. Once we have that, we can go from there. For help on creating a Reprex, see this guide:
https://community.rstudio.com/t/faq-how-to-do-a-minimal-reproducible-example-reprex-for-beginners/23061

Good luck!
PJ

1 Like

I rename the column names in this template: non_remote_casino, remote_betting, total_lotteries, total_national_lottery.

Then melt the data
mydata <- melt(mydata, id = "year")
colnames(mydata) <- c("year", "gaming_type", "value")

I use str_extract function to extract non_remote, remote, and total.
But has an error: object gaming_type not found.

I use colnames(mydata) to check column names, it has gaming_type.

library(tidyverse)

df<- tribble(
  ~Year, ~`Arcades (non-remote)`, ~`Betting (non-remote)`, ~`Bingo (non-remote)`, ~`Casino (non-remote)`, ~`Betting (remote)`, ~`Bingo (remote)`, ~`Casino (remote)`, ~`Lotteries (remote and non-remote)`, ~`The National Lottery (remote and non-remote)`,
  "Apr 15 - Mar 16", 418.06, 3318.19, 693.11, 999.38, 1743.91, 122.41, 2364.44, 379.77, 3416.80,
  "Apr 16 - Mar 17", 424.93, 3310.84, 684.28, 1163.54, 1952.99, 161.15, 2661.04, 442.43, 2978.60,
  "Apr 17 - Mar 18", 424.81, 3268.26, 680.01, 1180.72, 2251.61, 163.93, 2931.13, 502.29, 3007.80,
  "Apr 18 - Mar 19", 446.50, 3261.86, 674.05, 1058.82, 2020.86, 176.19, 3108.43, 540.59, 3079.30,
  "Apr 19 - Mar 20", 430.93, 2415.45, 576.31, 1017.59, 2325.81, 177.06, 3232.15, 612.73, 3399.21,
  "Apr 20 - Mar 21", 261.94, 1035.45, 246.31, 116.65, 2639.52, 188.95, 4014.12, 635.01, 3531.65,
  "Apr 21 - Mar 22", 412.15, 2145.14, 441.72, 691.78, 2285.81, 183.94, 3853.98, 666.50, 3483.24,
  "Apr 22 - Mar 23", 572.16, 2475.62, 591.82, 810.42, 2288.17, 173.64, 4037.79, 682.47, 3490.12,
) %>% 
  mutate(Year = fct_inorder(Year))  

df %>% 
  pivot_longer(-Year, names_to = "Type", values_to = "Value") %>% 
  mutate(status = ifelse(grepl("non-remote", Type), "non-remote", "remote")) %>% 
  reframe(.by = c(Year, status), Value = sum(Value)) %>%
  ggplot(aes(x = Year, y = Value, color = status, group = status)) +
  geom_line()
2 Likes

I found the error in the pattern, so I changed it.

mydata$type <- sub("^(.*)_.*$","\\1", mydata$gaming_type)
mydata$gaming_type <- sub("^.*_","",mydata$gaming_type)

Then I got the column 'type' that includes non_remote, remote, and total. Gaming_type includes gaming activities, like bingo, betting.

I have a new question about adjusting the 'year column.
I run the following code and create line graph about comparing Betting's remote and non-remote GGY.

mydata$year = gsub("[*]","",mydata$year)

The line graph's x-axis, not very clear. I want to know can I use other method to adjust the 'year' column data?

You need to decide what you might want there.

Would 2021/22, 2022/23 etc be clear enough?

You could rotate the text. But a lot of vis people feel rotated text makes the graph hard to read aas you have to turn your head.

You could split this lines:

Apr 21
to
Mar 22

You could reduce the font size.

There are even options to stagger the labels

Apr 21 - Mar 22      |     Apr 23 - Mar 24
              Apr 22 - Mar 23

There is a FAQ all about this which should be your point of reference:
FAQ: Axes • ggplot2.