Smooth looking geom_ribbon without "steps"

I am trying to plot data that I was given. This data comes from a model and includes the mean of a variable, 2.5% and 97.5% CI of the variable, and I want to plot it with a line for the median (geom_line) and a ribbon for the CI (geom_ribbon).

I have made plots like this with other data and I get a nice smooth graphs for both the line and the ribbon, but with the new data set I get "steps" for both the line and the ribbon, similar to the example I have produce here.

Is there any way to make the ribbons smooth? Is this because the number of data points is not big enough to make it smooth?

Any ideas will be much appreciated! I run out of examples already posted on-line and none is working for me... THANKS in advance!

require(tidyverse)
#> Loading required package: tidyverse
library(airqualityES)

airqualityBEA <- airquality%>%
  arrange(desc(Temp))%>%
  mutate(list = 1:153)



# This plot makes step in both the geom_line and geom_ribbon.

ggplot() +
  
  geom_ribbon(data = airqualityBEA,
              aes(x= list,ymin = Temp - 1, ymax = Temp+1),
              fill = "#00798c", alpha = 0.5) +

  geom_line(data =airqualityBEA,
            aes(x= list, y=Temp), 
            alpha=1,  size=0.5, color = "#00798c") 


# Using Geom_smooth with loess instead of geom_line it improves the plot... (I cannot post a second image here :))

ggplot()+
  
  geom_ribbon(data = airqualityBEA,
              aes(x= list,ymin = Temp - 1, ymax = Temp+1),
              fill = "#00798c", alpha = 0.5) +
  
  geom_smooth(method = "loess", se = FALSE,
              data =airqualityBEA,
            aes(x= list, y=Temp), 
            alpha=1,  size=0.5, color = "#00798c")
#> `geom_smooth()` using formula 'y ~ x'

Created on 2020-03-26 by the reprex package (v0.3.0)

Hi @Beavet82: Your data has multiple repeated values, so you can't avoid the steps unless you modify the data:

library(tidyverse)
library(airqualityES)

airquality %>%
  count(Temp) %>% 
  arrange(desc(n)) %>% 
  head()
#> # A tibble: 6 x 2
#>    Temp     n
#>   <int> <int>
#> 1    81    11
#> 2    76     9
#> 3    82     9
#> 4    77     7
#> 5    86     7
#> 6    78     6

Created on 2020-03-26 by the reprex package (v0.3.0)

Data that had decimal parts would likely be smoother.

1 Like

Thanks @dromano, I had a look to my data, and although my Y variable has decimals and no repeated values, each of my X variable is repeated 5 times...
I don't know how to share my data (only if would be of any help) without sharing quite a long reprex.

I will check with my collaborator as he has a smooth graph with this data using another software other than R (I can't remember which one).

Understanding your data would helpful, and so would understanding your goals for the data. For example, if you have repeated x values, how would you like them represented? (The code you posted above would create zig-zags in the plot in that case.)

To share your data alone, you could do this:

```
<--- paste output of dput(your_table %>% head(50)) here, including ```s
```

Sorry it took me a bit to write back, but this is the data and the code of the plot I would like to do:

my_dput <- structure(list(DPF = c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5), median_proportion_positive = c(0.224, 
0.22, 0.216, 0.212, 0.208, 0.205, 0.201, 0.198, 0.195, 0.191, 
0.188, 0.185, 0.182, 0.179, 0.176, 0.173, 0.17, 0.167, 0.164, 
0.162, 0.159, 0.156, 0.154, 0.151, 0.148, 0.146, 0.143, 0.141, 
0.139, 0.136, 0.134, 0.132, 0.13, 0.127, 0.125, 0.123, 0.121, 
0.119, 0.117, 0.115, 0.113, 0.111, 0.109, 0.108, 0.106, 0.104, 
0.102, 0.101, 0.0988, 0.0971), Per2.5_proportion_positive = c(0.189, 
0.187, 0.184, 0.182, 0.179, 0.176, 0.173, 0.17, 0.167, 0.164, 
0.16, 0.157, 0.154, 0.15, 0.147, 0.143, 0.14, 0.137, 0.133, 0.13, 
0.127, 0.123, 0.12, 0.117, 0.114, 0.111, 0.108, 0.105, 0.102, 
0.0995, 0.0968, 0.0942, 0.0917, 0.0893, 0.0868, 0.0844, 0.082, 
0.0798, 0.0776, 0.0755, 0.0735, 0.0715, 0.0695, 0.0676, 0.0658, 
0.064, 0.0623, 0.0606, 0.0589, 0.0572), Per97.5_proportion_positive = c(0.261, 
0.256, 0.251, 0.246, 0.241, 0.237, 0.233, 0.229, 0.225, 0.222, 
0.219, 0.215, 0.213, 0.21, 0.207, 0.204, 0.202, 0.2, 0.197, 0.195, 
0.193, 0.191, 0.189, 0.187, 0.186, 0.184, 0.182, 0.18, 0.178, 
0.177, 0.175, 0.174, 0.172, 0.171, 0.169, 0.168, 0.166, 0.165, 
0.163, 0.162, 0.161, 0.159, 0.158, 0.157, 0.156, 0.154, 0.153, 
0.152, 0.15, 0.149)), row.names = c(NA, 50L), class = "data.frame")

If you call that data:

my_dput

This is the plot I want to make, but without the step (although as you suggested previously I have repeated values for DPF variable). Maybe a way around it is to give each of those decimal points to the integers...?

ggplot()+
  
  geom_ribbon(data =my_dput, 
              aes(x= DPF, 
                  ymin=Per2.5_proportion_positive,  
                  ymax=Per97.5_proportion_positive),
              fill = "#00798c", alpha = 0.5)  +
  
  geom_line(data = my_dput, 
             aes(x = DPF, y = median_proportion_positive), 
             size = 0.5 ) 

Could you say why you have multiple rows with the same value of DPF? And maybe what information you're trying to convey with your graph? That might help folks understand the underlying issue better.

P.S. Thanks for posting the data -- one tweak to make it copy-friendly would be to add the assignment my_dput <- immediately before it, so folks can capture it right away.

All sorted now! I checked with the collaborator that gave me the data as I didn't understand either why there were multiple rows with the same value of DPF.

After not understanding each other he saw those repeated DPF, and said those datapoints should be different numbers with decimal parts.

I had converted his text file to csv and added the variable names, but at the same time changed the format of the numbers loosing the decimal part.

Sorry I had started a topic looking for help, but I have now learned a few things: put, reprex and to check the original file if the graph doesn't make sense!

Many thanks for your help @dromano and hope you keep well.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.