2014-03-03 76 views
0

这些可能是一些简单的问题,但我正在学习。 我使用GGPLOT2的堆叠barplots和我想出如何做到这一点的一种基本形式:ggplot2堆叠式barplot尺寸和颜色

df<- data 
stack <-ggplot(df,aes(x=group, y=average.contribution, fill=taxa)) + 
      geom_bar(stat="identity") 

(dataset with bacterial abundance) 

,但我有问题,调整图形大小,我找不到很好的例子(可能与theme())。

特别是:

  1. 限定的曲线的大小(例如,5厘米×5厘米),
  2. 限定条形的宽度
  3. 距离轴标签的对于所述轴线
  4. 字体用于剧情
  5. 定义图例的大小

有没有我可以用scale_fill_brewer()来代替各种主题的颜色矢量?我需要超过7种颜色。

+0

欢迎来到Stack Overflow!如果答案对您有用,如果您接受答案,将不胜感激。这将为未来的读者提供解决方案价值的线索。另见本帮助页面:[当某人回答我的问题时,我该怎么办?](http://stackoverflow.com/help/someone-answers) – Jaap

回答

0

当你想有超过7种颜色,你有几种选择:

# your plot code as a starting point 
stack <- ggplot(df,aes(x= group, y=average.contribution, fill=taxa)) + 
    geom_bar(stat="identity", width=.7) + 
    facet_grid(. ~ fac) + 
    scale_y_continuous(limits=c(0,100)) + 
    theme(panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank()) + 
    theme_bw() 

您可以选择设置颜色maually:

# manually with color names 
stack + scale_colour_manual(values=c("red", "blue")) # 2 colors as example, add as many as you need 

# manually with RGB values 
stack + scale_colour_manual(values=c("#CC6666", "#7777DD")) 

您还可以使用RColorBrewer包:

library(RColorBrewer) 
display.brewer.all() # shows the different palettes you can choose from 

stack + scale_fill_brewer(palette="Set3") # a color palette with 12 colors 

用于更改为轴轴标签的距离,至少有两种选择:

# inserting blank lines with \n 
stack + xlab("\nLabel") 

# vertically or horizontally adjusting the label 
stack + theme(axis.text.x = element_text(hjust=-1, vjust=-1)) # ylab -> hjust; xlab -> vjust 

用于更改文字的外观:

element_text(size=14, face="bold", color="red", family = "sans") 

当您想使用特定的字体时,安装extrafont包是一种可能性。见this answer

0

我找到了一些解决方案,使情节更加吸引人,并通过自己调整大小:

df<-so2 
stack<-ggplot(df,aes(x= group, y=average.contribution, fill=taxa)) 
+ geom_bar(stat="identity", width=.7) 
+ facet_grid(. ~ fac) +scale_y_continuous(limits=c(0,100)) 
+ theme(panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank()) 
+ theme_bw() 

ggsave(stack, file="stack.pdf", width=10, height=10) 

因此仍然主要是关于颜色向量问题