1.用Excel准备数据并存为CSV格式,数据格式与内容为:

读取数据:
e<-read.csv(file.choose())
e

2.开始作图:
library(ggplot2)
ep<-ggplot(e,aes(x=Pairwise.connections,y=Percentage.change.from.monoculture,fill=pos))+geom_bar(stat="identity")
ep

然后对图例的文字,位置,坐标轴等进行修改:
ep<-ep+theme(legend.position="bottom")#图例调整到最低部分
ep

去除legend title:
ep<-ep+theme(legend.title = element_blank())
ep

对坐标轴标签进行角度变化:
ep<-ep+theme(axis.text.x = element_text(angle = 90, hjust = 1))
ep
得到:

调整横坐标名字的格式:
ep<-ep + theme(axis.title.x = element_text(size = 15, color = "black", face = "bold", vjust = 0.5, hjust = 0.5))
ep
得到:

调整纵坐标的名称格式:
ep<-ep + theme(axis.title.y = element_text(size = 15, color = "black", face = "bold", vjust = 0.5, hjust = 0.5))
ep
得到:

调整横坐标标签的间隔:
ep<-ep + scale_x_continuous(breaks=seq(0, 14, 1))
ep
得到

给纵轴标签间隔进行调整:
ep<-ep + scale_y_continuous(breaks=seq(-40, 150,20 ))
ep
得到:

删除垂直网格线:
ep<-ep + theme(panel.grid.major.x = element_blank() ,panel.grid.minor.x = element_blank())
ep
得到:

添加图片名称:
ep<-ep + labs(title="Synergistic versus non-synergistic
interactions")
ep
总的代码:
library(ggplot2)#加载包
e<-read.csv(file.choose())#读取数据
head(e)
#作图
ep<-ggplot(e,aes(x=Pairwise.connections,y=Percentage.change.from.monoculture,fill=pos))+geom_bar(stat="identity")
ep<-ep+theme(legend.position="bottom")#图例调整到最低部分
ep<-ep+theme(legend.title = element_blank())
ep<-ep+theme(axis.text.x = element_text(angle = 90, hjust = 1))
ep<-ep + theme(axis.title.x = element_text(size = 15, color = "black", face = "bold", vjust = 0.5, hjust = 0.5))
ep<-ep + theme(axis.title.y = element_text(size = 15, color = "black", face = "bold", vjust = 0.5, hjust = 0.5))
ep<-ep + scale_y_continuous(breaks=seq(-100, 150,50 ))
ep<-ep + scale_x_continuous(breaks=seq(0, 14, 1))
ep<-ep + labs(title="Synergistic versus non-synergistic interactions")
ep<-ep + theme(panel.grid.major.x = element_blank() ,panel.grid.minor.x = element_blank())
ep<-ep+theme(panel.grid.major.y = element_line(colour = "black") ,panel.grid.minor.y = element_blank())
ep

网友评论