How do I use color in a geom_dotplot?

给你一囗甜甜゛ 提交于 2021-02-16 13:30:08

问题


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?


回答1:


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:




回答2:


Using geom_beeswarm from package ggbeeswarm is an option. It doesn't center the even numbered rows of dots quite the same way, but the point color seems to work out better than geom_dotplot.

library(ggbeeswarm)

ggplot(mpg, aes(drv, hwy, color = manufacturer)) +
     geom_beeswarm(size = 2)



来源:https://stackoverflow.com/questions/45444848/how-do-i-use-color-in-a-geom-dotplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!