4

我有以下数据框:

  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"))

我想要做的是平滑情节。下面是我使用的代码:

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

它产生以下情节: 在此处输入图像描述

请注意,虚线是ggalt::geom_xspline()的预期平滑线。

我打算 x 轴的顺序是: c("NT", "IBD+PBS", "IBD+Serpin") 因此它们被编码为一个因素。

我怎样才能让它像这样顺利呢?但使用预期的 x 轴顺序:

在此处输入图像描述

4

1 回答 1

7

通过按出现顺序读取数据,样条函数似乎被抛弃了,而您是按照因子的顺序绘制它。看起来这可以通过在geom_xspline看到数据之前对数据进行排序来解决:

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

或者:

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

然后是现有代码:

在此处输入图像描述

于 2019-08-29T17:18:24.333 回答