在ggplot2中的条形图之间添加空格 [英] Adding space between bars in ggplot2

查看:270
本文介绍了在ggplot2中的条形图之间添加空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,我想在ggplot2中的条形图之间添加空格。此页面提供了一个解决方案: http://www.streamreader.org/stats/questions/6204/how-to-increase-the-space-between-the-bars-in-a-bar-plot -in-GGPLOT2 。但是,此解决方案不是使用x轴组的因子水平,而是创建数字序列x.seq来手动放置条,然后使用width()参数对它们进行缩放。但是,如下例所示,当我使用x轴的因子级别分组时,width()不起作用。

  library(ggplot2)

处理< - rep(c('T','C '),each = 2)
性别< - rep(c('M','F'),2)
响应< - 样本(1:100,4)
df< - data.frame(治疗,性别,反应)

hist <-ggplot(df,aes(x = Gender,y = Response,fill = Treatment,stat =identity)) )
hist + geom_bar(position =dodge)+ scale_y_continuous(limits = c(0,
100),name =)

有人知道如何获得与链接示例中相同的效果,但是使用因子级别分组?

干杯,

亚伦

解决方案

这是你想要的吗?

  hist + geom_bar(width = 0.4,position = position_dodge(宽度= 0.5))




  • width geom_bar 决定栏的宽度。

  • width position_dodge 中确定每个柱的位置。


可能你可以轻松了解他们的行为后,你玩了一段时间。




Dear all, I'd like to add spaces between bars in ggplot2. This page offers one solution: http://www.streamreader.org/stats/questions/6204/how-to-increase-the-space-between-the-bars-in-a-bar-plot-in-ggplot2. Instead of using factor levels for the x-axis groupings, however, this solution creates a numeric sequence, x.seq, to manually place the bars and then scales them using the width() argument. width() doesn't work, however, when I use factor level groupings for the x-axis as in the example, below.

library(ggplot2)

Treatment <- rep(c('T','C'),each=2)
Gender <- rep(c('M','F'),2)
Response <- sample(1:100,4)
df <- data.frame(Treatment, Gender, Response)

hist <- ggplot(df, aes(x=Gender, y=Response, fill=Treatment, stat="identity"))
hist + geom_bar(position = "dodge") + scale_y_continuous(limits = c(0, 
    100), name = "") 

Does anyone know how to get the same effect as in the linked example, but while using factor level groupings?

Cheers,

Aaron

解决方案

Is this what you want?

hist + geom_bar(width=0.4, position = position_dodge(width=0.5))

  • width in geom_bar determines the width of the bar.
  • width in position_dodge determines the position of each bar.

Probably you can easily understand their behavior after you play with them for a while.

这篇关于在ggplot2中的条形图之间添加空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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