ggplot不能分组吧 [英] ggplot can not group bars

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

问题描述

谁能告诉我ggplot为什么不能给我分组的条形图?

Who can tell me why ggplot can't give me grouped bars?

ggplot(df, aes(x = factor(labels), y = srednia, dodge=factor(group))) + 
    labs(title = gen, size=3)+ ylab("Fold change")+ xlab("Linnia komórkowa") +
    geom_bar(aes(fill=factor(group)),stat="identity",position ="dodge") + 
    geom_errorbar(aes(ymin=minus, ymax=plus))

分组的条,我的意思是这样的(绘画艺术):

Grouped bars I means something like this (paint art):

提前谢谢!

推荐答案

我想您可以通过更改x轴的比例来实现.这是一个可复制的示例和可能的解决方案.

I guess you can achieve this by changing the scale for the x axis. Here's a reproducible example and a possible solution.

# packages
require(plyr)
require(ggplot2)
# generate data
set.seed(123)
df <- data.frame(labels=LETTERS[1:6], 
                 group=rep(1:3, each=2),
                 srednia=runif(6))
# limits for x axis
mylims <- head(unlist(dlply(df, .(group), function(x) c(levels(factor(x$labels)), "space"))), -1)
# additional space between groups
ggplot(df, aes(x = factor(labels), y = srednia, dodge=factor(group))) + 
  geom_bar(aes(fill=factor(group)),stat="identity") + 
  scale_x_discrete(limits=mylims, breaks=levels(factor(df$labels)))
# removing space within group
ggplot(df, aes(x = factor(labels), y = srednia, dodge=factor(group))) + 
  geom_bar(aes(fill=factor(group)),stat="identity", width=1) + 
  scale_x_discrete(limits=mylims, breaks=levels(factor(df$labels)))

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

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