ggplot中的分层轴? [英] Layered axes in ggplot?

查看:349
本文介绍了ggplot中的分层轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在GGLPOT2(或其他图形包;我只是更喜欢ggplot)中制作分层/分段轴。



我想要做的就是取下面的数据,制作一个叠加的条形图,其中x轴上有Period,但是在每个周期内,每个动物也是如此。然后,每个动物中的酒吧的颜色将是'颜色'变量。

  set.seed(1234)
数据< - data.frame(
animal = sample(c('bear','tiger','lion'),50,replace = T),
color = sample(c('black ','brown','orange'),50,replace = T),
period = sample(c('first','second','third'),50,replace = T),
value = sample(1:100,50,replace = T))

一个堆积的条形图:

  library(ggplot2)
plot < - ggplot(data,aes(x = period ,y = value,fill = color))+
geom_bar(stat ='identity')

其中产生这一点:





但是我真正想要的是,在每个时间段内,每个动物都有三个单独的堆叠条(按颜色堆叠)。



我有一种感觉,我在这里错过了一个简单的语法,但'明显'的东西似乎不起作用,例如,

$ (数据,aes(x = c(周期,动物),y =值,填充=颜色))+ $ b(b,b
$ b

  $ b geom_bar(stat ='identity')


解决方案

Two步骤如下:


  1. group = animal 审美添加到(告诉它由动物分组)


  2. position =dodge code> geom_bar 图层(告诉它应该分开)


因此:

  ggplot(data,aes(x = period,y = value,fill = color,group = animal,color =动物))+ 
geom_bar(stat =identity,position =dodge)

这看起来像:





其中之一就是这里起诉的是它没有描述哪个动物是哪个:没有特别简单的方法来解决这个问题。这就是为什么我可能通过构建这个图:

  ggplot(data,aes(x = animal,y = value,fill = color))+ geom_bar(stat =identity)+ 
facet_wrap(〜period)


I'm wondering if it's possible to make layered/segmented axes in GGLPOT2 (or another graphics package; I just prefer ggplot).

What I want to do is take the below data, make a stacked bar chart that has Period on the x-axis, but within each period, each animal as well. Then the colors of the bars within each animal will be the 'color' variable

set.seed(1234)
data <- data.frame(
    animal = sample(c('bear','tiger','lion'), 50, replace=T),
    color = sample(c('black','brown','orange'), 50, replace=T),
    period = sample(c('first','second','third'), 50, replace=T),
    value = sample(1:100, 50, replace=T))

then putting this into a stacked bar chart:

library(ggplot2)
plot <- ggplot(data, aes(x=period, y=value, fill=color)) + 
    geom_bar(stat='identity')

Which produces this:

But what I really want is, within each bar for period, three separate stacked bars for each animal (stacked by color).

I have a feeling I'm missing a simple piece of syntax here, but the 'obvious' things don't seem to work, eg,

plot <- ggplot(data, aes(x=c(period,animal), y=value, fill=color)) + 
    geom_bar(stat='identity')

解决方案

Two steps to doing this:

  1. Add the group=animal aesthetic to the plot (tell it to group by animal)

  2. Add position="dodge" to your geom_bar layer (tell it the bars should be separate)

Thus:

ggplot(data, aes(x=period, y=value, fill=color, group=animal, color=animal)) +
        geom_bar(stat="identity", position="dodge")

This looks like:

One of the issues here is that it doesn't describe which animal is which: there isn't a particularly easy way to fix that. That's why I would probably make this plot through faceting:

ggplot(data, aes(x=animal, y=value, fill=color)) + geom_bar(stat="identity") +
    facet_wrap(~ period)

这篇关于ggplot中的分层轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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