制作一个 ggpaired 图,其中 line.color 是一个加权函数?

Making a ggpaired plot where line.color is a weighted function?

我有一些治疗前后的数据,想看看配对数据,所以我求助于ggpaired。我能够使它与我的数据一起正常工作。 (我模拟了一些与我正在使用的数据相似的数据,以便其他人可以乱用它。)

set.seed(123)
size <- 28
a.d <- round(runif(size, 1, 4))
g.d <- round(runif(size, 1, 4))
s.d <- round(runif(size, 1, 4))
p.d <- round(runif(size, 1, 4))
a.f <- a.d + round(runif(1,-1,1))
g.f <- g.d + round(runif(1,-1,1))
s.f <- s.d + round(runif(1,-1,1))
p.f <- p.d + round(runif(1,-1,1))

df.t <- data.frame("A" = c(a.d,a.f),"G" = c(g.d,g.f),"S" = c(s.d,s.f),"P" = c(p.d,p.f),"V" = c(rep("D", size),rep("F", size)))

然后绘图(我已经安装并加载了软件包ggpubrgridExtraggplot2):

p <- list()
for(i in colnames(df.t[,-5])){
    d <- head(df.t[i],nrow(df.t)/2)
    d <- d[,i]
    f <- tail(df.t[i],nrow(df.t)/2)
    f <- f[,i]
    fin <- data.frame(draft = d, final = f)

    p[[i]] <- ggpaired(fin, cond1 = "draft", cond2 = "final", fill = "condition", line.color = "gray", line.size = 0.4, palette = "jco", xlab = "Draft version", ylab = paste(colnames(df.t[i]),"rating"), title = paste("Paired box plot of",colnames(df.t[i]),"ratings"))
}

do.call(grid.arrange,p)

生成图像:

这很好,但我有很多值,例如,预处理值 2,然后是处理值 1 post,你真的不能按原样使用线条颜色将其可视化。在谷歌搜索时,我遇到了 ,我认为这不是我需要的。我不知道如何最好地表达这个问题并继续寻找网络边缘线宽的结果。

基本上,我想做的是:如果我有 11 个从 3 到 2 的观察值,我希望从 3 到 2 的线比从 1 到 0 的线更暗,它只有 3 个观察值,有点像我在 Paint 中做的这个非常快速的模型:

我希望可以用 line.color(或者可能用 line.weight?)做这样的事情,并制作一个根据重量(或更具体地说)为线条着色的函数,计数),但我不是 R 的最佳人选(而且是新手),所以任何帮助将不胜感激,因为我不知道如何开始做这样的事情,以及我所做的一切 google 关于这个主题似乎与网络图有关。

你想要的当然是可能的,但使用 ggpaired 可能不是最直接的方法(免责声明:我不怎么使用 ggpubr 包。)

ggpaired 本质上是对底层 ggplot2 包函数的包装。如果你想改变事情的完成方式,改变底层功能是一种干净的方法。 (如果你以后打算用R的话,踏踏实实做事也是一个很好的学习方法。)

这是我的做法,从原始数据帧开始 df.t:

library(dplyr)

df.t %>%
  mutate(pair.order = rep(seq(1, n()/2), times = 2)) %>% # add new column to keep track of pairs
  tidyr::pivot_longer(cols = A:P, names_to = "facet") %>% # convert data to long form so all 
                                                          # values are captured in one variable
  ggplot(aes(x = V, y = value, fill = V)) +
  geom_boxplot() +  
  geom_line(data = . %>%                             # further data manipulation for line layer
              tidyr::pivot_wider(names_from = V) %>% # arrange values in pairs
              count(facet, D, F) %>%                 # & aggregate them for each treatment
              mutate(n = cut(n, breaks = c(0, 5, 10, Inf),
                             labels = c("n \u2264 5", "6 < n \u2264 10", "n > 10"))) %>%
              mutate(line.group = seq(1, n())) %>%   # add grouping identifier for line
              tidyr::pivot_longer(cols = D:F, names_to = "V"),  # return to long form
            aes(group = line.group, 
                colour = forcats::fct_rev(n)), # reverse category order for count
            size = 2) +                        # increase line size for easier comparison
  
  facet_wrap(~facet,     # split into 4 plot facets, one for each treatment
             labeller = labeller(facet = function(x) paste("Paired boxplot of", x, "ratings"))) +
  scale_x_discrete(labels = c("draft", "final")) +
  labs(y = "rating", colour = "Number of\ncounts") +
  ggsci::scale_fill_jco(guide = FALSE) + # not showing legend since it's the same as x-axis
  scale_colour_grey() +
  theme_pubr() +
  theme(legend.position = "right",
        axis.title.x = element_blank())