从ggplot中的Grouped Data制作散点图?

时间:2015-08-28 06:50:35

标签: r ggplot2

我有一个带有序列数据的data.frame,里面有从1:6分组的数据集。每个组都有20行和3列。

当我绘制这3列时,我想区分哪个趋势线属于哪个列。

由于数据格式与mtcars或我使用的任何其他现有样本数据格式不同。

  x <- rep(c(seq(2,40,length.out=15),seq(2,20,length.out=15),seq(2,30,length.out=15)),times=2);
  yy <- replicate(3,c(sort(10^runif(15,-9,1),decreasing=TRUE), sort(10^runif(15,-6,1),decreasing=TRUE),sort(10^runif(15,-3,0),decreasing=TRUE)))
  No <- rep(seq(1:6),each=15)
  df <-   data.frame(x,yy,No)

#        

           x           X1           X2           X3 No
1   2.000000 6.261953e+00 1.757757e-01 3.886871e+00  1
2   4.714286 6.226159e+00 1.697990e-01 3.266477e-01  1
3   7.428571 5.365266e+00 6.694360e-03 1.809687e-01  1
4  10.142857 5.249526e+00 9.127380e-04 1.131525e-01  1
5  12.857143 2.797482e+00 3.497180e-04 6.449251e-02  1
6  15.571429 2.877427e-01 1.938191e-04 6.007792e-02  1
7  18.285714 2.538422e-01 1.197096e-04 1.097680e-03  1
8  21.000000 2.020753e-02 1.134354e-04 2.466243e-05  1
9  23.714286 4.311454e-03 6.875258e-05 1.775533e-05  1
10 26.428571 2.602848e-03 1.146186e-05 4.845931e-07  1
11 29.142857 8.287594e-06 3.594231e-06 1.325905e-07  1
12 31.857143 1.379797e-06 5.030768e-07 4.489024e-08  1
13 34.571429 1.253097e-06 2.085111e-07 7.997009e-09  1
14 37.285714 3.735280e-07 6.909048e-08 6.527832e-09  1
15 40.000000 1.380056e-08 1.812853e-09 5.100159e-09  1
16  2.000000 6.821891e+00 1.222666e+00 2.684762e+00  2
17  3.285714 1.923142e+00 1.036708e+00 1.484942e+00  2
18  4.571429 1.784273e+00 7.738927e-01 1.306751e+00  2
19  5.857143 1.394139e+00 5.151793e-01 5.812497e-01  2
20  7.142857 3.114498e-01 2.818626e-01 4.488202e-01  2
21  8.428571 2.657815e-01 1.386009e-02 3.581497e-01  2
22  9.714286 1.937902e-01 2.168172e-03 2.360844e-02  2
23 11.000000 6.575245e-02 1.330587e-03 1.272102e-02  2
24 12.285714 3.096995e-02 7.447904e-04 3.919140e-03  2
25 13.571429 1.966715e-02 3.601749e-04 3.635299e-03  2
26 14.857143 1.428500e-04 1.540179e-04 2.867794e-03  2
27 16.142857 5.285846e-05 2.884267e-06 5.203390e-04  2
28 17.428571 3.853737e-05 1.832420e-06 2.346204e-04  2
29 18.714286 6.855677e-06 1.784994e-06 9.354291e-05  2
30 20.000000 1.512266e-06 1.152505e-06 1.014575e-05  2
31  2.000000 9.433434e-01 5.226171e-01 9.796947e-01  3
32  4.000000 5.372254e-01 2.491069e-01 4.007479e-01  3
33  6.000000 4.643998e-01 2.344810e-01 2.522774e-01  3
34  8.000000 3.966998e-01 1.606139e-01 1.380709e-01  3
35 10.000000 3.712383e-01 1.289794e-01 1.267681e-01  3
36 12.000000 3.155555e-01 1.169318e-01 8.759734e-02  3
37 14.000000 3.043470e-01 5.413357e-02 5.146976e-02  3
38 16.000000 8.046015e-02 3.184139e-02 1.221951e-02  3
39 18.000000 2.079006e-02 2.949842e-02 1.076110e-02  3
40 20.000000 9.353735e-03 2.743903e-02 9.442426e-03  3
41 22.000000 5.983976e-03 6.207805e-03 7.685787e-03  3
42 24.000000 4.969129e-03 2.517870e-03 6.336618e-03  3
43 26.000000 4.902493e-03 1.491867e-03 5.157470e-03  3
44 28.000000 1.846900e-03 1.289566e-03 1.619360e-03  3
45 30.000000 1.699238e-03 1.139109e-03 1.029230e-03  3
46  2.000000 6.261953e+00 1.757757e-01 3.886871e+00  4
47  4.714286 6.226159e+00 1.697990e-01 3.266477e-01  4
48  7.428571 5.365266e+00 6.694360e-03 1.809687e-01  4
49 10.142857 5.249526e+00 9.127380e-04 1.131525e-01  4
50 12.857143 2.797482e+00 3.497180e-04 6.449251e-02  4
51 15.571429 2.877427e-01 1.938191e-04 6.007792e-02  4
52 18.285714 2.538422e-01 1.197096e-04 1.097680e-03  4
53 21.000000 2.020753e-02 1.134354e-04 2.466243e-05  4
54 23.714286 4.311454e-03 6.875258e-05 1.775533e-05  4
55 26.428571 2.602848e-03 1.146186e-05 4.845931e-07  4
56 29.142857 8.287594e-06 3.594231e-06 1.325905e-07  4
57 31.857143 1.379797e-06 5.030768e-07 4.489024e-08  4
58 34.571429 1.253097e-06 2.085111e-07 7.997009e-09  4
59 37.285714 3.735280e-07 6.909048e-08 6.527832e-09  4
60 40.000000 1.380056e-08 1.812853e-09 5.100159e-09  4
61  2.000000 6.821891e+00 1.222666e+00 2.684762e+00  5
62  3.285714 1.923142e+00 1.036708e+00 1.484942e+00  5
63  4.571429 1.784273e+00 7.738927e-01 1.306751e+00  5
64  5.857143 1.394139e+00 5.151793e-01 5.812497e-01  5
65  7.142857 3.114498e-01 2.818626e-01 4.488202e-01  5
66  8.428571 2.657815e-01 1.386009e-02 3.581497e-01  5
67  9.714286 1.937902e-01 2.168172e-03 2.360844e-02  5
68 11.000000 6.575245e-02 1.330587e-03 1.272102e-02  5
69 12.285714 3.096995e-02 7.447904e-04 3.919140e-03  5
70 13.571429 1.966715e-02 3.601749e-04 3.635299e-03  5
71 14.857143 1.428500e-04 1.540179e-04 2.867794e-03  5
72 16.142857 5.285846e-05 2.884267e-06 5.203390e-04  5
73 17.428571 3.853737e-05 1.832420e-06 2.346204e-04  5
74 18.714286 6.855677e-06 1.784994e-06 9.354291e-05  5
75 20.000000 1.512266e-06 1.152505e-06 1.014575e-05  5
76  2.000000 9.433434e-01 5.226171e-01 9.796947e-01  6
77  4.000000 5.372254e-01 2.491069e-01 4.007479e-01  6
78  6.000000 4.643998e-01 2.344810e-01 2.522774e-01  6
79  8.000000 3.966998e-01 1.606139e-01 1.380709e-01  6
80 10.000000 3.712383e-01 1.289794e-01 1.267681e-01  6
81 12.000000 3.155555e-01 1.169318e-01 8.759734e-02  6
82 14.000000 3.043470e-01 5.413357e-02 5.146976e-02  6
83 16.000000 8.046015e-02 3.184139e-02 1.221951e-02  6
84 18.000000 2.079006e-02 2.949842e-02 1.076110e-02  6
85 20.000000 9.353735e-03 2.743903e-02 9.442426e-03  6
86 22.000000 5.983976e-03 6.207805e-03 7.685787e-03  6
87 24.000000 4.969129e-03 2.517870e-03 6.336618e-03  6
88 26.000000 4.902493e-03 1.491867e-03 5.157470e-03  6
89 28.000000 1.846900e-03 1.289566e-03 1.619360e-03  6
90 30.000000 1.699238e-03 1.139109e-03 1.029230e-03  6

因此我将df转换为包含melt的长数据格式,但输出图有意连接所有X1 X2X3列。这就是问题所在。

library(reshape2)
df_new <- melt(df,id=c("No","x"))



 ggplot(df_new,aes(x=x, y=value,col=variable))+
geom_point()+
geom_line()+
scale_y_log10(breaks=c(1e-12,1e-10,1e-8,1e-6,1e-4,1e-2,1),limits = c(1e-9,1))
到目前为止

输出图像是这样的; 任何帮助将不胜感激。

enter image description here

1 个答案:

答案 0 :(得分:1)

这更像是你想要的方向吗? (警告;由于比例而缺少数据)

ggplot(df_new,aes(x=x, y=value,col=variable))+
  geom_point()+
  geom_line()+
  scale_y_log10(breaks=c(1e-12,1e-10,1e-8,1e-6,1e-4,1e-2,1),limits = c(1e-9,1)) +
  facet_wrap(~No)

enter image description here