0
votes

Here's a small reproducible example from the iris dataset. We plot mean ± standard deviation of petal lengths of the different species, like so:

i2 <- iris %>%
  select(Species, Petal.Length) %>%
  pivot_longer(-Species, names_to='petal', values_to='measure') %>%
  group_by(Species) %>%
  summarise_at(vars(measure),
               list(
                 mean= ~ mean(.),
                 sd= ~ sd(.)
               ))

ggplot(i2, aes(x=Species, y=mean)) +
  geom_pointrange(aes(ymin=mean-sd, ymax=mean+sd, colour=Species), shape=21, size=1, fill='black')

Now, I would like to set the range line and point stroke in two different colours. The above is the closest I could get to that. Point shape 21 allows me to have a fill and stroke colour, but the colour parameter in aes() also sets the range line colour! So it does not quite get me there...

I have in mind something like:

enter image description here

1

1 Answers

1
votes

One option to achieve your desired result would be to create your point range using geom_linerange(...) + geom_point(...):

library(ggplot2)

ggplot(i2, aes(x=Species, y=mean)) +
  geom_linerange(aes(ymin=mean-sd, ymax=mean+sd, group=Species), size=1, color='black') +
  geom_point(aes(colour=Species), shape=21, size=4, fill='black', stroke = 2)