R ggplot2:Barplot部分/半堆栈 [英] R ggplot2: Barplot partial/semi stack

查看:398
本文介绍了R ggplot2:Barplot部分/半堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个barplot的情况,我希望一些层次被堆积,有些需要躲开。这可能吗?

 图书馆(ggplot2)
图书馆(重塑)

数据< - data.frame(名称= c(a,b,c,d),
full = c(124,155,122,145),
parta = c(86, $ d $ b partb = c(38,83,82,119))

dat1< - reshape :: melt(dat,id.vars = c(name ))

#stack
ggplot(dat1,aes(x = name,y = value,group = variable,fill = variable))+
geom_bar(stat =identity ,position =stack)+
ggtitle(stack)

#dodge
ggplot(dat1,aes(x = name,y = value,group = variable ,fill = variable))+
geom_bar(stat =identity,position =dodge)+
ggtitle(dodge)



在上例中,图1和图2(左起)是标准的条形图。图3被修改为我正在寻找的内容。我想让'完整'级别保持'闪避',而'parta'和'partb'要堆叠在一起。

这是一个虚拟的例子。但是,实际应用是当我想为某件东西显示
的全部价值,然后展示它如何分解成子部件。上面的例子是一个单独的分割。也许人们可能想要展示多个分割。见下图。





红色是由一定量蓝色和绿色。绿色还包含一定量的黄色和粉红色。也许有更好的方法来做到这一点。

解决方案

这就是你想要的吗?

 #第一层
g1 < - ggplot(dat1% >%filter(variable ==full),
aes(x = as.numeric(name) - 0.15,weight = value,fill = variable))+
geom_bar(position =identity ,width = 0.3)+
scale_x_continuous(breaks = c(1,2,3,4),labels = unique(dat1 $ name))+
labs(x =name)

第二层
g1 + geom_bar(data = dat1%>%filter(grepl(part,variable)),
aes(x = as.numeric(name) + 0.15,fill = variable),
position =stack,width = 0.3)


I have a barplot situation where I would like some of the levels to be stacked and some to be dodged. Is this possible? Here is a working example.

library(ggplot2)
library(reshape)

dat <- data.frame(name=c("a","b","c","d"),
           full=c(124,155,122,145),
           parta=c(86,72,40,26),
           partb=c(38,83,82,119))

dat1 <- reshape::melt(dat,id.vars=c("name"))

#stack
ggplot(dat1,aes(x=name,y=value,group=variable,fill=variable))+
  geom_bar(stat="identity",position="stack")+
  ggtitle("stack")

#dodge
ggplot(dat1,aes(x=name,y=value,group=variable,fill=variable))+
  geom_bar(stat="identity",position="dodge")+
  ggtitle("dodge")

In the above example, figure 1 and figure 2 (from left) are standard barplots. Figure 3 is modified to what I am looking for. I would like level 'full' to stay 'dodged' while 'parta' and 'partb' to be stacked together.

This is a dummy example. But, the practical use is when I want to show full value for something and then show how that splits into sub-components. The example above is a single split. Perhaps one might want to show multiple splits. See figure below.

Red is composed of a certain amount of blue and green. Green is further composed of a certain amount of yellow and pink. Maybe there is a better way to do this.

解决方案

Is this what you want? I adapted from the link which @Henrik pointed out.

# 1st layer
g1 <- ggplot(dat1 %>% filter(variable == "full"),
             aes(x=as.numeric(name) - 0.15, weight=value, fill=variable)) +
  geom_bar(position="identity", width=0.3) + 
  scale_x_continuous(breaks=c(1, 2, 3, 4), labels=unique(dat1$name)) +
  labs(x="name")

# 2nd layer
g1 + geom_bar(data=dat1 %>% filter(grepl("part", variable)),
              aes(x=as.numeric(name) + 0.15, fill=variable),
              position="stack", width=0.3)

这篇关于R ggplot2:Barplot部分/半堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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