【问题标题】:loop through data frame to plot all possible cases循环遍历数据框以绘制所有可能的情况
【发布时间】:2016-04-17 14:35:25
【问题描述】:

我有一个数据集(mydata),其中一部分如下所示:

'data.frame':  36190 obs. of 16 variables: 

$ RE                    : int  38 41 11 67 30 18 38 41 41 30 ... 

$ LU                    : int  4200 3330 530 4500 3000 1790 4700 3400   

$ COUNTRY               : Factor w/ 4 levels "DE","FR","JP", "FR"… 

$Light                  : Factor w/2 levels  "ON","OFF","ON", …. 

$OR                     : Factor w/2 levels  "S","T","S",…. 

$PAT                    : Factor w/3 levels  "low", "high", "middle",…. 

现在我想针对所有可能的情况绘制 RE 与 LU,例如,一张图用于 (COUNTRY= FR, Light=off, OR=S, PAT=low) 和一张图用于 (COUNTRY= FR, Light=on , OR=S, PAT=high) 一个代表(COUNTRY= FR, Light=off, OR=S, PAT=high)等等。我已经创建了一个如下所示的数据框,但我不知道如何遍历该数据框并使用 ggplot2 生成所有图

mydata <- data.frame( RE = sample( 5:50, 100, TRUE), LU = sample(1500:4500, 100 ), COUNTRY = factor( sample( c( "DE","FR","JP","AU"), 100, TRUE)),Light=factor( sample( c( "ON", "OFF" ), 100, TRUE)), OR = factor( sample( c( "S", "T" ), 100, TRUE)), PAT = factor( sample( c( "low", "high", "middle" ),100,TRUE)))

感谢您的帮助。

【问题讨论】:

  • 你真的想要 4*2*2*3 = 48 个地块吗?
  • 尝试改用构面

标签: r plot dataframe


【解决方案1】:

尝试:

ggplot(data=mydata,aes(x=LU,y=RE))+geom_point()+facet_grid(COUNTRY+LIGHT~OR+PAT)

您可以更改 facet_grid(COUNTRY+LIGHT~OR+PAT) 中的变量来控制列/行划分。

这个图是用 1000 个样本制作的,以提供一些数据量

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2017-03-09
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    相关资源
    最近更新 更多