如何将ggplot2的geom_dotplot()与fill和group一起使用

时间:2015-07-22 08:34:51

标签: r ggplot2

我非常喜欢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))

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

导致这个情节:

Dotplot without color

但是当我添加fill参数时:

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

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

Dotplot with color

当我尝试指定组时,忽略填充颜色:

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

Dotplot with group specified

启用堆栈组似乎可以避免崩溃:

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

dotpoot with stackgroups

然后我失去了数据的中心到&#34; x&#34;和&#34; y&#34;在其他3个地块中找到了。

有没有办法在两个群组中使用geom_dotplot()并填写?

2 个答案:

答案 0 :(得分:6)

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:

public FrameworkModel(TypeOfResourceHome resources)
{
    page= new PageModel();
    page.Title = localization.LocalizeText(resources.home.Title);
    ///....
}

enter image description here

答案 1 :(得分:2)

我认为您必须添加参数position = "dodge"

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

enter image description here