如何在没有数据的类别上强制执行ggplot的position_dodge? [英] How to enforce ggplot's position_dodge on categories with no data?

查看:139
本文介绍了如何在没有数据的类别上强制执行ggplot的position_dodge?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ggplot上使用position_dodge来获取共享相同类别(cat)的两个不同信号(ind)的箱形图。当一个信号的数据类别不同于另一个信号的数据类别时,带有数据的信号的箱形图覆盖了所有的水平间距,并且不考虑该特定类别的position_dodge指令。有没有办法让ggplot执行躲避规则?正如你在下面的例子中看到的那样,信号x没有类别B的数据,所以它失去了position_dodge保留的空间。

 数据< -data.frame(猫= C( 'A', 'A', 'A', 'A', 'B', 'B', 'A', 'A', 'A', 'A' ,'B','B'),
值= c(3,2,1,4,NA,NA,4,5,6,7,8,9),
ind = c ( 'X', 'X', 'X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'))
$ b print(ggplot()+
scale_colour_hue(guide ='none')+
geom_boxplot(
aes(x = as.factor(cat),y =值,
fill = ind),
position = position_dodge(width = .60),
data = data,
outlier.size = 1.2,
na.rm = T))



进度更新



经过一些解决方法之后,我想出了我正在寻找的结果......(种类)



<$ p ('A','A','A','A','B','B','','',' A','A','A','A','B','B','B'),
值= c(3,2,1,4,NA,NA, ('x','x','x','x','x','x','y',' y','y','y','y','y','x'))

p < - ggplot()+
scale_colour_hue(guide ='none' )+
geom_boxplot(aes(x = as.factor(cat),y = values,fill = ind),
position = position_dodge(width = .60),
data = data,
outlier.size = 1.2,
na.rm = T)+
geom_line(aes(x = x,y = y),
data = data.frame(x = c(0,3),y = rep(0,2)),
size = 1,
col ='white')
print(p)



有些人为了我想要的效果而使用faceting来混合使用。刻面不会给我我要找的效果。我查找的最终图如下所示:





我希望我们可以更加无缝地组合不同的geom对象。我将这个报告称为哈德利github上的一个bug,但是哈德利说这是position_dodge的设计行为。我想我以非标准的方式使用ggplot2,解决方法是解决这些问题的方法。无论如何,我希望这可以帮助一些R族人员进一步推动ggplot的强大功能。

解决方案

经过一些解决方法之后,我(类型)

 数据<  -  data.frame(
cat = c('A','A','A','A','B','B','A','A','A','A','B', 'B','B'),
值= c(3,2,1,4,NA,NA,4,5,6,7,8,9,0),
ind = C( 'X', 'X', 'X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'x'))

p < - ggplot()+
scale_colour_hue(guide ='none')+
geom_boxplot(aes(x = as.factor(cat), y =值,fill = ind),
position = position_dodge(width = .60),
data = data,
outlier.size = 1.2,
na.rm = T )+
geom_line(aes(x = x,y = y),
data = data.frame(x = c(0,3),y = rep(0,2)),
size = 1,
col = 'white')
print(p)



有些人推荐使用faceting来达到我想要的效果。刻面不会给我我要找的效果。我查找的最终图如下所示:





我希望我们可以更加无缝地组合不同的geom对象。我将这个报告称为哈德利github上的一个bug,但是哈德利说这是position_dodge的设计行为。我想我以非标准的方式使用ggplot2,解决方法是解决这些问题的方法。无论如何,我希望这可以帮助一些R人将ggplot强大的功能推进一点。


I'm trying to use position_dodge on ggplot to obtain boxplots of two different signals (ind) sharing the same categories (cat). When there is a category with data for one signal but not for the other one, the boxplot for the signal with data covers all the horizontal spacing, and does not respect the position_dodge instruction for that particular category. Is there a way to make ggplot to enforce the dodging rule? As you can see on the example below, the signal x has no data for category B, so it loses the space reserved by position_dodge. I would like to avoid that.

Thanks in advance.

data<-data.frame(cat=c('A','A','A','A','B','B','A','A','A','A','B','B'), 
                 values=c(3,2,1,4,NA,NA,4,5,6,7,8,9), 
                 ind=c('x','x','x','x','x','x','y','y','y','y','y','y'))

print(ggplot() +
        scale_colour_hue(guide='none') +
      geom_boxplot(
           aes(x=as.factor(cat), y=values, 
               fill=ind), 
           position=position_dodge(width=.60), 
           data=data,
           outlier.size = 1.2,
           na.rm=T))

PROGRESS UPDATE

After some workarounds, I came up with the outcome I was looking for... (kind of)

data            <- data.frame(
cat=c('A','A','A','A','B','B','A','A','A','A','B','B','B'), 
values=c(3,2,1,4,NA,NA,4,5,6,7,8,9, 0), 
ind=c('x','x','x','x','x','x','y','y','y','y','y','y','x'))

p  <- ggplot() +
      scale_colour_hue(guide='none') +
      geom_boxplot(aes(x=as.factor(cat), y=values, fill=ind),
      position=position_dodge(width=.60), 
      data=data,
      outlier.size = 1.2,
      na.rm=T) +
      geom_line(aes(x=x, y=y), 
                data=data.frame(x=c(0,3),y=rep(0,2)), 
                size = 1, 
                col='white')
print(p)

Some people remcomended using faceting for the effect I wanted. Faceting doesn't give me the effect I'm looking for. The final graph I was looking for is shown below:

If you notice, the white major tick mark at y = 10 is thicker than the other tick marks. This thicker line is the geom_line with size=1 that hides unwanted boxplots.

I wish we could combine different geom objects more seamlessly. I reported this as a bug on Hadley's github, but Hadley said this is how position_dodge behaves by design. I guess I'm using ggplot2 in a non-standard way and workarounds are the way to go on these kind of issues. Anyways, I hope this helps some of the R folks to push ggplot great functionality a little further.

解决方案

After some workarounds, I came up with the outcome I was looking for... (kind of)

data            <- data.frame(
cat=c('A','A','A','A','B','B','A','A','A','A','B','B','B'), 
values=c(3,2,1,4,NA,NA,4,5,6,7,8,9, 0), 
ind=c('x','x','x','x','x','x','y','y','y','y','y','y','x'))

p  <- ggplot() +
      scale_colour_hue(guide='none') +
      geom_boxplot(aes(x=as.factor(cat), y=values, fill=ind),
      position=position_dodge(width=.60), 
      data=data,
      outlier.size = 1.2,
      na.rm=T) +
      geom_line(aes(x=x, y=y), 
                data=data.frame(x=c(0,3),y=rep(0,2)), 
                size = 1, 
                col='white')
print(p)

Some people recommended using faceting for the effect I wanted. Faceting doesn't give me the effect I'm looking for. The final graph I was looking for is shown below:

If you notice, the white major tick mark at y = 10 is thicker than the other tick marks. This thicker line is the geom_line with size=1 that hides unwanted boxplots.

I wish we could combine different geom objects more seamlessly. I reported this as a bug on Hadley's github, but Hadley said this is how position_dodge behaves by design. I guess I'm using ggplot2 in a non-standard way and workarounds are the way to go on these kind of issues. Anyways, I hope this helps some of the R folks to push ggplot great functionality a little further.

这篇关于如何在没有数据的类别上强制执行ggplot的position_dodge?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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