R中具有合并中值曲线/直线的散点图

时间:2018-08-29 14:33:37

标签: r

我正在尝试制作一个散点图,将V2到V3绘制为两种不同的颜色,我可以使用下面的代码来做到这一点。

Dataset %>%
  filter(V2 < 1700) %>%
  ggplot(aes(x=V3, y=V2, col=V5))+
  geom_point(alpha=0.3)

我正在苦苦挣扎的地方是,然后我需要绘制V2中数据的中位数,而V3设置为0.5格。 (因此,中位数为1到1.5、1.5到2 ..... 15到15.5等)。理想情况下,它将显示在散点图的上方,并被过滤为V5数据=“良好”。

structure(list(Turbine_Power_002 = c(1670.233, 1662.4911, 1669.3992, 
1676.4406, 1670.4106, 1667.5694, 1676.624, 1678.1306, 1672.9572, 
1679.0921, 1673.1359, 1676.1989, 1671.6978, 1672.3488, 1670.723, 
1667.0172, 1660.8016, 1666.5119, 1664.0723, 1662.4677, 1660.4893, 
1660.5033, 1658.2013, 1662.0981, 1655.6329, 1652.6624, 1651.0581, 
1648.85, 1652.0266, 1651.4511, 1649.2938, 1650.1171, 1660.2227, 
1661.316, 1666.9459, 1636.5026, 1513.7669, 1328.3449, 1177.9153, 
1388.2277, 1361.618, 1528.8687, 1578.7937, 1617.7642, 1610.1726, 
1525.3414, 1357.1954, 1378.4442, 1154.6986, 1164.8143, 878.4577, 
845.1663, 839.8225, 740.9848, 717.6297, 629.4423, 681.7375, 501.3443, 
542.5272, 618.3111, 599.9095, 680.3778, 724.3055, 566.6395, 634.0534, 
759.797, 728.6984, 752.8808, 841.8173, 1184.8236, 1597.3052, 
1582.1896, 1507.2633, 1669.0799, 1675.5728, 1619.1406, 1631.349, 
1473.9408, 1493.4729, 1301.4209, 1312.0625, 1550.2915, 1663.0816, 
1689.5697, 1690.6906, 1689.2514, 1691.266, 1685.1839, 1687.3187, 
1684.73, 1683.7781, 1688.4965, 1687.2317, 1692.5478, 1689.7046, 
1688.2174), Wind_Speed_002 = c(10.614, 10.7256, 10.7985, 10.7571, 
10.7594, 10.8875, 11.0847, 10.9093, 10.7633, 10.553, 9.6269, 
9.6936, 9.4339, 9.1243, 9.3979, 9.3083, 8.6243, 8.55, 8.653, 
8.7922, 8.7225, 8.6905, 8.553, 8.5556, 8.5773, 8.6682, 8.6355, 
8.6509, 8.6767, 8.4967, 8.4006, 8.5276, 8.694, 9.2125, 8.5619, 
8.4786, 7.9269, 7.461, 7.2388, 7.1903, 7.2447, 7.3363, 7.3915, 
7.1909, 6.7161, 6.3915, 6.4324, 6.1211, 6.2195, 6.3502, 6.2055, 
6.1375, 5.3411, 5.3231, 5.9587, 6.3726, 6.2558, 6.1907, 6.1772, 
6.207, 6.4053, 6.5109, 6.3652, 6.7921, 6.5656, 6.3933, 6.3548, 
6.4828, 6.5872, 6.5717, 7.4201, 7.3904, 7.3151, 7.5944, 7.4698, 
8.1251, 7.4671, 7.7898, 7.4669, 7.4444, 7.5537, 7.7127, 7.8666, 
7.7562, 8.1654, 8.3819, 8.4663, 8.3663, 8.8485, 9.5838, 9.396, 
9.5373, 9.3826, 9.3901, 9.4029, 9.4286), Status = structure(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, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 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), .Label = c("bad", 
"good"), class = "factor")), class = "data.frame", row.names = c(NA, 
-96L))

不确定如何使它看起来不那么糟糕。

Turbine_Power_002   Wind_Speed_002  Status
1670.233    10.614  good
1662.4911   10.7256 good
1669.3992   10.7985 good
1676.4406   10.7571 good
1670.4106   10.7594 good
1667.5694   10.8875 good
1676.624    11.0847 good
1678.1306   10.9093 good
1672.9572   10.7633 good
1679.0921   10.553  good
1673.1359   9.6269  good
1676.1989   9.6936  good
1671.6978   9.4339  good
1672.3488   9.1243  good
1670.723    9.3979  good
1667.0172   9.3083  good
1660.8016   8.6243  good
1666.5119   8.55    good
1664.0723   8.653   good
1662.4677   8.7922  good
1660.4893   8.7225  good
1660.5033   8.6905  good
1658.2013   8.553   good
1662.0981   8.5556  good
1655.6329   8.5773  good
1652.6624   8.6682  good
1651.0581   8.6355  good
1648.85 8.6509  good
1652.0266   8.6767  good
1651.4511   8.4967  good
1649.2938   8.4006  good
1650.1171   8.5276  good
1660.2227   8.694   good
1661.316    9.2125  good
1666.9459   8.5619  good
1636.5026   8.4786  good
1513.7669   7.9269  good
1328.3449   7.461   good
1177.9153   7.2388  good
1388.2277   7.1903  good
1361.618    7.2447  good
1528.8687   7.3363  good
1578.7937   7.3915  good
1617.7642   7.1909  good
1610.1726   6.7161  bad
1525.3414   6.3915  bad
1357.1954   6.4324  bad
1378.4442   6.1211  bad
1154.6986   6.2195  bad
1164.8143   6.3502  good
878.4577    6.2055  good
845.1663    6.1375  good
839.8225    5.3411  bad
740.9848    5.3231  bad
717.6297    5.9587  good
629.4423    6.3726  bad
681.7375    6.2558  bad
501.3443    6.1907  bad
542.5272    6.1772  bad
618.3111    6.207   bad
599.9095    6.4053  bad
680.3778    6.5109  bad
724.3055    6.3652  bad
566.6395    6.7921  bad
634.0534    6.5656  bad
759.797 6.3933  bad
728.6984    6.3548  bad
752.8808    6.4828  bad
841.8173    6.5872  bad
1184.8236   6.5717  good
1597.3052   7.4201  good
1582.1896   7.3904  good
1507.2633   7.3151  good
1669.0799   7.5944  good
1675.5728   7.4698  good
1619.1406   8.1251  good
1631.349    7.4671  good
1473.9408   7.7898  good
1493.4729   7.4669  good
1301.4209   7.4444  good
1312.0625   7.5537  good
1550.2915   7.7127  good
1663.0816   7.8666  good
1689.5697   7.7562  good
1690.6906   8.1654  good
1689.2514   8.3819  good
1691.266    8.4663  good
1685.1839   8.3663  good
1687.3187   8.8485  good
1684.73 9.5838  good
1683.7781   9.396   good
1688.4965   9.5373  good
1687.2317   9.3826  good
1692.5478   9.3901  good
1689.7046   9.4029  good
1688.2174   9.4286  good

1 个答案:

答案 0 :(得分:0)

您可以独立于Status

尝试所有数据
library(tidyverse)
Breaks=seq(0, ceiling(max(d$Wind_Speed_002)), .5)

d %>%
  mutate(bins=.bincode(Wind_Speed_002, 
                  breaks = Breaks))  %>% 
  mutate(bins=factor(bins,levels = seq_along(Breaks), labels = Breaks)) %>% 
  group_by(bins) %>% 
  mutate(Med=median(Turbine_Power_002)) %>% 
  ungroup() %>% 
  mutate(bins2=as.numeric(as.character(bins))) %>% 
    ggplot()+
    geom_point(aes(x=Wind_Speed_002, y=Turbine_Power_002, col=Status), alpha=0.3) + 
    geom_segment(aes(x=bins2, xend=bins2+0.5, y=Med, yend=Med), size=1.5)

enter image description here