为ggplot - R排列数据帧格式 [英] Arrange dataframe format for ggplot - R

查看:126
本文介绍了为ggplot - R排列数据帧格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从宽格式转换为长格式,以便我可以使用ggplot创建图形。我正在遇到一些问题,以便正确使用数据。到目前为止,我以27个数据框的列表开始我的过程(只显示前10个数据框):

 > str(NDVI_stat)
27
$的列表:'data.frame':10 obs。 2个变量:
.. $ NDVI 1平均值:num [1:10] 0.1796 0.3105 0.1422 0.0937 0.1711 ...
.. $ NDVI 1 sd:num [1:10] 0.1117 0.05845 0.00743 0.02754 0.01506 ...
$:'data.frame':10 obs。 2个变量:
.. $ NDVI 2 mean:num [1:10] 0.0819 0.5954 0.1328 0.0953 0.1492 ...
.. $ NDVI 2 sd:num [1:10] 0.00872 0.10508 0.00863 0.01878 0.02303 ...
$:'data.frame':10 obs。 2个变量:
.. $ NDVI 3 mean:num [1:10] 0.0634 0.681 0.2108 0.0151 0.179 ...
.. $ NDVI 3 sd:num [1:10] 0.0344 0.076 0.0361 0.0638 0.0428 ...
$:'data.frame':10 obs。 2个变量:
.. $ NDVI 4 mean:num [1:10] 0.0971 0.6885 0.2326 0.1157 0.3219 ...
.. $ NDVI 4 sd:num [1:10] 0.00991 0.07509 0.02054 0.02793 0.0303 ...
$:'data.frame':10 obs。 2个变量:
.. $ NDVI 5 mean:num [1:10] 0.0817 0.4825 0.2754 0.1003 0.4155 ...
.. $ NDVI 5 sd:num [1:10] 0.00998 0.05034 0.02781 0.03248 0.04056 ...
$:'data.frame':10 obs。 2个变量:
.. $ NDVI 6 mean:num [1:10] 0.1119 0.7667 0.582 0.0997 0.4426 ...
.. $ NDVI 6 sd:num [1:10] 0.023 0.0672 0.0649 0.0331 0.0557 ...
$:'data.frame':10 obs。 2个变量:
.. $ NDVI 7 mean:num [1:10] 0.1997 0.6567 0.5111 0.0988 0.3307 ...
.. $ NDVI 7 sd:num [1:10] 0.0671 0.0756 0.0435 0.0288 0.0457 ...
$:'data.frame':10 obs。 2个变量:
.. $ NDVI 8 mean:num [1:10] 0.3626 0.7356 0.6304 0.0954 0.335 ...
.. $ NDVI 8 sd:num [1:10] 0.1454 0.0888 0.0502 0.0298 0.038 ...
$:'data.frame':10 obs。 2个变量:
.. $ NDVI 9 mean:num [1:10] 0.541 0.748 0.637 0.089 0.577 ...
.. $ NDVI 9 sd:num [1:10] 0.0968 0.0721 0.0396 0.0276 0.0656 ...
$:'data.frame':10 obs。 2个变量:
.. $ NDVI 10 mean:num [1:10] 0.6691 0.4377 0.6713 0.0942 0.6827 ...
.. $ NDVI 10 sd:num [1:10] 0.088 0.0698 0.033 0.0316 0.0688 ...
$:'data.frame':10 obs。的变量:

我使用 rbindlist data.table包将所有内容合并为一个 dataframe



newdf <-rbindlist(NDVI_stat,use.names = TRUE,fill = TRUE)



代码正常工作,但我没有创建我真正需要的结构。输出结果是 dataframe ,其中270个观察点和54个变量(每个观察点有27个数据帧* 2列)

newdf的图片



正如您在图像 newdf 中看到的那样,它创建了270行,但我想获得的是10行(所以避免了NA值)



对此有何帮助?



这个问题类似于这个
用ggplot2绘制数据框 - R



不同之处在于,我改变了生成输入的方式,并知道如何正确安排数据框以便以后使用。

NDVIdf_forplot< - 然后使用ggplot创建我的图表

然后使用ggplot创建我的图表。



对此有何帮助?

解决方案

我想你问如何列绑定矩阵。据我所知, data.table 没有 cbindlist 函数,所以你可以试试: do.call(cbind,NDVI_stat)虽然不完全相同,但如果每个数据框中没有相同数量的行,则会失败。


I want to reshape my data from wide to long format so that I can use ggplot to create graphs. I am having some problems to properly arragne the data. So far I start my process with a list of 27 dataframes (just showing you the first 10 ones):

> str(NDVI_stat)
List of 27
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 1 mean: num [1:10] 0.1796 0.3105 0.1422 0.0937 0.1711 ...
  ..$ NDVI 1 sd  : num [1:10] 0.1117 0.05845 0.00743 0.02754 0.01506 ...
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 2 mean: num [1:10] 0.0819 0.5954 0.1328 0.0953 0.1492 ...
  ..$ NDVI 2 sd  : num [1:10] 0.00872 0.10508 0.00863 0.01878 0.02303 ...
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 3 mean: num [1:10] 0.0634 0.681 0.2108 0.0151 0.179 ...
  ..$ NDVI 3 sd  : num [1:10] 0.0344 0.076 0.0361 0.0638 0.0428 ...
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 4 mean: num [1:10] 0.0971 0.6885 0.2326 0.1157 0.3219 ...
  ..$ NDVI 4 sd  : num [1:10] 0.00991 0.07509 0.02054 0.02793 0.0303 ...
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 5 mean: num [1:10] 0.0817 0.4825 0.2754 0.1003 0.4155 ...
  ..$ NDVI 5 sd  : num [1:10] 0.00998 0.05034 0.02781 0.03248 0.04056 ...
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 6 mean: num [1:10] 0.1119 0.7667 0.582 0.0997 0.4426 ...
  ..$ NDVI 6 sd  : num [1:10] 0.023 0.0672 0.0649 0.0331 0.0557 ...
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 7 mean: num [1:10] 0.1997 0.6567 0.5111 0.0988 0.3307 ...
  ..$ NDVI 7 sd  : num [1:10] 0.0671 0.0756 0.0435 0.0288 0.0457 ...
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 8 mean: num [1:10] 0.3626 0.7356 0.6304 0.0954 0.335 ...
  ..$ NDVI 8 sd  : num [1:10] 0.1454 0.0888 0.0502 0.0298 0.038 ...
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 9 mean: num [1:10] 0.541 0.748 0.637 0.089 0.577 ...
  ..$ NDVI 9 sd  : num [1:10] 0.0968 0.0721 0.0396 0.0276 0.0656 ...
 $ :'data.frame':   10 obs. of  2 variables:
  ..$ NDVI 10 mean: num [1:10] 0.6691 0.4377 0.6713 0.0942 0.6827 ...
  ..$ NDVI 10 sd  : num [1:10] 0.088 0.0698 0.033 0.0316 0.0688 ...
 $ :'data.frame':   10 obs. of  2 variables:

I am using rbindlist from the data.table package to merge everything into a single dataframe

newdf<-rbindlist(NDVI_stat, use.names = TRUE, fill = TRUE)

The code works properly but I am not creating the structure I really need. The output is a dataframe with 270 (27 daframes * 10 rows in each one) observations and 54 variables (27 dataframes * 2 columns in each one)

image of newdf

As you can see in the image newdf it is creating 270 rows but what I want to obtain is 10 rows (so avoid the NA values)

Any help on that?

This question is similar to this one Plot dataframe with ggplot2 - R

The difference is that I changed the way I produced my input and know I dont know how to arrange the dataframe properly to later use

NDVIdf_forplot <- gather(NDVIdf, key = statistic, value = value, -ID)

and then use ggplot to create my graph

Any help on that?

解决方案

I think you're asking how to column bind the matrices. As far as I'm aware, data.table doesn't have a cbindlist function so you could try: do.call("cbind", NDVI_stat) though that's not quite the same and will fail if you don't have an equal number of rows in each dataframe.

这篇关于为ggplot - R排列数据帧格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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