美文网首页
R基础画图

R基础画图

作者: 嘉期几许 | 来源:发表于2020-03-22 11:31 被阅读0次

柱状图


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

ggplot(data=dd,aes(group,gene))+
  stat_summary(geom = "bar",
               fun=mean,
               aes(fill=group))+
  stat_summary(geom="errorbar",
               fun.min = min,
               fun.max = max,
               width=0.2)+
  geom_signif(comparisons = list(c("early","mid"),
                                # c("B","D"),
                                 c("mid","late")),
              test = 't.test',
              y_position = c(10,17,18),
              map_signif_level = T)+
  scale_y_continuous(expand = c(0,0),limits = c(0,5))+
  theme_bw()+
  scale_fill_manual(values = c("steelblue", 
                               "yellowgreen", 
                               "violetred1"))+
  theme(legend.title = element_blank())+
  labs(x="group",
       y="expression",
       title = "LAMA4")+
  theme(panel.grid = element_blank())
  1. 山脊图


    image.png
#山脊图
##产生数据
set.seed(2019)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]


ggplot(dsamp,aes(carat,fill = clarity))+
  geom_density(alpha = .5,color = NA)+
  scale_fill_brewer(palette = 'Set2')+
  theme_classic()+
  theme(legend.position = c(.85,.65))


install.packages('ggridges')
library(ggridges)

ggplot(dsamp,aes(x = carat,y = clarity, fill = clarity))+
  geom_density_ridges(alpha = .5)+
  scale_fill_brewer(palette = 'Set3')+
  theme_classic()
  1. 折线图+散点
set.seed(2019)
x <- 1:8
dat <- tibble(x = rep(x,2),
              y = 1.2*x+5+rnorm(16,0,2),
              group = rep(c('Group1','Group2'),each = 8))

p1 <- ggplot(dat,aes(x, y, color = group))+
  geom_line(size = .8)+
  scale_color_d3()+
  theme_classic()+
  theme(legend.position = c(.85,.15))

##log转换y
p2 <- ggplot(dat,aes(x, y, color = group))+
  geom_line(size = .8)+
  geom_point(shape = 21,color = 'black',size=3,fill = 'white')+
  scale_color_d3()+
  theme_classic()+
  theme(legend.position = c(.85,.15))

plot_grid(p1,p2,ncol=2,labels = LETTERS[1:2],align = c('v','h'))
  1. 配对坡度图


    image.png
ggplot(data = data, aes(x = Date, y = Pct, 
                        group = Party,color = Party)) +
  geom_line(size = 2) +
  geom_point(size = 4) +
  annotate('text',x = 2.1,y = c(37.9, 33.3, 27.3, 5, 8.4),
           label = c("Green", "Liberal", "NDP", "Others", "PC"))+
  scale_x_discrete(position = "top") +
  scale_color_brewer(palette = 'Paired')+
  theme_bw() +
  theme(legend.position = 'none') +
  theme(panel.border = element_blank())
  1. 散点图加曲线
#平滑曲线
set.seed(2019)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]

p1 <- ggplot(dsamp,aes(carat, price, fill = cut))+
  geom_point(shape = 21, color = 'black', size = 3)+
  geom_smooth(se = .8)+
  theme(legend.position = c(.1,.8))

##巧用数据叠加
p2 <- ggplot()+
  geom_point(data = dsamp,aes(carat, price, fill = cut),
             shape = 21, color = 'black', size = 3)+
  geom_smooth(data = dsamp,aes(carat, price),se = .8)+
  theme(legend.position = c(.1,.8))

plot_grid(p1,p2,ncol=2,labels = LETTERS[1:2],align = c('v','h'))


#ggplot2内置了很多平滑方法

p <- ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point(size = 3,shape = 21,fill = '#5dc863',color = 'black')

p1 <- p + geom_smooth(method = "lm", formula = y ~ x, size = 1)+
  ggtitle(label = 'liner model')
##x的平方
p2 <- p + geom_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1)+
  ggtitle(label = 'polynomial regression')
##广义加性模型
p3 <- p + geom_smooth(method = "gam", formula = y ~ x, size = 1)+
  ggtitle(label = 'GAM model')

p4 <- p + geom_smooth(method = "gam", formula = y ~ s(x), size = 1)+
  ggtitle(label = 'GAM model with spline')

plot_grid(p1,p2,p3,p4,ncol=2,labels = LETTERS[1:4],align = c('v','h'))
  1. 生存曲线
library(survminer)
library(survival)
###按照sex进行分类,拟合分组信息
fit <- survfit(Surv(time, status) ~ sex, data = lung)
p1 <- ggsurvplot(fit, data = lung,
                 legend.title = 'Sex',
                 legend = c(.85,.8))

p2 <- ggsurvplot(
  fit, 
  data = lung, 
  size = 1,                
  palette = c("#E7B800", "#2E9FDF"),
  conf.int = TRUE,          
  pval = TRUE,              
  risk.table = TRUE,        
  risk.table.col = "strata",
  legend.labs = 
    c("Male", "Female"),    
  risk.table.height = 0.25, 
  ggtheme = theme_bw(),
  legend.title = 'Sex',
  legend = c(.85,.8)
)
  1. 和弦图


    image.png
library(circlize)

set.seed(2019)

numbers <- sample(c(1:1000), 100, replace = T)
data <- matrix( numbers, ncol=5)
rownames(data) <- paste0("Set-", seq(1,20))
colnames(data) <- paste0("Pair-", seq(1,5))

chordDiagram(data, transparency = 0.5)
  1. 饼图


    image.png
##排序后
injuries2 <- injuries %>% mutate(
  type = factor(type, levels = type[order(counts)])) 
p2 <- ggplot(injuries2,aes('', y = counts, fill = type))+
  geom_bar(width = 1, size = 1, color = 'white',stat = 'identity')+
  coord_polar(theta = 'y')+
  geom_text(aes(label = paste0(round(share,1),'%')),
            position = position_stack(vjust = .5))+
  labs(x = NULL, y = NULL, fill = NULL, 
       title = "Injury proportion in young adults")+
  scale_fill_npg()+
  theme_classic() +
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        plot.title = element_text(hjust = 0.5, color = "#666666"))+
  guides(fill = guide_legend(reverse = TRUE))
  1. 玫瑰图
#画一朵玫瑰
ggplot(injuries,aes(x = type, y = counts, fill = type))+
  geom_bar(width = 1,color = 'black',stat = 'identity')+
  coord_polar(theta = 'x')+
  geom_text(aes(label = paste0(round(share,1),'%'),
                y = counts+2))+
  labs(x = NULL, y = NULL, fill = NULL, 
       title = "Injury proportion in young adults")+
  scale_fill_tron()+
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5, color = "#666666"),
        panel.border = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        legend.position = 'none')
  1. 玫瑰图内有分类
#多层玫瑰图
injuries <- tibble(age = rep(c('Young adults',
                               'Middle-aged people',
                               'the elders'),each=5),
                   type = rep(c('Road injury','Self-harm','CVD',
                                'Cancers','Infectious Diseases'),3),
                   counts = c(214,123,69,24,17,
                              129,110,201,101,45,
                              56,32,212,189,78))
injuries <- injuries %>% 
  mutate(age = factor(age, levels = c('Young adults',
                                      'Middle-aged people',
                                      'the elders')))
injuries <- injuries %>%
  mutate(type = factor(type,levels = c('CVD','Road injury','Cancers',
                                       'Self-harm','Infectious Diseases')))
ggplot(injuries,aes(x = type, y = counts, fill = age))+
  geom_bar(width = 1,color = 'black',stat = 'identity',size = .2)+
  coord_polar(theta = 'x')+
  labs(x = NULL, y = NULL, fill = NULL, 
       title = "Injury proportion in young adults")+
  scale_fill_brewer(palette = 'Blues')+
  theme(plot.title = element_text(hjust = 0.5, color = "#666666"),
        panel.border = element_blank(),
        panel.background = element_blank())
  1. 柱状图有组成成分


    image.png
#条柱的另外两种排列方式
p1 <- ggplot(injuries,aes(type,weight = counts,fill = age))+
  geom_hline(yintercept = seq(100,400,100),color = 'gray')+
  geom_bar(color = 'black',width = .7,position = 'stack')+
  scale_fill_brewer(palette = 'Accent')+
  scale_y_continuous(expand = c(0,0))+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45,hjust=1))
  1. 上下分明的条图


    image.png
set.seed(2019)
df <- tibble(diseases = c('Heart attack','Stroke','CHD',
                          'Arrhythmia','Heart failure',
                          'Hepatitis','Malaria','HIV',
                          'TB','Influenza','HCC','CRC',
                          'Lung','Gastric','Breast',
                          'Diabetes','Hypertention',
                          'Mucolipidoses','IBD','Celiac Disease',
                          "Crohn's Disease",'Diarrhea',
                          'Autism','ADHD','Depression','Meningitis',
                          'Migraine','GBM'),
             trends = rnorm(28,0,2))
df <- df %>% mutate(
  diseases = factor(diseases,levels = rev(diseases[order(trends)])))

ggplot(df,aes(diseases,weight = trends))+
  geom_hline(yintercept = seq(-4,3,1),color = 'gray')+
  geom_bar(color = 'black',width = .6,fill = '#5bd1d7',size = .3)+
  scale_y_continuous(limits = c(-5,3.5))+
  theme_classic()+
  ylab('Temporal trends of diseases')+
  theme(axis.text.x = element_text(angle = 45,hjust=1))
  1. 好看的箱子图
ggbetweenstats(
  data = iris,
  plot.type = 'box',
  x = Species,
  y = Sepal.Length,
  mean.plotting = F,
  messages = T,
  type = 'p',
  palette = 'Set1') + 
  scale_y_continuous(breaks = seq(3, 8, by = 1))+
  theme_classic()+
  theme(legend.position = c(.13,.8))

带组间比较

#添加统计信息标识符
ggbetweenstats(
  data = iris,
  plot.type = 'box',
  x = Species,
  y = Sepal.Length,
  pairwise.comparisons = T,
  mean.plotting = F,
  messages = T,
  type = 'p',
  palette = 'Set1') + 
  scale_y_continuous(breaks = seq(3, 8, by = 1))+
  theme_classic()+
  theme(legend.position = c(.11,.8))
  1. seqlogo

相关文章

  • R基础画图

    山脊图image.png 折线图+散点 配对坡度图image.png 散点图加曲线 生存曲线 和弦图image.p...

  • R003 基础画图

    1. 图形创建及保存 1.1 图形创建 1.2 创建新的图形窗口 在通过执行如plot()、hist()(绘制直方...

  • R语言画图基础功能

    R语言有着很强大的画图功能。我们可以从下面的语句中得到 1、绘画函数 高级画图功能(创建一个新的图形) 低级绘图函...

  • R004 基础画图速查代码

    基础画图代码 参考 RobertI.Kabacoff, 卡巴科弗, 陈钢,等. R语言实战[J]. 2013.

  • R语言基础绘图函数散点图~跟着Nature Communicat

    今天继续 跟着Nature Communications学画图系列第二篇。学习R语言基础绘图函数画散点图。 对应的...

  • R考核的内容:

    我们经过了四周的分享,把R的整理数据,基础画图,ggplot2 画图以及从分析数据到报告的过程进行了了解。我们现在...

  • R相关命令

    R字符处理相关 R画图相关 文件输出

  • ggplot箱线图之如何添加最大值最小值线(whisker-en

    ggplot作为R语言画图的瑞士军刀,相比于基础的R包,语法更加易于理解和掌握,不需要掌握很多的命令就能画出整洁美...

  • 绘图

    Qt--基础图形绘制 QT基础(三)----QPainter画图功能 & 画图小项目

  • 跟着Nature Communications学画图~Figur

    今天开始 跟着Nature Communications学画图 系列第一篇。学习用R语言的基础绘图函数画箱线图。 ...

网友评论

      本文标题:R基础画图

      本文链接:https://www.haomeiwen.com/subject/qectyhtx.html