Skip to content

Geom ribbon

2 messages · Thomas Subia, j@de@shod@@ m@iii@g oii googiem@ii@com

#
Colleagues,
I am plotting tolerance ratios by location and dateMy reproducible code is:
library(ggplot2)
library(cowplot)

# tolerance ratio 
day1 <- runif(3)
day2 <- runif(3)
day3 <-runif(3)
location <- c("A","B","C")

df <- data.frame(location,day1,day2,day3)

# plot of tolerance ratio by location and day
myplot <-ggplot(df,aes(location,day1))+geom_point()+
? geom_point(aes(location,day2))+
? ylab("Tolerance ratio")+
? xlab("Location")+
? theme_cowplot()+
? ggtitle("Tolerance ratio by location and day")
I need to shade the areas between the following y data

Tolerance
Ratio > 0.6??? ????????????????????area shaded = red??? ????Unacceptable
0.3 <= Ratio <= 0.6?????? ????light red???????????????????? Inadequate
0.25 <= Ratio < 0.3??????? ????yellow?????????????????????? Marginal0.2 <= Ratio < 0.25??????? ????light green???????????????? Good
Ratio < 0.2?????????????????????? ????green??????????????????????? Great
I believe that using geom_ribbon may work for this.
Any suggestions for this would be appreciated.
All the best,
Thomas Subia
#
I'm not sure if geom_ribbon works with categorical data. It didn't
work for me, so I have coded location as a numeric, which works. You
can then manuall re-label the tick marks, as per the code below.
Others may be able to add to the code to add a legend, or propose a
different solution altogether, which may involve having to restructure
your data frame (I'm not an expert in R/ ggplot).

Below is some code. It's not elegant, but it seems to do at least part
of the job...

# tolerance ratio
day1 <- runif(3)
day2 <- runif(3)
day3 <-runif(3)
location <- c(1,2,3)

df <- data.frame(location,day1,day2,day3)
max <- 1.0  # find the maximum y-value in your data. Here, I've
assigned 1 for ease
ggplot()+
  geom_ribbon(aes(ymin = 0, ymax = 0.199999999, x = location), fill = "green") +
  geom_ribbon(aes(ymin = 0.2, ymax = 0.249999999, x = location), fill
= "light green") +
  geom_ribbon(aes(ymin = 0.25, ymax = 0.299999999, x = location), fill
= "yellow") +
  geom_ribbon(aes(ymin = 0.3, ymax = 0.6, x = location), fill =
"orange") +   # "light red " is not recognised as a colour
  geom_ribbon(aes(ymin = 0.6000001, ymax = max, x = location), fill = "red") +
  geom_point(data = df, aes(location, day1))+
  geom_point(aes(location,day2))+
   scale_x_discrete(limits=c("A","B","C"))+
  ylab("Tolerance ratio")+
  xlab("Location")+
  ggtitle("Tolerance ratio by location and day")


Hope this provides at least a start!

Jade

On Sat, 9 Jul 2022 at 16:13, Thomas Subia via R-help
<r-help at r-project.org> wrote: