简体   繁体   English

R:使用xyplot中的线将特定变量级别的点连接起来

[英]R: Use lines in xyplot to connet points from a specific variable level

I have a data frame df with the following variables: Test , A , p and D . 我有一个带有以下变量的数据框dfTestApD。 I want to print A against p , grouping by variable D levels. 我想针对p打印A ,按可变D级分组。

I am using xyplot to do so and type=('p','l') to plot the points and the connecting lines between points. 我正在使用xyplot来执行此操作,然后键入=('p','l')绘制点和点之间的连接线。 This procedure connects all points of the same level of variable D . 此过程将变量D的相同级别的所有点连接起来。 The problem is that I want to connect only the points from the same Test . 问题是我只想连接同一Test中的点。

Any clue how this can be done? 任何线索如何做到这一点? Or if it is possible to do it with xyplot ? 或者如果可以用xyplot做到这一点

Example for data 数据示例

    Test         A           p     D
95    32 0.0000000 0.010148395 55.04
96    32 0.2746429 0.018040352 55.04
97    32 1.0000000 0.019101864 55.04
271   72 0.0000000 0.005900593 22.64
272   72 0.1677419 0.006708547 22.64
273   72 0.4290323 0.009770698 22.64
274   72 1.0000000 0.010637487 22.64
275   73 0.0000000 0.005175419 22.64
276   73 0.2242424 0.006683234 22.64
277   73 0.3793939 0.009750174 22.64
278   73 1.0000000 0.012231502 22.64

This is the code I am using: 这是我正在使用的代码:

xyplot(A ~ p, data=df, groups=D, type=c("p","l"))

This is an example of what I would like to achieve. 这是我想要实现的示例。 Maybe it is not possible with xyplot . 也许xyplot是不可能的。

Example of what I want to achieve 我要实现的示例

Quite difficult to know for sure what you're asking. 很难确定您要问的是什么。 Do try to elaborate further with an example figure of what you want to achieve. 请尝试通过示例示例进一步说明您要实现的目标。

Anyway, here's a quick way using ggplot to show respective Ap plots for different levels of D and Test : 无论如何,这是使用ggplot显示不同DTest水平的相应Ap图的快速方法:

ggplot(df, aes(A, p)) + 
    geom_line() + 
    geom_point() + 
    facet_grid(D~Test)

在此处输入图片说明

I think I finally get to solve this problem. 我想我终于可以解决这个问题了。

With data originally posted and the code 带有原始发布的数据和代码

xyplot(A ~ p, data=df, groups=D, type=c("p","l"))

I get a scatter plot where points through different Test are connected against my will like in the following figure . 我得到了一个散点图,其中通过不同Test点违背了我的意愿,如下图所示

I add to "cheat" R in order to get what I wanted: connect only points from the same Test . 我添加到“作弊” R中以获得我想要的东西:仅连接同一Test点。 To do that I added rows to the original data frame with NA values at the end of each Test . 为此,我将行添加到原始数据帧,并在每个Test的末尾添加NA值。

   Test    A     p     D
1    32    0  0.01 35.04
2    32 0.27 0.018 35.04
3    32    1 0.019 35.04
4    32 <NA>  <NA> 35.04
5    72    0 0.006 12.64
6    72 0.17 0.007 12.64
7    72 0.43  0.01 12.64
8    72    1 0.011 12.64
9    72 <NA>  <NA> 12.64
10   73    0 0.005 12.64
11   73 0.22 0.007 12.64
12   73 0.38  0.01 12.64
13   73    1 0.012 12.64
14   73 <NA>  <NA> 12.64

And the result is showed in this figure . 结果如图所示

I hope this post can help others. 我希望这篇文章可以帮助其他人。

Best regards 最好的祝福

Ricardo 里卡多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM