ggplot2 barplot中的可变宽度条R [英] Variable width bars in ggplot2 barplot in R

查看:216
本文介绍了ggplot2 barplot中的可变宽度条R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个条形宽度由整数变量(样本大小)决定的条形图。添加width = variableName似乎不起作用。有没有一个确定的方法来做到这一点?这里有一些虚拟数据和代码。我想在这个例子中bar宽度是变量d的函数。

  dat<  -  data.frame(a = c(A,B,C),b = c(0.71,0.94,0.85),d = c(32,99,18))

ggplot(dat,aes (x = a,y = b,fill = a))+
geom_bar(color =black,size = .3)+
theme_bw()


解决方案如何在重新缩放后使用 width = d 向量,用一个常量表示?

  ggplot(dat, aes(x = a,y = b,width = d / 100))+ 
geom_bar(aes(fill = a),stat =identity,position =identity)


I'm trying to produce a barplot with bar widths determined by an integer variable (sample size). Adding "width = variableName" doesn't seem to work. Is there an established way of doing this? Here's some dummy data and code. I want the bar widths to be a function of variable d in this example.

dat <- data.frame(a=c("A", "B", "C"), b=c(0.71, 0.94, 0.85), d=c(32, 99, 18))

ggplot(dat, aes(x=a, y=b, fill=a)) +  
geom_bar(colour="black", size=.3) + 
theme_bw()  

解决方案

How about using width= after rescaling your d vector, say by a constant amount?

ggplot(dat, aes(x=a, y=b, width=d/100)) + 
  geom_bar(aes(fill=a), stat="identity", position="identity")

这篇关于ggplot2 barplot中的可变宽度条R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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