1

I am trying to use geom_ribbon to fill an area under a geom_smooth line in ggplot and there are gaps under the curve where the color is not shaded. My data consists of six discrete values for proportion values on the y axis. Is there a way to use ymax in geom_ribbon differently to have the color meet the curved line better?

enter image description here

Here is the reproducible code for the data:

q1 <- structure(list(Session = 1:6, Counts = c(244L, 358L, 322L, 210L, 
156L, 100L), Density_1000 = c(NA, NA, NA, NA, NA, NA),             Proportion_Activity = c(0.175539568, 
0.257553957, 0.231654676, 0.151079137, 0.112230216, 0.071942446
), Lifestage = structure(c(3L, 3L, 3L, 3L, 3L, 3L), .Label =    c("Adult", 
"Nymph", "Larvae"), class = "factor")), .Names = c("Session", 
"Counts", "Density_1000", "Proportion_Activity", "Lifestage"),    row.names = 13:18, class = "data.frame")

Here is the ggplot code:

ggplot(q1,aes(x=Session, y=Proportion_Activity, col =    Lifestage,fill=Lifestage)) 
+ geom_smooth(method = 'loess') 
+ geom_ribbon(data = q1,aes(x = Session, ymin=0,    ymax=Proportion_Activity, alpha=0.5))
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Why are you using `geom_smooth` with just 6 discrete points? Seems like it would just be a lot easier to use `geom_line` (and it wouldn't misrepresent your data). – MrFlick Sep 27 '18 at 16:21
  • Thank you for your suggestions, I am new to stack overflow. I added the reproducible code. I was hoping to use geom_smooth simply for aesthetic purposes and for norms in how this type of data is usually displayed. Do you have any suggestions? – user10425293 Sep 27 '18 at 17:25

2 Answers2

4

You can just use the area geom with the stat_smooth layer. For example

ggplot(q1,aes(x=Session, y=Proportion_Activity, col =    Lifestage,fill=Lifestage))  + 
  geom_smooth(method = 'loess') +
  stat_smooth(se=FALSE, geom="area", method = 'loess', alpha=.5)

enter image description here

Thou I really think smoothing should be used when you have a lot of data and want to show a general pattern. Using it like this to "smooth" the line to make it look pretty doesn't make it clear that you have modeled the results and shows data in places where you did not observe it.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
0

You can do something like this.

p1 <- ggplot(q1,aes(x=Session, y=Proportion_Activity)) +
  geom_smooth(method = 'loess', aes(color = Lifestage))

g1 <- ggplot_build(p1)

p2 <- data.frame(Session = g1$data[[1]]$x,
                 Proportion_Activity = g1$data[[1]]$y,
                 Lifestage = structure(g1$data[[1]]$group, .Label = c("Larvae", "Nymph", "Adult"), class = "factor"))


p1 + geom_ribbon(data = p2, aes(x = Session, ymin = 0, ymax = Proportion_Activity, fill = Lifestage), alpha = 0.5)

You can also use geom_line instead of geom_smooth.

geom_line(stat = "smooth", method = 'loess', alpha = 0.5, aes(color = Lifestage))

And remove the color from geom_smooth/geom_line if you want. Just add guides(color = FALSE) or fill if you want to remove that.

Anonymous coward
  • 2,061
  • 1
  • 16
  • 29
  • Good to hear. [Please upvote answers which were useful, and select the answer that best solves your problem](https://stackoverflow.com/help/someone-answers). – Anonymous coward Dec 07 '18 at 23:24