1

I am using simple logistic regression and use ggplot for a nice presentation. I adapted this code 1 to create the plot which worked nicely. Now, some of my bins are overlapping 2 and I would like to add borders around the bins for a clearer presentation.

How can you add borders to the bins when using geom_segment?

ggplot() + 
  geom_segment(data=H1, size=7, show.legend=FALSE,
               aes(x=SAL, xend=SAL, y=HG, yend=pct, color=factor(HG))) +
  geom_point() +
  stat_smooth(method="glm", se= FALSE, method.args = list(family = 
"binomial"),
              aes(x=SAL, y=HG),
              data = mydata) +
  labs(x="Salinity [‰]", y="Predicted probability")+
  scale_y_continuous(limits=c(-0.02,1.02)) +
  scale_x_continuous(limits=c(7,28)) +
  theme_bw(base_size=13.5)
Andi
  • 11
  • 1
  • 4
  • Can you use `geom_rect` instead? then you could use fill + color. I don't think you can add borders with geom_segment without doing some unorthodox stuff. – Reilstein Mar 02 '21 at 18:26

1 Answers1

2

You can define the colour of the outer line by passing the color (or, equivalently, colour) argument to geom_histogram().

library(tidyverse)

mtcars %>% 
  ggplot(aes(x = mpg)) + 
  geom_histogram(color = "black", fill = "red") # Border colour is set to black, and fill color is red

Created on 2021-02-28 by the reprex package (v0.3.0)

Ecoul
  • 35
  • 4
mhovd
  • 3,724
  • 2
  • 21
  • 47
  • 1
    As fill is not based on a variable this time, move the argument from the aesthetic mapping to the geom, `geom_histogram(fill = "red", color = "black")` (you can see this by changing the fill to "blue") – M.Viking Feb 28 '21 at 14:46
  • I agree - please feel free to edit the answer @M.Viking – mhovd Feb 28 '21 at 14:52
  • Thank you, I am aware of these commands. I am not working with geom_histogram because I am splitting the bars like in the example (see link). I am using geom_segment for this. I am adding my arguments in the original question so you get a better idea. – Andi Feb 28 '21 at 15:46
  • 1
    I found my mistake. geom_segment() represents lines, therefore I can't just put borders around them like in geom_histogram(). Creating the sort of reverse histoplot (see link in original question) I am aiming for requires an entirely different approach. Thank you anyway. – Andi Mar 01 '21 at 11:35