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:
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()
).
In particular:
And are there color vectors of various themes out there I could use instead from scale_fill_brewer()
? I need more than 7 colors.
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:
# 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:
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. See this answer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.