force boxplots from geom_boxplot to constant width

后端 未结 2 1304
北荒
北荒 2021-02-19 04:01

I\'m making a boxplot in which x and fill are mapped to different variables, a bit like this:

ggplot(mpg, aes(x=as.factor(cyl), y=cty,          


        
2条回答
  •  逝去的感伤
    2021-02-19 04:52

    Just use the facet_grid() function, makes things a lot easier to visualize:

    ggplot(mpg, aes(x=as.factor(drv), y=cty, fill=as.factor(drv))) + 
        geom_boxplot() +
        facet_grid(.~cyl)
    

    See how I switch from x=as.factor(cyl) to x=as.factor(drv).
    Once you have done this you can always change the way you want the strips to be displayed and remove margins between the panels... it can easily look like your expected display.
    By the way, you don't even need to use the as.factor() before specifying the columns to be used by ggplot(). this again improve the readability of your code.

提交回复
热议问题