Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用ggplot在一个图形上绘制多个方框图_R_Ggplot2 - Fatal编程技术网

R 如何使用ggplot在一个图形上绘制多个方框图

R 如何使用ggplot在一个图形上绘制多个方框图,r,ggplot2,R,Ggplot2,我正在运行以下代码行 bp <- ggplot(data, aes(x = Month, y = days, group = Month)) + geom_boxplot()+facet_wrap(~Depth) 如果这是一个重复的问题,我道歉,但我看的问题似乎不适合我的需要 我试过使用 bp + geom_boxplot(position = position_dodge(width = 0.8)) 如建议,但未创建一个图表 谢谢我不确定要求“深度颜色编码”是什么意思,所以让我们

我正在运行以下代码行

 bp <- ggplot(data, aes(x = Month, y = days, group = Month)) + geom_boxplot()+facet_wrap(~Depth)
如果这是一个重复的问题,我道歉,但我看的问题似乎不适合我的需要 我试过使用

bp + geom_boxplot(position = position_dodge(width = 0.8)) 
如建议,但未创建一个图表


谢谢

我不确定要求“深度颜色编码”是什么意思,所以让我们从要求“全部在一个绘图”开始。您可以使用交互功能构建一个双向分组,该分组将由
geom_boxplot
执行:

 bp <- ggplot(dat, aes(x = interaction(month,depth, sep=" x "), y = days)) + 
                   geom_boxplot()
 bp

bp如果我正确理解了您的问题,您的任务可以通过以下代码完成:

data <- read.table(text = "depth month days
25     1    49
25     1    51
100    1    52
100    1    55 
300    1    52
300    1    50
25     2    47
25     2    48
100    2    53
100    2    57
300    2    56
300    2    59", header = TRUE)
然后绘制箱线图,必须使用
scale\u color\u manual()
指定颜色


bp在我的交互式图形设备上,如果您在
参数后插入
粘贴
,这看起来好多了,非常感谢!这看起来似乎解决了我的问题,尽管我还不能让它与我的完整数据集一起工作。我想我应该能够让它工作起来,再加上一点变通谢谢你的建议42-第一个答案看起来是我所需要的,实际上提供的数据中有一个错误,它应该只有25个,如果每个因素组合只有一个值,那么使用箱线图就没有多大意义了。以下是我试图实现的
ggplot(数据,aes(x=因子(月),y=天,颜色=深度))+geom_箱线图(aes(组=组))
。我的描述可能没有解释清楚也许:
bp
 bp <- ggplot(data, aes(x = group, y = days, 
                        group = interaction(month, depth) , colour = as.factor(depth) )) + 
          geom_boxplot() 
 bp
data <- read.table(text = "depth month days
25     1    49
25     1    51
100    1    52
100    1    55 
300    1    52
300    1    50
25     2    47
25     2    48
100    2    53
100    2    57
300    2    56
300    2    59", header = TRUE)
data$group <- with(data, paste("Month:", month, ",depth:", formatC(depth, width = 3, flag = 0), sep = ""))
bp <- ggplot(data, aes(x = group, y = days, group = group, colour = group)) + geom_boxplot() + scale_colour_manual(values = rep(1:3, 2))
bp