R ggplot2 boxplot从10个文件 [英] R ggplot2 boxplot from 10 files

查看:122
本文介绍了R ggplot2 boxplot从10个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个文件,分别称为 0_X_cell.csv,0_S_cell.csv和15_X_cell.csv,15_S_cell.csv 格式:

  p U:0 U:1 U:2 Tracer Tracer_0 U_0:0 
-34.014 0.15268 -3.7907 -0.20155 10.081 10.032 0.12454
-33.836 0.07349 -2.1457 -0.30531 27.706 27.278 0.076542

我想为 Tracer / 3600 ,并使用 ggplot2 将它们放在同一个图表上,但我发现它并不那么直截了当。任何建议将不胜感激:





I 'm认为它可能是这样的:


  1. 从所有文件导入数据到不同的变量:

  2. 从每一个提取Tracer并放入data.frame中。
  3. 绘制每列Tracer / 3600的箱形图。但每一列都会被称为Tracer ...

正确的程序是什么?

  z< / p> 

; - list.files(pattern =。* cell \\.csv $)
z < - lapply(1:length(z),function(x){chars < - strsplit(z [ x],_);
cbind(data.frame(Tracer = read.csv(z [x])$ Tracer),time = chars [[1]] [1],treatment = chars [ 1]] [2])})
z< - do.call(rbind,z)



然后绘制它:

  library(ggplot2)

ggplot(z,aes(y = Tracer / 3600,x = factor(time)))+ geom_boxplot(aes(fill = factor(treatment)))+ ylab(Tracer)


I have 4 files each called 0_X_cell.csv, 0_S_cell.csv and 15_X_cell.csv, 15_S_cell.csv of the format:

   p    U:0      U:1         U:2    Tracer  Tracer_0    U_0:0
-34.014 0.15268 -3.7907 -0.20155    10.081  10.032      0.12454
-33.836 0.07349 -2.1457 -0.30531    27.706  27.278      0.076542

I'd like to create boxplots out of the values for Tracer/3600 and put them on the same graph using ggplot2 but I'm finding it not quite so straightforward. Any suggestions would be much appreciated:

I'm thinking it might something like this:

  1. Import data from all files into separate variables:
  2. Extract Tracer from each one and put into a data.frame
  3. Plot the boxplots of every column Tracer/3600. But each column will be called Tracer...

What would the correct procedure be?

解决方案

To read in the data from your dir:

z <- list.files(pattern = ".*cell\\.csv$")
z <- lapply(1:length(z), function(x) {chars <- strsplit(z[x], "_");
         cbind(data.frame(Tracer = read.csv(z[x])$Tracer), time = chars[[1]][1], treatment = chars[[1]][2])})
z <- do.call(rbind, z)

Then plot it:

library(ggplot2)

ggplot(z, aes(y = Tracer/3600, x = factor(time))) +geom_boxplot(aes(fill = factor(treatment))) + ylab("Tracer")

这篇关于R ggplot2 boxplot从10个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆