用R中的ggplot绘图

时间:2017-09-22 15:33:55

标签: r ggplot2 dplyr

我有一个示例数据:

dput(Meanws)
structure(list(m = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
    TimeStamp = structure(c(1390065206.10932, 1392422100, 1394970900, 
    1397606100, 1400241300, 1402876500, 1405511700, 1404511324.74897, 
    1389786772.51605, 1381924500, 1384559700, 1387139253.87506
    ), class = c("POSIXct", "POSIXt"), tzone = "GMT"), MeanWindSpeed102.5 = c(6.12204448017149, 
    6.54359101516919, 7.32096804511278, 6.29412731481481, 5.57985663082437, 
    5.45419907407407, 5.56513216845878, 5.89754085449892, 5.25164781412412, 
    7.2097311827957, 7.13449768518519, 8.17678562698607), MeanWindSpeed100.0 = c(6.05450964630225, 
    6.44605892648775, 7.23954417293233, 6.22275, 5.48870967741935, 
    5.3645625, 5.5027396953405, 5.8113152195314, 5.15942678080098, 
    7.10622311827957, 7.03509259259259, 8.07463211928624), MeanWindSpeed76.6 = c(5.40157020364416, 
    5.81589848308051, 6.54625, 5.69984953703704, 5.03385752688172, 
    4.88327777777778, 4.99367831541219, 5.27035833825556, 4.66075512075818, 
    6.43867159498208, 6.37995138888889, 7.36318259594231), MeanWindSpeed53.8 = c(4.59780010718114, 
    5.15528588098016, 5.77638862781955, 5.02735648148148, 4.51373431899642, 
    4.34299537037037, 4.38113127240143, 4.5946288639496, 3.98211861815958, 
    5.64894041218638, 5.64181712962963, 6.57381080420435)), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("m", "TimeStamp", "MeanWindSpeed102.5", 
"MeanWindSpeed100.0", "MeanWindSpeed76.6", "MeanWindSpeed53.8"
), row.names = c(NA, -12L))

是不同高度的月风速。

 head(Meanws)
# A tibble: 6 x 6
      m           TimeStamp MeanWindSpeed102.5 MeanWindSpeed100.0 MeanWindSpeed76.6 MeanWindSpeed53.8
  <dbl>              <dttm>              <dbl>              <dbl>             <dbl>             <dbl>
1     1 2014-01-18 17:13:26           6.122044           6.054510          5.401570          4.597800
2     2 2014-02-14 23:55:00           6.543591           6.446059          5.815898          5.155286
3     3 2014-03-16 11:55:00           7.320968           7.239544          6.546250          5.776389
4     4 2014-04-15 23:55:00           6.294127           6.222750          5.699850          5.027356
5     5 2014-05-16 11:55:00           5.579857           5.488710          5.033858          4.513734
6     6 2014-06-15 23:55:00           5.454199           5.364562          4.883278          4.342995

关键是我每次加载数据都有不同的高度,我必须手动调整绘图命令。甚至我试图重塑数据,如:

c=Meanws %>%
  tidyr::gather(key = "key", value = "value", -TimeStamp,-m) %>%
  tidyr::extract(col = key, into = c("variable", "height"), regex = "([:alpha:]+)([:digit:]+)") %>%
  tidyr::spread(key = variable, value = value)

新的数据框现在如下:

head(c)
# A tibble: 6 x 4
      m           TimeStamp height MeanWindSpeed
  <dbl>              <dttm>  <chr>         <dbl>
1     1 2014-01-18 17:13:26    100      6.054510
2     1 2014-01-18 17:13:26    102      6.122044
3     1 2014-01-18 17:13:26     53      4.597800
4     1 2014-01-18 17:13:26     76      5.401570
5     2 2014-02-14 23:55:00    100      6.446059
6     2 2014-02-14 23:55:00    102      6.543591

现在看起来更好!但我现在不知道如何进行分组以及如何使用ggplot获取所有月平均数据的单个图。

1 个答案:

答案 0 :(得分:3)

您已经适当地准备了数据,以便可以使用ggplot进行绘制。您需要做的是在x轴上映射m,在y轴上映射MeanWindSpeed,在颜色上映射height

library(ggplot2)
ggplot(c, aes(x = m, y = MeanWindSpeed, colour = height)) +
    geom_line() +
    scale_x_continuous(breaks = 1:12) +
    labs(x = "Month", y ="Mean Wind Speed", colour = "Height")

enter image description here

如果您想要点而非线条,只需将geom_line()替换为geom_point()即可。或者您也可以使用它们来获得点和线。