ggplot左右两个y轴
来源:互联网 发布:展柜成本核算软件 编辑:程序博客网 时间:2024/06/14 05:27
两个ggplot 图合并到一起,一个x轴两个y轴
raw_data <- read_delim('./data/GO_pathway.txt', delim = '\t')head(raw_data)library(dplyr)library(Cairo)double_y_axis <- function(p1, p2){ g1 <- ggplot_gtable(ggplot_build(p1)) g2 <- ggplot_gtable(ggplot_build(p2)) # overlap the panel of 2nd plot on that of 1st plot pp <- c(subset(g1$layout, name == "panel", se = t:r)) g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, pp$l, pp$b, pp$l) # axis tweaks ia <- which(g2$layout$name == "axis-l") ga <- g2$grobs[[ia]] ax <- ga$children[[2]] ax$widths <- rev(ax$widths) ax$grobs <- rev(ax$grobs) ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm") g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1) g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b) # draw it grid.draw(g) return(g)}split_fun <- function(x){ tmp_split <- strsplit(x, '~') do.call('rbind', tmp_split)[,2]}go_data <- raw_data %>% dplyr::filter(grepl('GO',Term) & PValue<0.01) %>% select(c(1,2,3,5)) %>% dplyr::mutate(term = split_fun(Term)) %>% arrange(Category, PValue)go_data$term <- factor(go_data$term, levels = unique(go_data$term))# ggplotlibrary(ggplot2)library(gtable)library(grid)grid.newpage()# two plotsp1 <- ggplot(go_data)+ geom_bar(aes(x = term, y=Count, fill=Category), stat = 'identity') +# geom_line(aes(term, y=PValue, group=1), stat = 'identity', colour="#B00A13") +# geom_point(aes(term, y=PValue, group=1),colour="red", size=3, alpha=0.4)+ theme_bw() + scale_fill_manual(values = c('#6f359d', '#F28C42'), labels=c('Biological Process', 'Cellular Component'))+ theme(axis.text.x = element_text(angle = 40, hjust=1, vjust=1), axis.ticks.x = element_blank(), legend.position='left' # axis.title.y=element_text(vjust=8) )+ scale_y_continuous(expand = c(0,0), limits=c(0, 30))+ xlab('Go Items')+ ylab('The number of genes') # coord_fixed(ratio = 0.6)# 控制长宽比p1 p1 <- p1+coord_fixed(ratio = 0.6)# 控制长宽比p2 <- ggplot(go_data, aes(term, PValue, group=1)) + geom_line( stat = 'identity', colour="#B00A13") + geom_point(colour="red", size=3, alpha=0.4)+ scale_y_continuous(limits = c(0, 0.02))+ # scale_y_reverse()+ scale_x_discrete(breaks=NULL)+ theme(axis.text.x = element_text(), axis.ticks.x= element_blank(), # panel.background = element_rect(fill = NA), # panel.grid = element_blank(), legend.position='top')+ ylab('')+ xlab('')p2 # extract gtableg <- double_y_axis(p1,p2)tiff("Plot600.tiff", type="cairo", width = 14, height = 8, units = 'in', res = 300)grid.draw(g)dev.off()# 在右下角的框里调试好图片, 然后采用# par('din') 获取宽高CairoPNG('plotCairo.png', width = 13.58, height = 8.2, units='in', dpi=700)grid.draw(g)dev.off()
0 0
- ggplot左右两个y轴
- EXCEL 建立两个Y轴
- chart控件两个Y轴
- ggplot
- 用python建立两个Y轴的XY曲线图
- 2017.07.03回顾 matplotlib作两个y轴图形
- Asp.net DIV Y轴滚动条发生左右抖动/自动增加了空白块解决
- 基础代码--通过鼠标的左右滑动来实现摄像机在Y轴上视野旋转
- 左右两个div高度自适应
- origin作统计图(两个x正轴,一个y轴效果)
- echarts折线图有两个图例时如何实现分别采用两个不同单位的y轴
- 两个listbox选定左右增加选项
- js 两个select之间的左右移动
- 两个div左右对齐的问题
- ViewPager实现左右两个屏幕的切换
- android 实现左右两个屏幕的切换
- 同时更新框架的左右两个页面
- ViewPager-实现左右两个屏幕的切换
- LeetCode129 Sum Root to Leaf Numbers
- android数据库操作,部署LitePal
- iOS开发笔记--如何去掉UItableview header(footer)view黏性(sticky)
- 蓝桥杯 历届试题 翻硬币(贪心)
- 线性回归(Linear Regression)
- ggplot左右两个y轴
- C语言实现迷宫求解问题(详细思路+附源代码)
- python--字典
- Struts、Spring和Hibernate常见问题总结
- JVM-JAVA对象的访问
- DOM类和SAX类的区别
- 使用Android ViewPager与PhotoView实现图片滑动查看
- RaspberryPi: Proxy Server (Goagent-->XX-Net)
- 单一职责原则