在ggplot2中显示多个数据帧 [英] Displaying multiple data frames in ggplot2

查看:179
本文介绍了在ggplot2中显示多个数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一行图中绘制多个数据框,使用 x = index y = values 。我使用的8个data.frames以这种格式(索引和值)进行处理,长达数百行:

 
2306 0.000000
2307 1.004711

因为数据框并不全都有相同的大小,我也试图通过将数据集转换为百分比(索引/总数值)* 100来调整数据集的大小,我应该将它放在绘图代码中还是应该在绘图之前更好地转换数据集?



希望StackOverflow可以帮助R新手解决方案

如果你想它们都在一个单独的图中,如果您首先堆叠数据框并包含标识数据来自哪个原始数据帧的列,那将是最容易的。

  library(dplyr)
library(ggplot2)

首先创建假数据。下面的代码创建一个包含八个数据帧的列表。我们假设这是我们读完数据后开始的地方。如果您正在从单独的文件(例如csv文件)中读取数据框,请将它们全部读入一个列表,然后使用 bind_rows 来堆叠它们:

 #假数据
set.seed(954)
df = lapply(paste0(d, 0:7),函数(x){
n = sample(seq(100,500,100),1)
data.frame(source = x,index = 1:n,values = cumsum(rnorm(n) ))
$)

#将八个数据帧叠加到一个数据框中
df = bind_rows(df)

使用ggplot绘图。我们使用 source (原始数据框的名称)作为 color 审美性:

  ggplot(df,aes(index,values,color = source))+ 
geom_line()+
theme_bw()



或者,如果要规范化 index 以跨越每个数据帧的相同范围:

  ggplot(df%>%group_by(source)%>%
mutate(index = index / max(index)),
aes(index,values, color = source))+
geom_line()+
theme_bw()



更新:回应你的评论,如果你hav e数据框已经存在,你可以这样做来获得一个单一的数据框:

  df = lapply(paste0(sign ,1:8),函数(x){
data.frame(source = x,get(x))
})

df = bind_rows(df)

但是您必须在某些时候将数据读入R,并且您可以在处理此类处理时您将数据文件读入R.


I am trying to layer multiple data frames in one line plot, with x = index, y = values. The 8 data.frames I work with come in this format (index and value) and are several hundred rows long:

      Values
2306  0.000000
2307  1.004711

Because the data frames don't all have the same size, I am also trying to resize the data sets by converting them into percent (index/total number of values)*100, should I place this in the plotting code or should I better convert the data sets before plotting?

Hope the hivemind of StackOverflow can help an R newbie

解决方案

If you want them all in a single plot, it would be easiest if you "stack" the data frames first and include a column that identifies which original data frame the data came from.

library(dplyr)
library(ggplot2)

First create fake data. The code below creates a list containing eight data frames. We'll assume this is where we start after we've read in the data. If you're reading in your data frames from separate files (csv files, for example), just read them all into a single list and then use bind_rows to stack them:

# Fake data
set.seed(954)
df = lapply(paste0("d",0:7), function(x) {
  n=sample(seq(100,500,100),1)
  data.frame(source=x, index=1:n, values=cumsum(rnorm(n)))
})

# Stack the eight data frames into a single data frame
df = bind_rows(df)

Plot using ggplot. We use source (the name of the original data frame) as the colour aesthetic:

ggplot(df, aes(index, values, colour=source)) +
  geom_line() +
  theme_bw()

Or, if you want to normalize index to span the same range for each data frame:

ggplot(df %>% group_by(source) %>%
         mutate(index = index/max(index)), 
       aes(index, values, colour=source)) +
  geom_line() +
  theme_bw()

UPDATE: In response to your comment, if you have the data frame already, you could do this to get a single data frame:

df=lapply(paste0("sign",1:8), function(x) {
  data.frame(source=x, get(x))
})

df=bind_rows(df)

But you must have read the data into R at some point and you can take care of this type of processing when you read the data files into R.

这篇关于在ggplot2中显示多个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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