格式如下:
1.PNG代码如下:
ggplot(data,aes(x=size,y=num))
#画条形图
+geom_bar(stat="identity",width=0.2)
#identity表示把y当做值去输入。如果是bin,就会计算y出现的頻数。width调整直方图的宽度。
+labs(x="size",y="number")
#设置坐标轴的名称
+ggtitle("MspJl_chr1")
#设置图的名称
+theme(axis.line=element_line(size=0.5,color="black"))
#加坐标轴刻度线
+geom_text(aes(label=paste(num)),vjust=-0.8)
#给直方图中增加图注或者数据,vjust可以调整离柱子的距离,其值越小,离柱子越远
+scale_x_discrete(limits=c("1","2","3","4","5","6_13","13_20","20_35","35_55","55_75","75_100","100_200","200_300","300_400","400_600","600_1000",">=1000"))
#修改坐标轴上项目的顺序
+theme(plot.title = element_text(hjust = 0.5))
#标题居中
这里面有个问题,最开始的时候我没有加scale_x_discrete(limits=c("1","2","3","4","5","6_13","13_20","20_35","35_55","55_75","75_100","100_200","200_300","300_400","400_600","600_1000",">=1000"))
另外,一开始的条形图的标题也不是居中,而是在左上角,百度了一下有说在ggtitle()中加个hjust=0.5,但是我试了一下还是不行,后来发现是R版本的问题,我用的R是3.4.1的,还好后来找到另外一种那就是我加在最后的一条语句。
网友评论