是否可以使用第三个变量作为R中的滤波器来绘制两个变量? [英] Is it possible to plot two variables using a third one as filter in R?

查看:40
本文介绍了是否可以使用第三个变量作为R中的滤波器来绘制两个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个变量( DATE INT_RATE )创建一个图,并将第三个变量过滤器> GRADE .问题是我真的无法弄清楚如何使用变量 GRADE 作为行的过滤器.

I am trying to create a plot using two variables (DATE and INT_RATE) using for filter the content of a third variable GRADE. The problem is that I can't really figure out how to use the variable GRADE as a filter for the row.

在下面的部分中,我提供了详细的初始数据示例以及我要实现的绘图图.预先感谢.

In the below section i provide a detailed sample of starting data as well as draw of the plot I'm trying to achieve. Thanks in advance.

 STARTING DATA     

 | DATE  | INT_RATE | GRADE |
––––––––––––––––––––––––––––––
 | 1-jan | 5%       | A     | <-- A
 | 5-feb | 3%       | B     |
 | 9-feb | 2%       | D     |
 | 1-apr | 3%       | A     | <-- A
 | 5-jun | 5%       | A     | <-- A
 | 1-aug | 3%       | G     |
 | 1-sep | 2%       | E     |
 | 3-nov | 1%       | C     |
 | 8-dec | 8%       | A     | <-- A
 |   .   | .        | .     |
 |   .   | .        | .     |
 |   .   | .        | .     |

这是我想要实现的图形,这是非常基本的图形,除了之前需要进行的过滤工作之外.

And this is the kind of graph i would like to achieve, which is a very basic one, except for the filtering work needed before.

WANTED RESULT:

GRADE "A"

   INT_RATE
       |
       |
    8%-|                            •   
       |                           ̷
       |                        ̷ 
       |                     ̷
    5%-|  •              •
       |   \            /
       |     \        /
       |       \     /
       |         \ /
    3%-|          •
       |
       |  
       |
       |
    ––––––––––––––––––––––––––––––––––-–––>
       |  ˆ       ˆ      ˆ           ˆ   DATE
       |1-jan   1-apr   5-jun      8-dec

在@apax的宝贵帮助下,我设法得到了一个图,但是由于R显示它的方式很奇怪,所以结果令人不满意(我认为这可能与所讨论的数据集非常大这一事实有关80万行).你有什么建议吗?

Following the precious help from @apax I managed to get a plot, but the result is not satisfying because of the weird way R is displaying it (I think it might be related to the fact that the dataset in question is very large 800k rows). Do you have any suggestion?

顺便说一句,这解决了我的问题:

By the way, this solved my problem:

plot(x = DATE, y = INT_RATE, data =  filter(df, GRADE == "A"))

我还上传了格式错误的图表的PNG.再次感谢所有人.

I am also uploading a PNG of the malformed chart. Thanks again to all.

推荐答案

这是一种快速的一线解决方案,我假设您的数据存储在名为 df

Here's a quick one-liner solution where I assume your data is stored in an object named df

library(dplyr) ## For filter() function below

plot(x = DATE, y = INT_RATE, data =  filter(df, GRADE == "A"))

这篇关于是否可以使用第三个变量作为R中的滤波器来绘制两个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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