如何在geom_dotplot中使用颜色? [英] How do I use color in a geom_dotplot?

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

问题描述

我有这个点图:

  ggplot(mpg,aes(drv,hwy))+geom_dotplot(binwidth = 1,binaxis ='y',stackdir ='center') 

呈现为

我想按制造商给圆点上色.如果添加 fill 美观度:

  ggplot(mpg,aes(drv,hwy,fill =制造商))+geom_dotplot(binwidth = 1,binaxis ='y',stackdir ='center') 

它呈现为

添加颜色似乎在某种程度上击败了堆叠算法,导致点重叠.我该如何解决?

解决方案

这是我得到的最接近的内容:

  ggplot(mpg,aes(drv,hwy,fill =制造商))+geom_dotplot(stackdir ="centerwhole",stackgroups = TRUE,binpositions ="all",binaxis ="y",binwidth = 1)+theme_bw() 

如果您需要使所有内容都完美居中,我将回顾

I have this dotplot:

ggplot(mpg, aes(drv, hwy)) +
  geom_dotplot(binwidth = 1, binaxis = 'y', stackdir = 'center')

which renders as

I want to color the dots by manufacturer. If I add a fill aesthetic:

ggplot(mpg, aes(drv, hwy, fill = manufacturer)) +
  geom_dotplot(binwidth = 1, binaxis = 'y', stackdir = 'center')

it renders as

It looks like adding color has defeated the stacking algorithm somehow, causing the dots to overlap. How do I fix this?

解决方案

This is the closest I've gotten:

ggplot(mpg, aes(drv, hwy, fill = manufacturer)) +
  geom_dotplot(stackdir = "centerwhole",stackgroups = TRUE,
  binpositions = "all", binaxis = "y", binwidth = 1)+
  theme_bw()

If you need everything to be perfectly centered, I'd review this example of writing a for loop to plot three separate charts (one for each factor level), using a shared legend.

Edit:

And here's the chart following the linked process of using a function to combine three plots with the same legend:

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

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