Andi
Andi

Reputation: 11

Adding borders to histograms in ggplot

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)

Upvotes: 1

Views: 6665

Answers (1)

mhovd
mhovd

Reputation: 4077

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)

Upvotes: 2

Related Questions