300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > ggplot添加图例

ggplot添加图例

时间:2022-04-25 01:08:43

相关推荐

ggplot添加图例

本文转载来源https://mp./s/KDHkZlRKuh0cnzabJBBYWg

library("ggplot2")library("grid")library("ggsci")# load datadata("mtcars")# plotggplot(mtcars,aes(x = mpg,y = disp)) +geom_line(size = 1,aes(color = mpg)) +scale_color_gradient2(low = 'red',mid = '#CCFF99',high = '#99CCFF',name = '',midpoint = 22.5) +# botomn rectannotation_custom(grob = rectGrob(gp = gpar(col = NA,fill = '#99CCCC')),xmin = 10,xmax = 15,ymin = -25,ymax = 0) +annotation_custom(grob = rectGrob(gp = gpar(col = NA,fill = '#FFCC99')),xmin = 15,xmax = 25,ymin = -25,ymax = 0) +annotation_custom(grob = rectGrob(gp = gpar(col = NA,fill = '#FF6666')),xmin = 25,xmax = 35,ymin = -25,ymax = 0) +# pointannotation_custom(grob = circleGrob(gp = gpar(fill = '#33CC33',col = 'white',lty = 'solid',lwd = 1.5)),xmin = 11.5,xmax = 13.5,ymin = -25,ymax = 0) +annotation_custom(grob = circleGrob(gp = gpar(fill = '#99CCFF',col = 'white',lty = 'solid',lwd = 1.5)),xmin = 19,xmax = 21,ymin = -25,ymax = 0) +annotation_custom(grob = circleGrob(gp = gpar(fill = '#FFFF99',col = 'white',lty = 'solid',lwd = 1.5)),xmin = 29,xmax = 31,ymin = -25,ymax = 0) +# rightannotation_custom(grob = segmentsGrob(gp = gpar(col = "#CC0033",lwd = 2,lineend = "square"),arrow = arrow(angle=20,length = unit(2,'mm'))),xmin = 36,xmax = 36,ymin = 260,ymax = max(mtcars$disp))+annotation_custom(grob = segmentsGrob(gp = gpar(col = "grey50",lwd = 2,lineend = "square"),arrow = arrow(angle=20,ends = 'first',length = unit(2,'mm'))),xmin = 36,xmax = 36,ymin = min(mtcars$disp),ymax = 240) +# leftannotation_custom(grob = rectGrob(gp = gpar(col = 'black',alpha = 0.7,fill = '#0099CC')),xmin = 5.5,xmax = 6.5,ymin = min(mtcars$disp),ymax = 250) +annotation_custom(grob = rectGrob(gp = gpar(col = 'black',alpha = 0.7,fill = '#009933')),xmin = 5.5,xmax = 6.5,ymin = 250,ymax = max(mtcars$disp)) +theme_bw(base_size = 16) +coord_cartesian(clip = "off") +theme(aspect.ratio = 0.6,plot.margin = margin(0,0,0,1,unit = 'cm'),legend.position = 'right') +xlab('') + ylab('') +guides(color = guide_colorbar(barheight = 14))

分类变量

col_df <- data.frame(gene = c('MYC','SOX2','NANOG','ACTB','GAPDH'),exp = rev(seq(0.6,1,0.1)))col_df# gene exp# 1 MYC 1.0# 2 SOX2 0.9# 3 NANOG 0.8# 4 ACTB 0.7# 5 GAPDH 0.6pal_lancet()(8)# [1] "#00468BFF" "#ED0000FF" "#42B540FF" "#0099B4FF" "#925E9FFF" "#FDAF91FF" "#AD002AFF"# [8] "#ADB6B6FF"# ordercol_df$gene <- factor(col_df$gene,levels = col_df$gene)# plotggplot(col_df,aes(x = gene,y = exp,fill = gene)) +geom_col(show.legend = F,fill = '#FF9900',width = 0.6) +# botomn rectannotation_custom(grob = rectGrob(gp = gpar(col = NA,fill = '#00468BFF')),xmin = 0.7,xmax = 1.3,ymin = -0.15,ymax = -0.1) +annotation_custom(grob = rectGrob(gp = gpar(col = NA,fill = '#ED0000FF')),xmin = 1.7,xmax = 2.3,ymin = -0.15,ymax = -0.1) +annotation_custom(grob = rectGrob(gp = gpar(col = NA,fill = '#42B540FF')),xmin = 2.7,xmax = 3.3,ymin = -0.15,ymax = -0.1) +annotation_custom(grob = rectGrob(gp = gpar(col = NA,fill = '#0099B4FF')),xmin = 3.7,xmax = 4.3,ymin = -0.15,ymax = -0.1) +annotation_custom(grob = rectGrob(gp = gpar(col = NA,fill = '#925E9FFF')),xmin = 4.7,xmax = 5.3,ymin = -0.15,ymax = -0.1) +scale_fill_lancet(name = '') +scale_y_continuous(expand = c(0,0)) +theme_classic(base_size = 16) +xlab('') +theme(aspect.ratio = 1,axis.text.x = element_text(color = 'black')) +coord_cartesian(clip = "off")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。