R中ggplot中的问题

时间:2014-07-29 02:20:50

标签: r ggplot2

数据集

> dput(head(spdistuc,50))
structure(list(Lane = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L), Vehicle.class = c(2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), 
    speedmph = c(0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 
    30.6, 34, 37.4, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 
    30.6, 34, 37.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6, 
    34, 0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6, 
    34, 37.4, 0, 3.4, 6.8, 10.2, 13.6, 17), cprob = c(0, 0.01, 
    0.04, 0.08, 0.14, 0.22, 0.32, 0.5, 0.73, 0.95, 0.99, 1, 0, 
    0, 0.03, 0.07, 0.16, 0.3, 0.51, 0.81, 0.99, 1, 1, 0, 0.03, 
    0.05, 0.1, 0.21, 0.49, 0.84, 1, 1, 0, 0, 0.01, 0.01, 0.06, 
    0.1, 0.17, 0.4, 0.76, 0.95, 1, 1, 0, 0, 0.01, 0.01, 0.02, 
    0.04)), .Names = c("Lane", "Vehicle.class", "speedmph", "cprob"
), row.names = c(6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 64L, 65L, 66L, 67L, 
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 88L, 89L, 90L, 91L, 92L, 
93L), class = "data.frame")

> dput(head(cspdistuv,50))
structure(list(lanem = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), cars.speedmph = c(18, 
20, 22, 24, 26, 28, 30, 32, 34, 36, 10, 15, 20, 25, 30, 35, 5, 
10, 15, 20, 25, 30, 35, 0, 5, 10, 15, 20, 25, 30, 35, 5, 10, 
15, 20, 25, 30, 35), cars.prob = c(0, 0.13, 0.17, 0.2, 0.2, 0.27, 
0.37, 0.8, 0.97, 1, 0, 0.03, 0.13, 0.4, 0.77, 1, 0, 0.03, 0.17, 
0.27, 0.5, 0.8, 1, 0, 0.03, 0.1, 0.27, 0.53, 0.6, 0.83, 1, 0, 
0.07, 0.17, 0.33, 0.53, 0.8, 1)), .Names = c("lanem", "cars.speedmph", 
"cars.prob"), row.names = c(NA, 38L), class = "data.frame")

问题

我绘制了spdistuc

cu1 <- ggplot() + geom_point(data = spdistuc, mapping = aes(x=speedmph, y = cprob, color = 'observed')) + facet_wrap(~Lane) + theme_bw() + my.theme()

这给了我以下信息:

enter image description here

但是当我在现有的情节上添加另一个情节时,

cu2 <- cu1 + geom_point(data = cspdistuv, mapping = aes(x = cars.speedmph, y = cars.prob, color = 'simulated-default')) + facet_wrap(~lanem)

我得到了以下内容:

enter image description here

问题

为什么现有的情节(“观察”)发生了变化?对于x轴上的单个值,您可以看到多于1个点。我做错了什么?

1 个答案:

答案 0 :(得分:0)

将我的评论扩展为答案:

问题是你在第一个数据集中使用“Lane”,在第二个数据集中使用“lanem”。

这可以通过使列名相同来解决。

names(cspdistuv)[names(cspdistuv) == "lanem"] <- "Lane"

进行此项更改后,您不需要在facet_wrap定义中加入cu2。它仍然会从cu1的定义中记住。