用ggplot2控制条宽

时间:2013-08-14 16:54:49

标签: r ggplot2

我正在尝试创建由建筑物镶嵌的条形图。为了便于阅读,我想为每组四栋建筑创建一个单独的图表。我希望所有图表中条形的宽度都相同。

我尝试过使用width命令。这可以调整特定绘图中的宽度,但不会使绘图中的宽度保持一致。看起来好像有一个覆盖函数,它根据变量Final的值的数量来修复宽度。有没有人知道要修正宽度以使其在各个图块中相同?

library(ggplot2)
building <- c(1,1,1,2,5,4,5,4,2,3,1,4,3,5,5)
Final       <- c("Developing","Developing","Developing","Skilled","Skilled","Skilled","Skilled","Skilled","Skilled","Skilled","Skilled","Skilled","Skilled","Accomplished","Accomplished")
mydata <- data.frame(building,Final)
str(mydata)

sub1 <- subset(mydata, building <= 4)
sub2 <- subset(mydata, building == 5)

p1 <- ggplot(sub1, aes(Final)) + 
  geom_bar(aes(fill=Final),position="dodge",colour="black",width=0.4) + 
  facet_wrap( ~ building,ncol=2) + coord_flip()
p1  + xlab("Final Summative Rating") +   
  scale_x_discrete(labels = c("","","","")) + 
  scale_fill_manual(
    values=c("Ineffective" ="red2","Developing" ="darkgoldenrod2",
             "Skilled" =  "olivedrab3","Accomplished"= "springgreen3")) + 
  theme(strip.text.x = element_text(size = 7.5)) + 
  ylab("Teacher Count") + 
  labs(fill = "Rating")

p2 <- ggplot(sub2, aes(Final)) + 
  geom_bar(aes(fill=Final),position="dodge",colour="black",width=0.4) + 
  facet_wrap( ~ building,ncol=2) + coord_flip()
p2  + xlab("Final Summative Rating") +   
  scale_x_discrete(labels = c("","","","")) + 
  scale_fill_manual(values=c("Ineffective" ="red2","Developing" ="darkgoldenrod2",
                             "Skilled" =  "olivedrab3","Accomplished"= "springgreen3")) + 
  theme(strip.text.x = element_text(size = 7.5)) + 
  ylab("Teacher Count") + labs(fill = "Rating")

0 个答案:

没有答案