Skip to content
Prev 387393 / 398502 Next

Color in stripchart

Dear fellow R users,

I'd like to color individual points in a stripchart. This works well as
long as I have only a single value per y axis category:

df <- data.frame(year = seq(2011, 2018), value = seq(10,80, 10))
df$color <- 'black'
df[df$value<33,]$color <- 'blue'
df[df$value>66,]$color <- 'red'
stripchart(df$value ~ df$year, pch=18, col=df$color, method='stack')

When I use multiple values per y axis category, all points in a specific
y axis category have the same color:

set.seed(20210310)
years <- sample(x=seq(2011, 2018), size = 365*8, replace = TRUE)
values <- sample(x=seq(0,100), size = 365*8, replace = TRUE)
df <- data.frame(year = years, value = values)
df$color <- 'black'
df[df$value<33,]$color <- 'blue'
df[df$value>66,]$color <- 'red'
stripchart(df$value ~ df$year, pch=18, col=df$color, method='stack')

I'd expect to see all points with a value below 33 in blue color, all
points with a value above 66 in red color, and the remaining points in
black color.

I can use a black stripchart plot as basis and plot blue and red points
over the black ones, but I do not get the stacking right:

stripchart(df$value ~ df$year, pch=18, method='stack')
points(df[df$color=='blue',]$value, df[df$color=='blue',]$year-2010,
type='p', pch=18, col='blue')
points(df[df$color=='red',]$value, df[df$color=='red',]$year-2010,
type='p', pch=18, col='red')

Am I somehow misusing the stripchart function?


Best regards,

Robin