ggplot2-堆积柱形图????

  1. 数据整理ggplot2-堆积柱形图????_第1张图片

  2. 代码:

lin2 <- read.delim("~/1/lin2.txt")
View(lin2)
ggplot(data=lin2, aes(x=pH, y=R, fill=type))+geom_col(position = “fill”)
lin2 p H < − f a c t o r ( l i n 2 pH<-factor(lin2 pH<factor(lin2pH, levels = c(“Very Strong Acidity”, “Strong Acidity”, “Acidity”, “Neutral”)) #更改x轴顺序
ggplot(data=lin2, aes(x=pH, y=R, fill=type))+geom_col(position = “fill”)
ggplot(data=lin2, aes(x=pH, y=R, fill=type))+geom_col(position = “fill”, width = 0.5) #width()是调整柱子宽窄的函数,0.5是窄柱子
p=ggplot(data=lin2, aes(x=pH, y=R, fill=type))+geom_col(position = “fill”, width = 0.5)
p + xlab(“Acidity interval”)+ ylab(“Relative abundance (%)”) #添加x轴、y轴的标签【添加图表标题的函数是ggtitle(“xxx”)
p+labs(fill=“phylum”) #改图例标题
p+scale_fill_discrete(limits=c(“Acidobacteria”,“Actinobacteria”,“Bacteroidetes”,“Chloroflexi”,“Firmicutes”,“Gemmatimonadetes”,“Planctomycetes”,“Proteobacteria”,“Saccharibacteria”,“Verrucomicrobia”,“others”)) #更改图例顺序

  1. 成图

lin2 <- read.delim("~/1/lin2.txt")
View(lin2)
ggplot(data=lin2, aes(x=pH, y=R, fill=type))+geom_col(position = “fill”)
lin2 p H < − f a c t o r ( l i n 2 pH<-factor(lin2 pH<factor(lin2pH, levels = c(“Very Strong Acidity”, “Strong Acidity”, “Acidity”, “Neutral”)) #更改x轴顺序
ggplot(data=lin2, aes(x=pH, y=R, fill=type))+geom_col(position = “fill”)
ggplot2-堆积柱形图????_第2张图片

窄柱子

ggplot(data=lin2, aes(x=pH, y=R, fill=type))+geom_col(position = “fill”, width = 0.5) #width()是调整柱子宽窄的函数,0.5是窄柱子
ggplot2-堆积柱形图????_第3张图片

  1. 坐标轴标签

p=ggplot(data=lin2, aes(x=pH, y=R, fill=type))+geom_col(position = “fill”, width = 0.5)
p + xlab(“Acidity interval”)+ ylab(“Relative abundance (%)”) #添加x轴、y轴的标签【添加图表标题的函数是ggtitle(“xxx”)
ggplot2-堆积柱形图????_第4张图片

  1. 更改图例标题+图例顺序

p+labs(fill=“phylum”) #改图例标题
p+scale_fill_discrete(limits=c(“Acidobacteria”,“Actinobacteria”,“Bacteroidetes”,“Chloroflexi”,“Firmicutes”,“Gemmatimonadetes”,“Planctomycetes”,“Proteobacteria”,“Saccharibacteria”,“Verrucomicrobia”,“others”)) #更改图例顺序
ggplot2-堆积柱形图????_第5张图片

  1. **存在问题:为什么调整了图例顺序之后多了一个灰色???
    +++为什么之前设置的图例标题和y轴标题又没有了???

你可能感兴趣的:(R)