rggplot2line-plotggalt

How to plot smoothed line (ggalt::xspline) plot with x-axis as factor


I have the following data frame:

  lp_dat <- structure(list(kmeans_cluster = c("1", "2", "3", "4", "1", "2", 
"3", "4", "1", "2", "3", "4"), tc = structure(c(2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L), .Label = c("NT", "IBD+PBS", 
"IBD+Serpin"), class = "factor"), n = c(924, 1389, 0, 652, 924, 
0, 0, 0, 110, 1389, 11851, 0)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

What I want to do is to smooth the plot. Below is the code I use:

lp <-   ggplot(lp_dat, aes(x = tc, y = n, group = 1)) +
  geom_point(color = "blue") +
  geom_line(linetype = "solid", size = 0.5, color = "blue") +
  ggalt::geom_xspline( size = 0.5, linetype = 'dashed') +
  facet_wrap(~kmeans_cluster, scales = "free_y") +
  theme_bw() +
  xlab("") +
  ylab("Count")

lp

It produces the following plot: enter image description here

Noticed that the dashed line is the intended smoothen line with ggalt::geom_xspline().

I intended the order of x-axis to be: c("NT", "IBD+PBS", "IBD+Serpin") Hence they are encoded as a factor.

How can I make it a smooth one like this? But with the intended x-axis order:

enter image description here


Solution

  • The spline function seems to be thrown off by reading the data in order of appearance, whereas you are plotting it in order of the factor. It looks like this can be addressed by sorting the data before geom_xspline sees it:

    lp_dat <- lp_dat[order(lp_dat$tc),]
    

    or:

    library(dplyr)
    lp_dat <- lp_dat %>% arrange(tc)
    

    then existing code:

    enter image description here