从ggplot2中的错误栏中删除端点 [英] Remove endpoints from error bars in ggplot2

查看:131
本文介绍了从ggplot2中的错误栏中删除端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在R中创建箱形图(不必与ggplot2一起使用,但这就是我现在正在使用的),在样式上与我在某处(减去文本)找到的示例类似:

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

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

这将导致:

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

解决方案

errorbar地理区域中的width控制水平端筋的宽度,因此将其设置为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"))

My goal is to create boxplots in R (doesn't have to be with ggplot2, but that's what I'm using now) that are as stylistically similar to this example that I found somewhere (minus the text):

Here's the code I have so far:

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

Which results in:

Obviously there are a lot of differences between what I have and what the example shows, but right now I'm only focused on removing the endpoints from the error bars, by which I mean the horizontal top and bottom parts created by the stat_boxplot function. Does anyone know a way I can get the desired effect?

解决方案

The width in the errorbar geom controls the width of the horizontal end bars, so set that to 0 to remove the end bars. You are missing the dodging in the stat_boxplot layer, so you can add this in to get the error bars dodged correctly.

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

这篇关于从ggplot2中的错误栏中删除端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆