[英]ggplot2 stacked barplot size & color
These are probably some easy questions, but I 'm learning. 这些可能是一些简单的问题,但是我正在学习。 I'm using ggplot2 for stacked barplots and I figured out how to do that in a basic form: 我将ggplot2用于堆叠的条形图,并且想出了如何以基本形式进行操作:
df<- data
stack <-ggplot(df,aes(x=group, y=average.contribution, fill=taxa)) +
geom_bar(stat="identity")
(dataset with bacterial abundance)
but I have problems to adjust the size of the plot, I couldn't find good examples (probably with theme()
). 但是我在调整图的大小时遇到了问题,找不到很好的示例(可能是theme()
)。
In particular: 尤其是:
And are there color vectors of various themes out there I could use instead from scale_fill_brewer()
? 而且还有可以使用scale_fill_brewer()
代替的各种主题的颜色矢量吗? I need more than 7 colors. 我需要7种以上的颜色。
I found some solutions to make plot more attractive and to adjust the size by myself: 我找到了一些解决方案,以使图更具吸引力并自行调整尺寸:
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)
thus remains mostly the question about the color vectors 因此大部分仍然是关于颜色矢量的问题
When you want to have more than 7 colors, you have several options: 如果要使用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()
You can choose to set the colors maually: 您可以选择手动设置颜色:
# 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"))
You can also use the RColorBrewer
package: 您还可以使用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
For changing the distance of the axis label to the axis, you have at least two options: 要更改轴标签到轴的距离,至少有两个选择:
# 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
For changing the appearance of the text: 要更改文本的外观:
element_text(size=14, face="bold", color="red", family = "sans")
When you want to use a specific font, installing the extrafont
package is a possibility. 当您要使用特定字体时,可以安装extrafont
软件包。 See this answer 看到这个答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.