Home > database >  geom_point() geom_boxplot() for different colour/fill variable?
geom_point() geom_boxplot() for different colour/fill variable?

Time:08-29

I am running the following code to generate a graph in which each point is coloured differently for each company per financial year according to a specific sequence. In the background, there is also a boxplot for each financial year.

library(dplyr); library(forcats)
df1 %>% arrange(financial_year != "2018-19", -production) %>%
  mutate(company_code = fct_inorder(as.factor(company_code))) %>%
  arrange(company_code, financial_year) %>%
  ggplot(., mapping = aes(x=financial_year, y = production)) 
       geom_point(aes(colour=company_code),position = position_jitter(height=0, width=0.2),
                  size = 1.1, alpha = 0.6)   geom_boxplot(aes(colour=financial_year))

This works as I envisioned except it disregards the first 3 functions (arrange, mutate, arrange) and does not colour company_code by the correct sequence. It should give an impression of gradient, but right now it is all random. Any ideas?

This works fine (sequence is correct) if I remove geom_boxplot(aes(colour=financial_year)).

example data below

company_code <- c(1,1,1,1,2,2,2,2,3,3,3,3)
financial_year <- c("2018-19","2019-20","2020-21","2018-21","2018-19","2019-20","2020-21","2018-21","2018-19","2019-20","2020-21","2018-21")
production <- c(2000,2500,3000,7500,1000,1500,1000,3500,5000,5500,4000,14500)

df <- data.frame(company_code,financial_year,production)
df$company_code <- as.factor(df$company_code)
df$financial_year <- as.factor(df$financial_year)

CodePudding user response:

You should swap the order of geom_point and geom_boxplot and add alpha to geom_boxplot like this:

company_code <- c(1,1,1,1,2,2,2,2,3,3,3,3)
financial_year <- c("2018-19","2019-20","2020-21","2018-21","2018-19","2019-20","2020-21","2018-21","2018-19","2019-20","2020-21","2018-21")
production <- c(2000,2500,3000,7500,1000,1500,1000,3500,5000,5500,4000,14500)

df <- data.frame(company_code,financial_year,production)
df$company_code <- as.factor(df$company_code)
df$financial_year <- as.factor(df$financial_year)

library(ggplot2); library(dplyr); library(forcats)
df %>% 
  arrange(financial_year != "2018-19", -production) %>%
  mutate(company_code = fct_inorder(as.factor(company_code))) %>%
  arrange(company_code, financial_year) %>%
  ggplot(., aes(x=financial_year, y = production))  
  geom_boxplot(colour = "black", alpha = 0)  
  geom_point(aes(colour=company_code),position = position_jitter(height=0, width=0.2),
             size = 1.1, alpha = 0.6) 

Created on 2022-08-29 with reprex v2.0.2

  • Related