从 ggplot2 中的误差条中删除端点

标签 r ggplot2 boxplot errorbar

我的目标是在 R 中创建箱线图(不必使用 ggplot2,但这就是我现在正在使用的),它们在风格上与我在某处找到的这个示例(减去文本)相似:

boxplot-example

这是我到目前为止的代码:

dat <- read.table(file = "https://www.dropbox.com/s/b59b03rc8erea5d/dat.txt?dl=1", header = TRUE, sep = " ")
library(ggplot2)
p <- ggplot(dat, aes(x = Subscale, y = Score, fill = Class))
p + stat_boxplot(geom = "errorbar", width = 1.2, size = 2.5, color = "#0077B3") +
  geom_boxplot(outlier.shape = NA, coef = 0, position = position_dodge(.9)) +
  scale_fill_manual(values = c("#66CCFF", "#E6E6E6")) +
  theme(panel.background = element_rect(fill = "white", color = "white"))

结果是:

my-boxplot

显然,我所拥有的与示例显示的内容之间存在很多差异,但现在我只专注于从误差条中删除端点,我的意思是由 stat_boxplot 创建的水平顶部和底部部分。功能。有谁知道我可以获得预期效果的方法?

最佳答案

widtherrorbar geom 控制水平端条的宽度,因此将其设置为 0 以删除端条。您错过了 stat_boxplot 中的闪避层,因此您可以添加它以正确躲避误差线。

ggplot(dat, aes(x = Subscale, y = Score, fill = Class)) +
    stat_boxplot(geom = "errorbar", width = 0, size = 2.5, 
               color = "#0077B3", position = position_dodge(.9)) +
    geom_boxplot(outlier.shape = NA, coef = 0, position = position_dodge(.9)) +
    scale_fill_manual(values = c("#66CCFF", "#E6E6E6")) +
    theme(panel.background = element_rect(fill = "white", color = "white"))

enter image description here

关于从 ggplot2 中的误差条中删除端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39274692/

相关文章:

r - 使用ggplot2更改R中的x轴刻度标签

r - 按在 R 中保留名称的值对列表进行排序

r - 从 R 中以 Word 格式将数据导出到表格

r - 跨多个数据帧的经纬度之间的最小距离

r - 使用另一个变量填充(类似于geom_tile)的Density2d图?

r - ggplot2 - R - 如何修复图例中气泡的大小

r - 如何使用ggplot2用正条和负条标记条形图条

r - 为什么 geom_boxplot 比 base boxplot 识别更多异常值?

r - ggplot2:强制为空的二级类别留出空间

r - 帮助 R 中的 ave() 函数