Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add a curve (smoothed line) to geom-bar in r?

for this data:

 big = runif(60);mangum = runif(61:120)
 df = data.frame(big, mangum)
 df_x <- eval(substitute(big), df)
 df_y <- eval(substitute(mangum), df)
 ccf.object <- ccf(df_x, df_y, plot = FALSE)
 output_table <- cbind(lag = ccf.object$lag, 
                    x.corr = ccf.object$acf) %>%
  as_tibble() %>%      mutate(cat = ifelse(x.corr > 0, "green", "red"))
 output_table %>%    ggplot(aes(x = lag, y = x.corr)) +
geom_bar(stat = "identity", aes(fill = cat))

[![enter image description here][1]][1]

How can add a smmothing curve above or below the bars like in the picture( bold line blue).

  • geom_smooth() does not do what I need.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use stat_xspline from ggalt:

output_table %>%    
  ggplot(aes(x = lag, y = x.corr)) +
  geom_col(aes(fill = paste0(cat, 3), color = after_scale(alpha(fill, 1))), 
           alpha = 0.5) +
  geom_hline(yintercept = 0) +
  ggalt::stat_xspline(spline_shape = -0.5, color = 'blue', linewidth = 1) +
  scale_fill_identity() +
  theme_minimal()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading