从cenboxplot()生成的进一步样式化框图

时间:2018-04-18 17:34:10

标签: r ggplot2 boxplot

我正在使用箱图分析审查数据,并使用cenboxplot()包(info here)中的函数NADA。我目前只知道如何在基础R中生成这些图。但有人知道是否有办法通过ggplot格式对这些图表进行样式化或创建小提琴图(即geom_violin())?

cenboxplot()的结构低于&示例数据框,其中“ResultCen”值为1表示该值低于检测限值,值为0表示值=结果值(该行是检测限制):

library("NADA")
Result <- as.numeric(c(1.2, .03, .05, .2, .02, .22, 1.1, .02))
ResultCen <- as.logical(c(0, 0, 0, 0, 1, 0, 0, 1))
Group <- as.factor(c("a", "a", "b", "b", "b", "c", "c", "c"))
x <- data.frame(Result, ResultCen, Group)
cenboxplot(x$Result, x$ResultCen, group=x$Group)

Censored Boxplot Example

1 个答案:

答案 0 :(得分:1)

You'll have to be more specific on what you actually want the plot to look like, but this code can at least get you started I think.

library(ggplot2)
library(NADA)
library(dplyr)

Result = as.numeric(c(1.2, .03, .05, .2, .02, .22, 1.1, .02))
ResultCen = as.logical(c(0, 0, 0, 0, 1, 0, 0, 1))
Group = as.factor(c("a", "a", "b", "b", "b", "c", "c", "c"))
x = data.frame(Result, ResultCen, Group)
df = cenboxplot(x$Result, x$ResultCen, group=x$Group)

max_threshold = x %>%
  filter(ResultCen == TRUE) %>%
  summarise(max = max(Result))

plot = ggplot(df, aes(x=group, y=ros.model, fill = group)) +
  geom_boxplot(coef=1.5
               , outlier.shape = NA) +
  geom_hline(yintercept = max_threshold[[1]], color = "red") +
  theme_bw() +
  xlab("Just for Example") +
  labs(title = "Pick your title")

print(plot)

enter image description here