R柱状图绘制1

#柱状图绘制

sheep=read.csv("TableS1-3.csv", sep=",", header=T, row.names=1)

View(sheep)

objects(sheep)

sheep$name=row.names(sheep)

ggplot(sheep,aes(x=name,y=NROH))+

  geom_bar(stat = "identity",fill="red",col="blue",width=0.5)

#根据区域填充颜色

ggplot(sheep,aes(x=name,y=NROH,fill=Continent))+

  geom_bar(stat = "identity")+theme(axis.text.x = element_text(angle = 90, hjust = 1))

View(sheep)

#柱状图绘制

sheep8=read.csv("TableS1-8.csv", sep=",", header=T, row.names=1)

View(sheep8)

objects(sheep8)

sheep8$Breeds=row.names(sheep8)

sheep8$code=row.names(sheep8)

ggplot(sheep8,aes(x=name,y=NROH))+

  geom_bar(stat = "identity",fill="red",col="blue",width=0.5)

#根据区域填充颜色

ggplot(sheep8,aes(x=breed,y=NROH,fill=Continent,))+

  geom_bar(stat = "identity")+theme(axis.text.x = element_text(angle = 90, hjust = 1))

#改变柱形图的顺序

install.packages("ggthemes")

library(ggthemes)

p1=ggplot(sheep8,aes(reorder(x=Breeds,N),y=NROH,fill=Continent))+

  geom_bar(stat = "identity",width=0.5)+theme_bw()+theme(panel.grid=element_blank())+

  theme(axis.text.x=element_text(angle = 90, hjust = 1))+

  scale_y_continuous(expand = c(0,0),limits=c(0,1500))+

  theme(axis.text.x = element_blank())

p2=ggplot(sheep8,aes(reorder(x=Breeds,N),y=FROH,fill=Continent))+

  geom_bar(stat = "identity",width=0.5)+theme_bw()+theme(panel.grid=element_blank())+

  theme(axis.text.x=element_text(angle = 90, hjust = 1))+

  scale_y_continuous(expand = c(0,0),limits=c(0,0.25))+

  theme(axis.text.x = element_blank())+labs(x=NULL)

#ggplot堆积柱形图(continent)

sheep4=read.csv("TableS1-7.csv", sep=",", header=T, row.names=1)

View(sheep7)

objects(sheep7)

sheep7$continet=row.names(sheep7)

sheep9=melt(sheep7,id="continet")

View(sheep9)

objects(sheep9)

p3=ggplot(sheep9,aes(x=continet,y=value,fill=variable))+

  geom_bar(stat = "identity",width=0.5)+theme_bw()+

  theme(panel.grid=element_blank())+

  scale_y_continuous(expand = c(0,0))+

  theme(axis.text.x = element_blank())

#ggplot堆积柱形图(breeds)

sheep6=read.csv("TableS1-6.csv", sep=",", header=T, row.names=1)

View(sheep6)

objects(sheep6)

sheep6$breed=row.names(sheep6)

sheep10=melt(sheep6,id="breed")

View(sheep10)

objects(sheep10)

p4=ggplot(sheep10,aes(x=breed,y=value,fill=variable))+

  geom_bar(stat = "identity",width=0.5)+theme_bw()+

  theme(panel.grid=element_blank())+

  theme(axis.text.x=element_text(angle = 90, hjust = 1))+

  scale_y_continuous(expand = c(0,0))

sheep6=read.csv("TableS1-6.csv", sep=",", header=T, row.names=1)

View(sheep6)

objects(sheep6)

sheep6$breed=row.names(sheep6)

sheep10=melt(sheep6,id="N")

View(sheep10)

objects(sheep10)

p4=ggplot(sheep10,aes(x=N,y=value,fill=variable))+

  geom_bar(stat = "identity",width=0.5)+theme_bw()+

  theme(panel.grid=element_blank())+

  theme(axis.text.x=element_text(angle = 90, hjust = 1))+

  scale_y_continuous(expand = c(0,0))+

  theme(axis.text.x = element_blank())+labs(x=NULL)

library(patchwork)

p4/p2/p1

p1=ggplot(sheep8,aes(reorder(x=Breeds,N),y=NROH,fill=Continent))+

  geom_bar(stat = "identity",width=0.5)+theme_bw()+

  theme(panel.grid=element_blank())+

  theme(axis.text.x=element_text(angle = 90, hjust = 1))+

  scale_y_continuous(expand = c(0,0),limits=c(0,1500))+labs(x=NULL)


p2=ggplot(sheep8,aes(reorder(x=Breeds,N),y=FROH,fill=Continent))+

  geom_bar(stat = "identity",width=0.5)+

  theme_bw()+theme(panel.grid=element_blank())+

  theme(axis.text.x=element_text(angle = 90, hjust = 1))+

  scale_y_continuous(expand = c(0,0),limits=c(0,0.25))+

  theme(axis.text.x = element_blank())+labs(x=NULL)

p4=ggplot(sheep10,aes(x=N,y=value,fill=variable))+

  geom_bar(stat = "identity",width=0.5)+theme_bw()+

  theme(panel.grid=element_blank())+

  theme(axis.text.x=element_text(angle = 90, hjust = 1))+

  scale_y_continuous(expand =c(0,0))+

  theme(axis.text.x = element_blank())+labs(x=NULL)

你可能感兴趣的:(R柱状图绘制1)