Changing colour by name in a scatterplot

Viewed 33

I am trying to display a scatterplot of the Yield per batch of a product: (make-up of that dataset)

Batch Yield
PROD_191105001 88
PROD_191106002 87
PROD_191107003 86
PROD_200203001 98
PROD_200204002 99
PROD_200205003 96

Now, I have a nice scatterplot for that, with average and quality chart rules (95th percentile 99th percentile) displayed. I want to compare last years Batches to this years batches. The dots of last years batches should be greyed-out: Scatterplot Yield

my spaghetti for that image:

ggplot(data=data) +
+ geom_point(aes(x=Batch, y=`normalized yield`))+
+ geom_hline(aes(yintercept = 115), colour = "green", size=1)+ %%set limit
+ geom_hline(aes(yintercept = 140), colour = "green", size=1)+ %%set limit
+ geom_hline(aes(yintercept=(mean(`normalized yield`)-2*sd(`normalized yield`))), colour = "blue", size=1)+
+ geom_hline(aes(yintercept=(mean(`normalized yield`)+2*sd(`normalized yield`))), colour = "blue", size=1)+
+ geom_hline(aes(yintercept=(mean(`normalized yield`)-3*sd(`normalized yield`))), colour = "red", size=1)+
+ geom_hline(aes(yintercept=mean(`normalized yield`)), linetype = "dashed")+
+ theme(axis.ticks = element_blank(), axis.text.x = element_blank())+
+ labs(title="Product Yield ", y="normalized yield in g", x= "Batch")

How do I tell R to split this dataset at a specific point, so I can change the colour makeup of that part?

1 Answers

The super quick version: use the color aesthetic option with a boolean 'less than' applied to Batch (note the addition of colour to geom_point():

ggplot(data=data) +
  geom_point(aes(x=Batch, y=`normalized yield`, colour = Batch < 'PROD_20200101'))
  theme(axis.ticks = element_blank(), axis.text.x = element_blank())+
  labs(title="Product Yield ", y="normalized yield in g", x= "Batch")

A more flexible route would be to extract the date from the Batch column into its own column so that you can reference it directly (e.g. for coloring a plot)

library(ggplot2)  # For plotting
library(tibble)  # To make the sample data
library(tidyverse)  # For piping and data transformation
library(lubridate)  # To handle dates

# Sample Data -- I aligned the sample data to 2001 / 2002
data <- tribble(
  ~batch, ~yield,
  'PROD_20011001', 88,
  'PROD_20011102', 87,
  'PROD_20011203', 86,
  'PROD_20020101', 98,
  'PROD_20020202', 99,
  'PROD_20020303', 96,
)

# Create new columns
# Extract date_string, then date, then year
# You could do this all in one step if you want,
# I used three steps to help illustrate what is happening.
data <- data %>%
  mutate(
    batch_date_str = substring(batch, 6),  # Extract the characters that represent the date from each batch
    batch_date = lubridate::ymd(batch_date_str),  # Convert those characters into a 'date'
    batch_year = lubridate::year(batch_date)  # Extract just the year from each batch date
  )

# If you want to color each year distinctly, convert `batch_year` to a factor
# so that ggplot does not use a continuous scale.
ggplot(data, aes(x=batch_date, y=yield, color=as_factor(batch_year))) +
  geom_point()
Related