如何在fill和group中使用ggplot2的geom_dotplot() [英] How to use ggplot2's geom_dotplot() with both fill and group

查看:794
本文介绍了如何在fill和group中使用ggplot2的geom_dotplot()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢 ggplot2 :: geom_dotplot()可以很好地将点放在类别的中间,但我似乎无法将它与填充颜色结合起来。



让我们来看一个例子:

 #test data 
tmpData< - data.frame(x = c(rep('x',3),rep('y',3)),y = c(1,1,2,1,2,2 ),fill = rep(c('A','B','B'),2))

#没有填充颜色的绘图
ggplot(tmpData,aes(x = x ,y = y))+
geom_dotplot(binaxis =y,stackdir =center,dotsize = 4)

结果如下:





但是当我添加fill参数时:

  ggplot(tmpData,aes(x = x,y = y,fill = fill))+ 
geom_dotplot(binaxis =y,stackdir =center,dotsize = 4)

填充似乎会覆盖在x上完成的分组,导致两个点(x,1) (x,1)被折叠,我希望它们有不同的颜色。




$ b $

  ggplot(tmpData,aes(x = x,y = y,group = x,fill = fill))+ 
geom_dotplot(binaxis =y,stackdir =center,dotsize = 4)



通过启用堆栈组可以避免崩溃:

  ggplot(tmpData,aes(x = x,y = y,fill = fill))+ 
geom_dotplot(binaxis =y,stackgroups = TRUE,stackdir =center,dotsize = 4)



但是后来我失去了将数据集中到x和y在其他3个地块中找到。



有没有办法使用 geom_dotplot()与两个组并填充?

解决方案

你可以打开一些简单的解决方案来获得它想要的样子......你可以通过简单地提供一个颜色名称向量来覆盖fill命令:

  tmpData $ colorname<  -  rep(c('red','blue','blue'),2)

ggplot( tmpData,aes(x = x,y = y))+
geom_dotplot(binaxis =y,stackdir =center,dotsize = 4,fill = tmpData $ colorname)

< img src =https://i.stack.imgur.com/czLxZ.pngalt =在这里输入图片描述>


I really like the way the ggplot2::geom_dotplot() can nicely stack dots towards the middle of a category but I cannot seem to combine that with a fill color.

Lets take a look at an example:

# test data
tmpData <- data.frame(x=c(rep('x', 3),rep('y', 3)), y=c(1,1,2,1,2,2), fill=rep(c('A', 'B', 'B'), 2))

# Plot without fill color
ggplot(tmpData, aes(x=x, y=y)) + 
    geom_dotplot(binaxis = "y", stackdir = "center", dotsize=4)

Resulting in this plot:

But when I add the fill argument:

ggplot(tmpData, aes(x=x, y=y, fill=fill)) +   
   geom_dotplot(binaxis = "y", stackdir = "center", dotsize=4)

The fill seems to overwrites the grouping done on "x" causing the two points (x, 1)(x, 1) to be collapsed I would like them to have different colors.

When I try to specify the group the fill color is ignored:

ggplot(tmpData, aes(x=x, y=y, group=x, fill=fill)) +
    geom_dotplot(binaxis = "y", stackdir = "center", dotsize=4)

The collapsing seems to be avoidable by enabling stackgroups:

ggplot(tmpData, aes(x=x, y=y, fill=fill)) +
    geom_dotplot(binaxis = "y", stackgroups=TRUE, stackdir = "center", dotsize=4)

But then I lose the centering of the data to the "x" and "y" that are found in the other 3 plots.

Is there a way to use geom_dotplot() with both groups and fill?

解决方案

If you're open to a bit of a hacky solution just to get it how you want it to look... You can overwrite the fill command by simply providing it with a vector of color names:

tmpData$colorname <- rep(c('red','blue','blue'),2)

ggplot(tmpData, aes(x=x, y=y)) + 
  geom_dotplot(binaxis = "y", stackdir = "center", dotsize=4, fill=tmpData$colorname)

这篇关于如何在fill和group中使用ggplot2的geom_dotplot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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