R绘图_ggplot2绘制Bar Plot

2020-02-16  本文已影响0人  谢俊飞

ggplot2在线学习:STHDA :Statistical tools for high-throughput data analysis.
ggplot2使用说明:https://ggplot2.tidyverse.org/reference/

Bar plot

英文:http://www.sthda.com/english/wiki/ggplot2-barplots-quick-start-guide-r-software-and-data-visualization

根据说明文档,运行代码……

#generate some data
df <- data.frame(dose=c("D0.5", "D1", "D2"),
                 len=c(4.2, 10, 29.5))
head(df)
#basic bar plot
p <- ggplot(data=df,aes(x=dose, y=len)) +
  geom_bar(stat = "identity")
p
#horizontal plot
p + coord_flip()
#change the wide of the bars
ggplot(df,aes(x=dose, y=len))+
  geom_bar(stat = "identity", width = 0.5)

#change colors
ggplot(df, aes(x=dose,y=len))+
  geom_bar(stat="identity",color="blue",fill="white" )
# Minimal theme + blue fill color
p<-ggplot(data=df, aes(x=dose, y=len)) +
  geom_bar(stat="identity", fill="steelblue")+
  theme_minimal()
p

#bar plot with labs
# Outside bars
ggplot(data=df, aes(x=dose, y=len)) +
  geom_bar(stat="identity", fill="steelblue")+
  geom_text(aes(label=len), vjust=5, size=3.5)+
  theme_minimal()

# Inside bars
ggplot(data=df, aes(x=dose, y=len)) +
  geom_bar(stat="identity", fill="steelblue")+
  geom_text(aes(label=len), vjust=1.6, color="white", size=3.5)+
  theme_minimal()

#to make a barplot of counts, we will use the mtcars data sets:
head(mtcars)
#don't map a variable to y
ggplot(mtcars, aes(x=factor(cyl))) +
  geom_bar(stat = "count",width = 0.7, fill = "steelblue") +
  theme_minimal()

#change the barplot line colors by groups
p <- ggplot(df, aes(x=dose, y=len, color=dose)) +
  geom_bar(stat = "identity", fill ="white")
p
#use the custom color palettes
p + scale_color_manual(values = c("#999999","#E69F00","#56B4E9"))
#use brewer color palettes
p + scale_color_brewer(palette = "Dark2")
#use grey scale
p + scale_color_grey() + theme_classic()

#change barplot fill colors by groups
p <- ggplot(df, aes(x=dose, y=len, fill=dose)) +
  geom_bar(stat = "identity") + theme_minimal()
p
# Use custom color palettes
p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# use brewer color palettes
p+scale_fill_brewer(palette="Dark2")
# Use grey scale
p + scale_fill_grey()

ggplot(df, aes(x=dose, y=len, fill=dose))+
  geom_bar(stat="identity", color="black")+
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  theme_minimal()

# Change bar fill colors to blues
p <- p+scale_fill_brewer(palette="Blues")
p + theme(legend.position="top")
p + theme(legend.position="bottom")
# Remove legend
p + theme(legend.position="none")


p + scale_x_discrete(limits=c("D2", "D0.5", "D1"))
#Barplot with multiple groups
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                  dose=rep(c("D0.5", "D1", "D2"),2),
                  len=c(6.8, 15, 33, 4.2, 10, 29.5))
head(df2)
# Stacked barplot with multiple groups
ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
  geom_bar(stat="identity")

# Use position=position_dodge()
ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
  geom_bar(stat="identity", position=position_dodge())

# Change the colors manually
p <- ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
  geom_bar(stat="identity", color="black", position=position_dodge())+
  theme_minimal()

# Use custom colors
p + scale_fill_manual(values=c('#999999','#E69F00'))
# Use brewer color palettes
p + scale_fill_brewer(palette="Blues")

ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
  geom_bar(stat="identity", position=position_dodge())+
  geom_text(aes(label=len), vjust=1.6, color="white",
            position = position_dodge(0.9), size=3.5)+
  scale_fill_brewer(palette="Paired")+
  theme_minimal()
上一篇 下一篇

猜你喜欢

热点阅读