nima
nima

Reputation: 37

Create a plot based on two groups with the same variables

I have the following data:

structure(list(Month = c("2018-08", "2018-09", "2018-10", "2018-11", 
"2018-12", "2019-01", "2019-02", "2019-03", "2019-04", "2019-05", 
"2019-06", "2019-07", "2019-08", "2019-09", "2019-10", "2019-11", 
"2019-12", "2020-01", "2020-02", "2020-03", "2020-04", "2020-05"
), `Member 1` = c("Yes", "Yes", "Yes", "No", "No", "No", "Yes", 
"Yes", "Yes", "Yes", "No", "No", "No", "Yes", "Yes", "No", "Yes", 
"Yes", "No", "Yes", "No", "Yes"), `Rating 1` = c(1L, 1L, 4L, 
5L, 3L, 2L, 3L, 1L, 4L, 1L, 4L, 5L, 6L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L), `Member 2` = c("No", "Yes", "No", "Yes", "Yes", 
"No", "Yes", "Yes", "Yes", "No", "Yes", "No", "No", "Yes", "No", 
"Yes", "No", "Yes", "No", "Yes", "No", "Yes"), `Rating 2` = c(1L, 
4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 2L, 2L, 2L, 3L, 4L, 5L, 5L, 
1L, 2L, 5L, 3L, 2L)), class = "data.frame", row.names = c(NA, 
-22L))

My goal is to create a plot via ggplot in R where I have "Month" on the x-axis and "Rating" on the y-axis. My data contains two columns for Ratings (called Rating 1 and Rating 2). However, they have the same scale so I can call the y-axis just Rating. But then I would like to have four lines in total in the plot. You can look at it as having "Member 1" and "Rating 1" belonging together and "Member 2" and "Rating 2". So I would like to have four lines in the plot:

There should be a legend with two subgroups and each containing a title (Rating 1 and Rating 2 for the title respectively):

Rating 1:

Rating 2:

I am new to ggplot and would really appreciate any help.

What I have tried so far:

  1. I created two different data frames, one for Member 1 and its Rating and one for Member 2 and its Rating. Then I changed the column names so they are identical in both data frames.
Data1 <- subset(Data, select=c(Month, `Member 1`, `Rating 1`))
Data2 <- subset(Data, select=c(Month, `Member 1`, `Rating 1`))

colnames(Data1) <- c("Month", "Member", "Rating")
colnames(Data2) <- c("Month", "Member", "Rating")
  1. I used ggplot the following way:
ggplot() + 
  geom_line(data = Data1, aes(x = Month, y = Rating, group=`Member`, fill=`Member`, color=`Member`, linetype=`Member`), color = "red") +
  geom_line(data = Data2, aes(x = Month, y = Rating, group=`Member`, fill=`Member`, color=`Member`, linetype=`Member`), color = "blue") +
  xlab('Month') +
  ylab('Rating')

When I run the ggplot code, I get a plot with the desired x-axis and y-axis. However, I would like to have a legend on the right as explained above. Right now, I only have a legend for the blue colors. Additionally, it would be nice if I could use geom_point for one Member which makes it more visualizable.

Upvotes: 0

Views: 730

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

You haven't said what type of plot you wanted. In any case, it would be best to pivot your data into long format to create the plot:

library(tidyverse)

df %>%
  pivot_longer(-1, names_to = c(".value", "PersonNum"),
               names_pattern = "^(.*) (\\d)$") %>%
  mutate(Month = as.Date(paste0(Month, "-01"))) %>%
  ggplot(aes(Month, Rating, color = Member)) +
  geom_line() +
  geom_point() +
  facet_grid(PersonNum~.) +
  scale_color_manual(values = c('orange2', 'deepskyblue4')) +
  theme_bw(base_size = 16)

enter image description here


Update

If you don't want facets, you could put all the lines in one panel. I think this is a harder plot to read, and less attractive / informative than the plot with facets, but is here for completeness.

df %>%
  pivot_longer(-1, names_to = c(".value", "PersonNum"),
               names_pattern = "^(.*) (\\d)$") %>%
  mutate(Month = as.Date(paste0(Month, "-01")),
         Group = paste0('Rating: ', PersonNum, ', Member: ', Member)) %>%
  ggplot(aes(Month, Rating, color = Group)) +
  geom_line() +
  geom_point() +
  scale_color_brewer(palette = 'Set1') +
  theme_bw(base_size = 16)

enter image description here

Upvotes: 2

Related Questions