![](/img/trans.png)
[英]How to plot a stacked barplot in ggplot2, with different shades of the given color
[英]ggplot2 stacked barplot size & color
這些可能是一些簡單的問題,但是我正在學習。 我將ggplot2用於堆疊的條形圖,並且想出了如何以基本形式進行操作:
df<- data
stack <-ggplot(df,aes(x=group, y=average.contribution, fill=taxa)) +
geom_bar(stat="identity")
(dataset with bacterial abundance)
但是我在調整圖的大小時遇到了問題,找不到很好的示例(可能是theme()
)。
尤其是:
而且還有可以使用scale_fill_brewer()
代替的各種主題的顏色矢量嗎? 我需要7種以上的顏色。
我找到了一些解決方案,以使圖更具吸引力並自行調整尺寸:
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)
因此大部分仍然是關於顏色矢量的問題
如果要使用7種以上的顏色,則有幾種選擇:
# your plot code as a starting point
stck <- 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()
您可以選擇手動設置顏色:
# manually with color names
stck + scale_colour_manual(values=c("red", "blue")) # 2 colors as example, add as many as you need
# manually with RGB values
stck + scale_colour_manual(values=c("#CC6666", "#7777DD"))
您還可以使用RColorBrewer
軟件包:
library(RColorBrewer)
display.brewer.all() # shows the different palettes you can choose from
stck + scale_fill_brewer(palette="Set3") # a color palette with 12 colors
要更改軸標簽到軸的距離,至少有兩個選擇:
# inserting blank lines with \n
stck + xlab("\nLabel")
# vertically or horizontally adjusting the label
stck + 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
軟件包。 看到這個答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.