ggplot2 geom_bar 中的条形宽度

发布于 2024-08-22 23:36:30 字数 1999 浏览 3 评论 0原文

我正在尝试用循环生成图。

l1<-factor(rep(letters,4))
n1<-abs(rnorm(104))*10000
b1<-rep(c("1","2","3","4","5","6","7","8"),c(2,2,11,24,11,20,33,1))
k1<-rep((rep(c("A","B","C","D"),c(2,3,4,4))),8)
my.df<-data.frame(l1,b1,k1,n1)                            #make a dataframe

names(my.df)<-c("letter","branch","ltrtype","number")     #factor names
library(ggplot2)

branch.list<-unique(my.df$branch)
sayi<-length(branch.list)                                 # list of factor:letters

for (i in 1:sayi) {

branch.iter<-branch.list[i]
my.df.plot<-subset(my.df,my.df$branch==branch.iter,drop=T)

my.df.plot$branch<-factor(my.df.plot$branch)               #So that unused levels don't show up
my.df.plot$letter<-factor(my.df.plot$letter)               #So that unused levels don't show up
my.df.plot$ltrtype<-factor(my.df.plot$ltrtype)             #So that unused levels don't show up
my.df.plot$number<-as.numeric(as.character(my.df.plot$number))
my.df.plot<-data.frame(my.df.plot)

myfilename<-paste(branch.iter,".jpeg",sep="")
jpeg(file=myfilename)

cizim<-ggplot(my.df.plot,aes(letter,number,fill=ltrtype))
cizim<-cizim + geom_bar(width = 1, position = "fill", binwidth = 1) +     facet_grid(ltrtype~.)
cizim<-cizim + opts(title=branch.iter)

print(cizim)
dev.off()

}

(Q1):当x轴的层数改变时,条形的宽度改变 我怎样才能防止这种情况并使每个图中的条形宽度相同?

替代文本 http://img411.imageshack.us/i/95325388.jpg/

替代文本 http://img411.imageshack.us/i/91510133.jpg/

(Q2): 当i=7 R 给出以下警告时:

(data$ymin == 0)) warning("当 ymin != 0 时填充未明确定义") : 需要 TRUE/FALSE 时缺少值

我该怎么办?

(问题3):在这种情况下,有没有更简单的方法来删除未使用的级别,这样我就不必

 my.df.plot$branch<-factor(my.df.plot$branch)

每次都使用?

I am trying to produce plots with a loop.

l1<-factor(rep(letters,4))
n1<-abs(rnorm(104))*10000
b1<-rep(c("1","2","3","4","5","6","7","8"),c(2,2,11,24,11,20,33,1))
k1<-rep((rep(c("A","B","C","D"),c(2,3,4,4))),8)
my.df<-data.frame(l1,b1,k1,n1)                            #make a dataframe

names(my.df)<-c("letter","branch","ltrtype","number")     #factor names
library(ggplot2)

branch.list<-unique(my.df$branch)
sayi<-length(branch.list)                                 # list of factor:letters

for (i in 1:sayi) {

branch.iter<-branch.list[i]
my.df.plot<-subset(my.df,my.df$branch==branch.iter,drop=T)

my.df.plot$branch<-factor(my.df.plot$branch)               #So that unused levels don't show up
my.df.plot$letter<-factor(my.df.plot$letter)               #So that unused levels don't show up
my.df.plot$ltrtype<-factor(my.df.plot$ltrtype)             #So that unused levels don't show up
my.df.plot$number<-as.numeric(as.character(my.df.plot$number))
my.df.plot<-data.frame(my.df.plot)

myfilename<-paste(branch.iter,".jpeg",sep="")
jpeg(file=myfilename)

cizim<-ggplot(my.df.plot,aes(letter,number,fill=ltrtype))
cizim<-cizim + geom_bar(width = 1, position = "fill", binwidth = 1) +     facet_grid(ltrtype~.)
cizim<-cizim + opts(title=branch.iter)

print(cizim)
dev.off()

}

(Q1): When number of levels in x-axis change width of bars change
How can i prevent this and make bar width in every plot same?

alt text http://img411.imageshack.us/i/95325388.jpg/

alt text http://img411.imageshack.us/i/91510133.jpg/

(Q2): when i=7 R gives following warning:

(data$ymin == 0)) warning("Filling not well defined when ymin != 0") :
missing value where TRUE/FALSE needed

what can i do about it?

(Q3): Is there an easier way to drop unused levels in such a case so i don't have to use

 my.df.plot$branch<-factor(my.df.plot$branch)

everytime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-08-29 23:36:30

你正在制作一些非常奇怪的情节。通过使用 position="fill" 您可以将每个条形拉伸至高度 1(因为与该字母对应的一个观察值是与面板中该字母对应的所有观察值的 100%),完全失去了无论您想要绘制什么信息。我的猜测是你的一些问题源于这个错误,但我不确定。

(Q1) 您希望不同分支的图中的条形宽度相同吗?由于您要更改 x 变量的级别数,因此条形必须变宽才能填满绘图。一些解决方案选项:

  • 对绘图的宽度进行一些智能调整以解决该问题。
  • 保留所有 x 水平 - 我认为这是最干净的方法
  • 您可以使用 scale_x_discreteexpand 选项使条形更窄并在绘图上居中。因此,如果您总共有 N 个 x 值(此处 N=26 个字母),但特定图仅使用其中的 k 个,则添加 +scale_x_discrete(expand=c(0.05, (Nk)/2))到你的情节。第一项是乘法扩展因子,这是默认值,第二项是加法因子。

(Q2) i=7 是唯一具有与相同字母/ltrgroup 组合对应的多个数字值的组。酒吧 geom 不知道该怎么办。我同意错误消息确实很神秘。

(Q3)一种选择是避免使用因子 - 在组合字符向量时使用 data.frame(...,stringsAsFactors=FALSE) ,然后子集化将不会保留未使用的级别。

You are producing some very strange plots. By using position="fill" you are stretching out each bar to have height 1 (because the one observation corresponding to the letter is 100% of all observations corresponding to the letter within a panel), completely loosing whatever information you are trying to plot. My guess is that some of your questions stem from this mistake, but I am not sure.

(Q1) Do you want the bar width to be the same in plots for the different branches? Since you are changing the number of levels of the x variable, the bars have to get wider to fill up the plot. Some solution options:

  • Do some smart resizing of the width of the plot to go around that.
  • Leave all the x levels in - I think this is the cleanest way
  • You can get your bars narrower and centered on the plot by using the expand option of scale_x_discrete. So if you have N total x-values (here N=26 letters), but a particular plot only uses k of them, then add + scale_x_discrete(expand=c(0.05, (N-k)/2)) to your plot. The first term is a multiplicative expansion factor, and this is the default value, and the second term is an additive factor.

(Q2) i=7 is the only group that has multiple number values corresponding to the same letter/ltrgroup combination. The bar geom does not know what to do with that. I agree that the error message is really cryptic.

(Q3) One option is to avoid using factors - use data.frame(...,stringsAsFactors=FALSE) when combining character vectors, and then subsetting will not keep unused levels around.

很酷又爱笑 2024-08-29 23:36:30

(Q1)我认为不可能固定条形宽度。 Aniko 保留所有关卡的建议对我来说最有意义。

(Q2)将 binwidth = 1 替换为 stat="identity",因为我认为您不需要 stat="bin"

(Q3) 其他选项包括 gdata 包中的 drop.levelsHmisc 包中的 dropUnusedLevels

(Q1) I don't think it is possible to fix bar width. Aniko's suggestion to keep all the levels makes most sense to me.

(Q2) replace binwidth = 1 with stat="identity", as I don't think you need stat="bin".

(Q3) Other options include drop.levels in gdata-package, and dropUnusedLevels in Hmisc-package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文