R绘图应用实例:数据录入、转换及绘图

上一篇文章[1]讲解了数据的导入格式,以及使用reshape2和tidyr包进行数据转换,本文主要利用上篇数据,进行直观的操作演示并绘制图形。,

1. 宽数据转为长数据

rm(list = ls())
# 读入宽格式数据
wide_line <- read.table(file = "C:/Users/Administrator/Desktop/wide_line.txt", 
                        header = T, sep = "")
head(wide_line)
  id sample date  x1  x2  x3  x4
1  1      A    1 190 220 182 199
2  2      A    2 156 178 169 142
3  3      A    3 102 110 130 100
4  4      A    4  69  78  56  88
5  5      B    1 220 210 199 160
6  6      B    2 156 142 163 152
##################################宽数据转换####################################
# tidyr package
library(tidyr)
wide_line$date <- as.factor(wide_line$date)
wide_line_long <- gather(data = wide_line, key = condition,
                    value = measurement, x1:x4, factor_key = TRUE)
head(wide_line_long)
  id sample date condition measurement
1  1      A    1        x1         190
2  2      A    2        x1         156
3  3      A    3        x1         102
4  4      A    4        x1          69
5  5      B    1        x1         220
6  6      B    2        x1         156
# reshape2  package
library(reshape2)
wide_line$date <- as.factor(wide_line$date)
wide_line_long <- melt(data = wide_line, id.vars = c("id","sample","date"), 
                  measure.vars= c("x1","x2","x3","x4"))
head(wide_line_long)

2. 长数据转为宽数据

# 读入长格式数据
long_line <- read.table(file = "C:/Users/Administrator/Desktop/long_line2.txt", 
                        header = T, sep = "")
head(long_line)
  id sample date value
1  1      A    1   190
2  2      A    1   220
3  3      A    1   182
4  4      A    1   199
5  5      A    2   156
6  6      A    2   178
##################################长数据转换####################################
# tidyr package
library(tidyr)
long_line$date <- as.factor(long_line$date)
long_line_wide <- spread(data = long_line, key = date, value = value)
head(long_line_wide)
  id sample   1   2  3  4
1  1      A 190  NA NA NA
2  2      A 220  NA NA NA
3  3      A 182  NA NA NA
4  4      A 199  NA NA NA
5  5      A  NA 156 NA NA
6  6      A  NA 178 NA NA
# reshape2  package
library(reshape2)
long_line$date <- as.factor(long_line$date)
long_line_wide <- dcast(data = long_line, formula = id + sample ~ date, 
                   value.var="value")
head(long_line_wide)

3. 统计分析

library(Rmisc)
long_line_count <- summarySE(long_line, measurevar = "value", 
                             groupvars = c("sample","date"))
head(long_line_count)
  sample date N  value        sd        se       ci
1      A    1 4 197.75 16.378339  8.189170 26.06159
2      A    2 4 161.25 15.692355  7.846177 24.97004
3      A    3 4 110.50 13.699148  6.849574 21.79840
4      A    4 4  72.75 13.598407  6.799203 21.63810
5      B    1 4 197.25 26.272609 13.136305 41.80558
6      B    2 4 153.25  8.770215  4.385107 13.95537

4. 绘图

library(ggplot2)
p1 <- ggplot(long_line_count, aes(x=date, y=value, group = sample, fill = sample)) +
  geom_bar(stat = "identity",color = "black", size = 0.3,position = position_dodge()) +
  geom_errorbar(aes(ymin = value - sd, ymax = value + sd), position = position_dodge(0.9),
                width = 0.2)


p2 <- ggplot(long_line_count, aes(x=date, y=value, group = sample, color = sample)) +
  geom_line(position = position_dodge(0.2)) +
  geom_point(position = position_dodge(0.2)) +
  geom_errorbar(aes(ymin = value - se, ymax = value + se), width = 0.2,
                position = position_dodge(0.2)) +
  theme_minimal()
library(ggpubr)
ggarrange(p1, p2, labels = c("A","B"))
Rplot01.png

  1. 数据录入与格式转换(reshape2、tidyr包)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容