匠人府分享 http://blog.sciencenet.cn/u/meiweipingg

博文

x~y1+y2+...的情况下,ggplot2作柱状图及误差棒

已有 6004 次阅读 2016-11-17 19:33 |个人分类:R语言|系统分类:科研笔记

http://www.sthda.com/english/wiki/ggplot2-error-bars-quick-start-guide-r-software-and-data-visualization


效果图:



# 操作实例-----------------------


dat1 <- read.csv("~/Tudat.csv")

View(dat1)

attach(dat1)


library(ggplot2)  # install.packages("ggplot2")


p<-ggplot(data=dat1,aes(x=Gene,y=Ct,fill=grp))

p

# Keep only upper error bars

p+geom_bar(stat="identity", color="black", width = 0.6,position=position_dodge()) +

 geom_errorbar(aes(ymin=Ct,ymax=Ct+SD), width=0.2, position=position_dodge(0.6)) ->q

q


# geom_errorbar(width=)表示误差棒的相对宽度

# geom_errorbar(position=position_dodge(0.6))中的0.6 需与

# geom_bar(width=)的取值一样,否则误差棒会错位

## Finished bar plot


# 更改barplot到x轴的距离为0

q+scale_y_continuous(expand = c(0, 0), limits = c(0,150),breaks = seq(0, 150,by=30))  


# 改变legend的位置,box,title etc, 数值为相对值,从0到1

last_plot()+ theme(legend.position=c(0.1,0.9),legend.title = element_blank(),legend.background = element_rect(fill=NULL,size = 0.5,linetype = "solid",colour = "black"),panel.background = element_rect(fill = "white",colour="black"),panel.grid.major = element_blank(),panel.grid.minor = element_blank())


# 添加 xy轴labels和标题,添加文本,更改 标题位置为居中

last_plot()+ggtitle("Try mutil-y errorbar")+labs( x = "Gene(ser)",y='Ct(***)')+geom_text(aes(x="G7",y=140,label="(a)"))+theme(plot.title = element_text(hjust = 0.5))


# save tiff 300dpi

# 可能图片会乱码。最好直接保存Metafile to ppt

      # 图形ok了


#以下附属内容,仅供参考

ggsave(filename = "Ct-Gene.test.tiff", width = 4,height = 4,dpi = 300)


#### If need, 将需要断y轴的High数据组,以分面facet_grid的形式代替。

last_plot()+facet_grid(~Mark,scale="free",space = "free")


# If need, 绘制x y轴线

last_plot()+theme(axis.line = element_line(color="black", size = 0.5))






https://blog.sciencenet.cn/blog-651374-1015354.html

上一篇:非线性回归(迭代法)及其两种拟合曲线:y=a+b*exp(c*x)
下一篇:用数据说话系列(4): 各种 t 检验 样本数 至少每组多少为宜
收藏 IP: 133.45.210.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-20 05:20

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部