【问题标题】:How can Plot (time series plot) monthly temperatures from Jan to Dec for the years(1880-2017)?如何绘制(时间序列图)这些年(1880-2017)从 1 月到 12 月的月度温度?
【发布时间】:2018-02-24 19:18:30
【问题描述】:

下面是部分数据框,时间线其实是1880-2017

     Year   Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
1   1880 -0.29 -0.18 -0.11 -0.19 -0.11 -0.23 -0.21 -0.09 -0.16 -0.23 -0.20 -0.23
2   1881 -0.15 -0.17  0.04  0.04  0.02 -0.20 -0.06 -0.02 -0.13 -0.20 -0.21 -0.10
3   1882  0.15  0.15  0.04 -0.18 -0.16 -0.26 -0.20 -0.05 -0.10 -0.24 -0.16 -0.24
4   1883 -0.31 -0.39 -0.13 -0.17 -0.20 -0.12 -0.08 -0.15 -0.20 -0.14 -0.22 -0.16
5   1884 -0.15 -0.08 -0.37 -0.42 -0.36 -0.40 -0.34 -0.26 -0.27 -0.24 -0.30 -0.28
6   1885 -0.58 -0.30 -0.25 -0.42 -0.42 -0.44 -0.35 -0.31 -0.23 -0.19 -0.19 -0.05
7   1886 -0.42 -0.45 -0.38 -0.27 -0.26 -0.38 -0.21 -0.33 -0.25 -0.28 -0.31 -0.26
8   1887 -0.72 -0.52 -0.34 -0.38 -0.32 -0.23 -0.23 -0.32 -0.22 -0.32 -0.23 -0.33
9   1888 -0.37 -0.36 -0.41 -0.22 -0.22 -0.18 -0.10 -0.16 -0.10  0.02  0.01 -0.06
10  1889 -0.11  0.18  0.08  0.07 -0.02 -0.14 -0.10 -0.20 -0.22 -0.22 -0.33 -0.30

这是我尝试过的:

mydata=mydata = read.csv("GLB.Ts_dSST.csv")
subset2=as.data.frame(t(mydata[,1:13])) 
plot(subset2$V1[2:13])

但是,我不能得到线图,而是点图。我知道我可以在绘图函数中使用“l”来更改类型,但它会返回

"xy.coords(x, y, xlabel, ylabel, log) 中的错误:'x' 和 'y' 长度 不同”。

谁能帮帮我谢谢!!!

【问题讨论】:

  • 你是说你想要这么多年的单行吗?在这种情况下,您需要创建一个“长”数据集,其中一列中包含月份-年份,另一列中包含 y 值。
  • 请正确格式化这篇文章。也许您应该尝试ts() 将您的数据设置为时间序列,并尝试plot.ts()ts.plot() 进行绘图。
  • 你应该给我们一个可以使用的示例数据集。使用函数dput(mydata[1:10, ] )(获取前10行),复制输出并在这里与我们分享。
  • 首先,谢谢你们。然后我会为每两个十年(1990,1920,.., 2000)和 2017 年制作月度温度。所以我想我需要在一个情节中使用多条不同颜色的线条来显示这些年份之间的差异。 x 轴应该是 Jan 到 Dec,y 轴应该是度数

标签: r plot


【解决方案1】:

使用 的解决方案。我们可以将数据框转换为长格式并绘制出来。

library(tidyverse)

dat2 <- dat %>% 
  gather(Month, Degree, -Year) %>%
  mutate(Month = factor(Month, month.abb),
         Year = factor(Year))

ggplot(dat2, aes(x = Month, y = Degree, color = Year, group = Year)) +
  geom_line() +
  scale_color_brewer(type = "qual", palette = "Set3") +
  theme_classic()

如果您想按照您在评论中提到的那样汇总多年的数据,您可以执行以下操作。

dat3 <- dat %>% 
  gather(Month, Degree, -Year) %>%
  mutate(Month = factor(Month, month.abb),
         Year_Group = case_when(
           Year >= 1880 & Year <= 1882    ~"1880-1882",
           Year >= 1883 & Year <= 1885    ~"1883-1885",
           Year >= 1886 & Year <= 1889    ~"1886-1889"
         )) %>%
  group_by(Year_Group, Month) %>%
  summarise(Degree = mean(Degree))

ggplot(dat3, aes(x = Month, y = Degree, color = Year_Group, group = Year_Group)) +
  geom_line() +
  scale_color_brewer(type = "qual", palette = "Set1") +
  theme_classic()

数据

dat <- read.table(text = "     Year   Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
1   1880 -0.29 -0.18 -0.11 -0.19 -0.11 -0.23 -0.21 -0.09 -0.16 -0.23 -0.20 -0.23
                  2   1881 -0.15 -0.17  0.04  0.04  0.02 -0.20 -0.06 -0.02 -0.13 -0.20 -0.21 -0.10
                  3   1882  0.15  0.15  0.04 -0.18 -0.16 -0.26 -0.20 -0.05 -0.10 -0.24 -0.16 -0.24
                  4   1883 -0.31 -0.39 -0.13 -0.17 -0.20 -0.12 -0.08 -0.15 -0.20 -0.14 -0.22 -0.16
                  5   1884 -0.15 -0.08 -0.37 -0.42 -0.36 -0.40 -0.34 -0.26 -0.27 -0.24 -0.30 -0.28
                  6   1885 -0.58 -0.30 -0.25 -0.42 -0.42 -0.44 -0.35 -0.31 -0.23 -0.19 -0.19 -0.05
                  7   1886 -0.42 -0.45 -0.38 -0.27 -0.26 -0.38 -0.21 -0.33 -0.25 -0.28 -0.31 -0.26
                  8   1887 -0.72 -0.52 -0.34 -0.38 -0.32 -0.23 -0.23 -0.32 -0.22 -0.32 -0.23 -0.33
                  9   1888 -0.37 -0.36 -0.41 -0.22 -0.22 -0.18 -0.10 -0.16 -0.10  0.02  0.01 -0.06
                  10  1889 -0.11  0.18  0.08  0.07 -0.02 -0.14 -0.10 -0.20 -0.22 -0.22 -0.33 -0.30",
                  header = TRUE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-06
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多