maribio18
maribio18

Reputation: 85

How to fill a plot below a regression line using ggplot

I have a scatter plot with a regression line and would like to fill the space below the regression line grey.

#my data
p2 = as.data.frame(cbind(Population = c(1432132, 2582830,1628701, 2278906,476179), Borough =c("BRONX", "BROOKLYN", "MANHATTEN", "QUEENS", "STATEN ISLAND"), count = c(341, 1427, 1092, 700, 39)))

p2$Population = as.numeric(as.character(p2$Population))
p2$count = as.numeric(as.character(p2$count))

fit = lm(p2$count ~ p2$Population , data = p2)

#the plot
ggplot(p2, aes(x=Population, y=count, color = Borough)) +
  geom_point() + 
  geom_text(label=p2$Borough, hjust = -0.1) + 
  geom_smooth(color = "black", method='lm', formula= y~x, se = FALSE) +
  theme_classic()

So far I have tried using geom_area(aes(fill=)) but instead of using the regression as data it connects my data points to the regression line. Is there anyway to specify to fill under the regression line instead of the data?

Upvotes: 0

Views: 397

Answers (1)

Electrino
Electrino

Reputation: 2890

This should do the trick:

ggplot(p2, aes(x=Population, y=count)) +
  geom_point() +
  geom_text(label=p2$Borough, hjust = -0.1) + 
  geom_smooth(color = "black", method='lm', formula= y~x, se = F) +
  geom_ribbon(aes(ymin = 0,ymax = predict(lm(count ~ Population))),
              alpha = 0.3,fill = 'green')+
  theme_classic()

Upvotes: 1

Related Questions