【问题标题】:ggplot2 error message: Error in seq.default(range[1], range[2], length.out = nframes) : 'from' must be a finite numberggplot2 错误消息:seq.default(range[1], range[2], length.out = nframes) 中的错误:'from' 必须是有限数
【发布时间】:2020-05-05 18:25:24
【问题描述】:

我正在尝试制作以下数据的动画气泡图:

Country/Region  1971    1972    1973    1974    1975    1976    1977    1978    1979    1980    1981    1982    1983    1984    1985    1986    1987    1988    1989    1990    1991    1992    1993    1994    1995    1996    1997    1998    1999    2000    2001    2002    2003    2004    2005    2006    2007    2008    2009    2010    GDP per Capita
  Albania        3.9    4.5      3.9     4.2     4.5    4.9     5.2     6.2      7.5    7.6     6.4      6.7     7.3     7.6    7.2     7.2     7.5      7.6    7.2     6.3      4.4     2.8     2.3    2.3     1.9     1.9      1.4    1.7     3.0      3.1     3.3     3.8    4.0     4.3     4.1      4.0    4.0     3.9      3.5     3.8 .   5,626
  Austria       48.7    50.5    54.0    51.3    50.2    54.3    51.8    54.5    57.2    55.7    52.8    51.0    51.1    52.9    54.3    53.2    54.2    52.1    52.5    56.4    60.6    55.7    56.0    56.2    59.4    63.1    62.4    62.9    61.4    61.7    65.9    67.4    72.6    73.7    74.6    72.5    70.0    70.6    63.5    69.3    56,259
  Belarus       0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     124.5   119.4   98.8    82.9    70.2    61.4    62.7    61.8    59.3    57.6    58.7    57.8    59.2    60.7    63.0    62.1    66.2    64.0    64.5    62.3    65.3    6,575
  Belgium       116.8   126.7   132.7   130.6   115.6   124.5   123.5   129.0   132.3   125.7   115.5   109.3   100.6   102.6   101.9   102.6   102.8   104.6   105.9   107.9   113.3   112.3   109.8   115.5   115.2   121.3   118.5   120.9   117.4   118.6   119.1   111.9   119.5   116.5   112.6   109.6   105.6   111.0   100.7   106.4   51,237
  Bosnia        0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     23.7    21.2    15.6    13.1    3.0     3.2     4.1    8.3 10.5    10.2    13.5    13.3    14.0    14.3    15.0    15.6    17.2    18.2    19.9    19.4    19.9    6,140
  Bulgaria      62.8    64.8    66.6    67.7    72.2    72.1    74.8    77.9    81.1    83.8    79.9    81.5    80.2    78.3    81.1    82.1    83.1    82.1    81.4    74.8    56.4    54.1    55.1    52.5    53.2    53.8    50.9    48.7    42.8    42.1    44.8    42.0    46.3    45.4    45.9    47.3    50.4    49.0    42.2    43.8    9,811
  Croatia       0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     21.6    15.7    15.2    15.8    15.0    15.8    15.6    17.3    18.4    18.3    17.7    18.6    19.6    21.0    20.4    20.8    20.8    22.1    21.0    19.8    19.0    15,533
  Cyprus        1.8     2.2     2.3     1.8     1.7     2.0     2.1     2.3     2.5     2.6     2.5     2.6     2.7     2.8     2.8     3.1     3.6     3.6     3.8     3.8     4.4     4.7     4.9     5.3     5.2     5.5     5.7     5.8     6.0     6.3     6.2     6.3     7.0     6.9     7.0     7.1     7.3     7.6     7.5     7.2     30,521
Czech Republic  151.0   150.0   147.1   146.3   152.6   157.4   166.9   163.0   172.5   165.8   166.5   169.3   170.5   173.1   173.1   173.1   174.2   170.8   163.5   155.1   140.9   131.4   126.7   120.2   123.7   125.6   124.0   117.6   110.9   121.9   121.4   117.2   120.7   121.8   119.6   120.7   122.0   117.3   110.1   114.5   26,114
  Denmark       55.0    57.1    56.0    49.8    52.5    58.1    59.7    59.2    62.7    62.5    52.5    54.6    51.3    52.9    60.5    61.1    59.3    55.5    49.8    50.4    60.5    54.8    57.1    61.0    58.0    71.2    61.6    57.7    54.6    50.6    52.2    51.9    57.1    51.6    48.3    56.0    51.4    48.4    46.7    47.0    66,196
  Estonia       0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     36.1    32.1    23.5    18.0    17.8    16.1    17.0    16.5    16.0    14.9    14.6    15.1    14.6    16.6    16.7    16.9    15.5    19.3    17.7    14.7    18.5    25,260
  Finland       39.8    43.7    48.0    44.5    44.4    50.5    50.2    54.7    54.4    55.2    46.0    44.5    43.2    44.4    48.6    49.5    53.8    53.1    52.9    54.4    55.9    53.7    54.8    61.4    56.0    62.2    60.1    56.8    56.1    55.1    60.3    63.0    70.8    67.2    55.2    66.8    65.0    57.0    55.0    62.9    54,869
  France        431.9   448.6   484.8   464.6   430.6   469.3   455.3   474.7   481.8   461.4   414.1   396.7   381.0   369.5   360.3   347.8   342.3   340.5   355.9   352.3   379.6   368.0   348.9   344.4   353.8   368.6   361.7   385.3   377.7   376.9   383.8   375.9   385.2   385.4   388.4   379.6   373.1   370.2   351.4   357.8   46,493
  Germany       978.6   1003.2  1053.1  1028.5  975.5   1032.2  1017.2  1055.9  1103.6  1055.6  1022.3  982.3   983.9   1006.1  1014.6  1016.3  1007.2  1001.2  976.8   949.7   924.8   886.5   879.9   868.5   867.8   896.5   865.8   858.9   826.9   825.0   843.3   830.7   839.8   840.8   809.0   820.9   796.3   800.1   747.1   761.6   53,276

我的气泡图代码是:

ggplot(europe.gdp, aes(europe.gdp$`GDP per capita`, europe.gdp$CO2.per.capita, size = europe.gdp$CO2.per.capita, color = europe.gdp$`Country/Region`)) +geom_point() +scale_x_log10() +theme_bw() +labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'CO2 emissions/ tonnes per capita') +transition_time('Year: {frame_time}) +ease_aes('linear')

其中 C02.per.capita 是以年份为首的数据。 但是我不断收到此错误:

no non-missing arguments to min; returning Infno non-missing arguments to max; returning -InfError in seq.default(range[1], range[2], length.out = nframes) : 
  'from' must be a finite number

这是什么意思,我该如何解决? 我对 r 很陌生,所以如果这真的很容易解决,我很抱歉。

【问题讨论】:

  • CO2.per.capita 只是每行总和的一列。它不包含在原始数据集中,它只是我使用代码创建的一个变量:europe.GDP$CO2_per_capita = rowSums(europe.GDP[,c(as.character(1971:2010))]).
  • 您必须在transition_time 中定义time 变量。过年了吗?
  • 是的,我该怎么做?

标签: r ggplot2 error-handling r-markdown gganimate


【解决方案1】:

问题是transition_time 函数需要在你的数据中接收一个变量,告诉它现在是哪一年,但你没有给它一个。

基本上,您的数据格式不正确。您需要首先从宽格式数据帧切换到长格式数据帧。

这意味着您不再有按年份列出 CO2 的列,而是有一个国家/地区列、一个 CO2 列和一个 GDP 列。国家和 GDP 将每年重复一次。我们可以使用 dplyr 和 tidyr 包来做到这一点:

library(tidyr)
library(dplyr)
library(ggplot2)
library(gganimate)

df <- europe.gdp                                                    %>% 
      group_by(`Country/Region`)                                    %>%
      gather(key = "Year", value = "CO2", 
             -`Country/Region`, -`GDP per Capita`, -CO2.per.capita) %>% 
      as.data.frame
df$GDP <- rep(europe.gdp$`GDP per Capita`, 40)
df$Year <- as.numeric(df$Year)

现在您可以使用年份作为动画变量:

ggplot(data = df, 
       aes(x     = GDP, 
           y     = CO2, 
           size  = CO2, 
           color = `Country/Region`)) + 
  geom_point()                        + 
  scale_x_log10()                     +
  theme_bw()                          +
  labs(title = 'Year: {frame_time}', 
       x     = 'GDP per capita', 
       y     = 'CO2 emissions/ tonnes per capita') + 
  transition_time(Year) + ease_aes('linear')

这是从问题和 cmets 中剥离的可重现数据:

europe.gdp <- structure(list(`Country/Region` = c("Albania", "Austria", "Belarus", 
"Belgium", "Bosnia", "Bulgaria", "Croatia", "Cyprus", "Czech Republic", 
"Denmark", "Estonia", "Finland", "France", "Germany"), `1971` = c(3.9, 
48.7, 0, 116.8, 0, 62.8, 0, 1.8, 151, 55, 0, 39.8, 431.9, 978.6
), `1972` = c(4.5, 50.5, 0, 126.7, 0, 64.8, 0, 2.2, 150, 57.1, 
0, 43.7, 448.6, 1003.2), `1973` = c(3.9, 54, 0, 132.7, 0, 66.6, 
0, 2.3, 147.1, 56, 0, 48, 484.8, 1053.1), `1974` = c(4.2, 51.3, 
0, 130.6, 0, 67.7, 0, 1.8, 146.3, 49.8, 0, 44.5, 464.6, 1028.5
), `1975` = c(4.5, 50.2, 0, 115.6, 0, 72.2, 0, 1.7, 152.6, 52.5, 
0, 44.4, 430.6, 975.5), `1976` = c(4.9, 54.3, 0, 124.5, 0, 72.1, 
0, 2, 157.4, 58.1, 0, 50.5, 469.3, 1032.2), `1977` = c(5.2, 51.8, 
0, 123.5, 0, 74.8, 0, 2.1, 166.9, 59.7, 0, 50.2, 455.3, 1017.2
), `1978` = c(6.2, 54.5, 0, 129, 0, 77.9, 0, 2.3, 163, 59.2, 
0, 54.7, 474.7, 1055.9), `1979` = c(7.5, 57.2, 0, 132.3, 0, 81.1, 
0, 2.5, 172.5, 62.7, 0, 54.4, 481.8, 1103.6), `1980` = c(7.6, 
55.7, 0, 125.7, 0, 83.8, 0, 2.6, 165.8, 62.5, 0, 55.2, 461.4, 
1055.6), `1981` = c(6.4, 52.8, 0, 115.5, 0, 79.9, 0, 2.5, 166.5, 
52.5, 0, 46, 414.1, 1022.3), `1982` = c(6.7, 51, 0, 109.3, 0, 
81.5, 0, 2.6, 169.3, 54.6, 0, 44.5, 396.7, 982.3), `1983` = c(7.3, 
51.1, 0, 100.6, 0, 80.2, 0, 2.7, 170.5, 51.3, 0, 43.2, 381, 983.9
), `1984` = c(7.6, 52.9, 0, 102.6, 0, 78.3, 0, 2.8, 173.1, 52.9, 
0, 44.4, 369.5, 1006.1), `1985` = c(7.2, 54.3, 0, 101.9, 0, 81.1, 
0, 2.8, 173.1, 60.5, 0, 48.6, 360.3, 1014.6), `1986` = c(7.2, 
53.2, 0, 102.6, 0, 82.1, 0, 3.1, 173.1, 61.1, 0, 49.5, 347.8, 
1016.3), `1987` = c(7.5, 54.2, 0, 102.8, 0, 83.1, 0, 3.6, 174.2, 
59.3, 0, 53.8, 342.3, 1007.2), `1988` = c(7.6, 52.1, 0, 104.6, 
0, 82.1, 0, 3.6, 170.8, 55.5, 0, 53.1, 340.5, 1001.2), `1989` = c(7.2, 
52.5, 0, 105.9, 0, 81.4, 0, 3.8, 163.5, 49.8, 0, 52.9, 355.9, 
976.8), `1990` = c(6.3, 56.4, 124.5, 107.9, 23.7, 74.8, 21.6, 
3.8, 155.1, 50.4, 36.1, 54.4, 352.3, 949.7), `1991` = c(4.4, 
60.6, 119.4, 113.3, 21.2, 56.4, 15.7, 4.4, 140.9, 60.5, 32.1, 
55.9, 379.6, 924.8), `1992` = c(2.8, 55.7, 98.8, 112.3, 15.6, 
54.1, 15.2, 4.7, 131.4, 54.8, 23.5, 53.7, 368, 886.5), `1993` = c(2.3, 
56, 82.9, 109.8, 13.1, 55.1, 15.8, 4.9, 126.7, 57.1, 18, 54.8, 
348.9, 879.9), `1994` = c(2.3, 56.2, 70.2, 115.5, 3, 52.5, 15, 
5.3, 120.2, 61, 17.8, 61.4, 344.4, 868.5), `1995` = c(1.9, 59.4, 
61.4, 115.2, 3.2, 53.2, 15.8, 5.2, 123.7, 58, 16.1, 56, 353.8, 
867.8), `1996` = c(1.9, 63.1, 62.7, 121.3, 4.1, 53.8, 15.6, 5.5, 
125.6, 71.2, 17, 62.2, 368.6, 896.5), `1997` = c(1.4, 62.4, 61.8, 
118.5, 8.3, 50.9, 17.3, 5.7, 124, 61.6, 16.5, 60.1, 361.7, 865.8
), `1998` = c(1.7, 62.9, 59.3, 120.9, 10.5, 48.7, 18.4, 5.8, 
117.6, 57.7, 16, 56.8, 385.3, 858.9), `1999` = c(3, 61.4, 57.6, 
117.4, 10.2, 42.8, 18.3, 6, 110.9, 54.6, 14.9, 56.1, 377.7, 826.9
), `2000` = c(3.1, 61.7, 58.7, 118.6, 13.5, 42.1, 17.7, 6.3, 
121.9, 50.6, 14.6, 55.1, 376.9, 825), `2001` = c(3.3, 65.9, 57.8, 
119.1, 13.3, 44.8, 18.6, 6.2, 121.4, 52.2, 15.1, 60.3, 383.8, 
843.3), `2002` = c(3.8, 67.4, 59.2, 111.9, 14, 42, 19.6, 6.3, 
117.2, 51.9, 14.6, 63, 375.9, 830.7), `2003` = c(4, 72.6, 60.7, 
119.5, 14.3, 46.3, 21, 7, 120.7, 57.1, 16.6, 70.8, 385.2, 839.8
), `2004` = c(4.3, 73.7, 63, 116.5, 15, 45.4, 20.4, 6.9, 121.8, 
51.6, 16.7, 67.2, 385.4, 840.8), `2005` = c(4.1, 74.6, 62.1, 
112.6, 15.6, 45.9, 20.8, 7, 119.6, 48.3, 16.9, 55.2, 388.4, 809
), `2006` = c(4, 72.5, 66.2, 109.6, 17.2, 47.3, 20.8, 7.1, 120.7, 
56, 15.5, 66.8, 379.6, 820.9), `2007` = c(4, 70, 64, 105.6, 18.2, 
50.4, 22.1, 7.3, 122, 51.4, 19.3, 65, 373.1, 796.3), `2008` = c(3.9, 
70.6, 64.5, 111, 19.9, 49, 21, 7.6, 117.3, 48.4, 17.7, 57, 370.2, 
800.1), `2009` = c(3.5, 63.5, 62.3, 100.7, 19.4, 42.2, 19.8, 
7.5, 110.1, 46.7, 14.7, 55, 351.4, 747.1), `2010` = c(3.8, 69.3, 
65.3, 106.4, 19.9, 43.8, 19, 7.2, 114.5, 47, 18.5, 62.9, 357.8, 
761.6), `GDP per Capita` = c(5626, 56259, 6575, 51237, 6140, 
9811, 15533, 30521, 26114, 66196, 25260, 54869, 46493, 53276), 
    CO2.per.capita = c(186.9, 2358.2, 1482.4, 4586.8, 293.2, 
    2495, 389.5, 176.5, 5690, 2218.2, 388.2, 2171.1, 15679.1, 
    37054)), row.names = c(NA, -14L), class = "data.frame")

【讨论】:

  • 是的,我们可以通过 df$Year &lt;- as.POSIXct(paste0(df$Year, "-01-01 00:00:00")) 将其更改为日期
  • 您必须将Year 定义为整数(或日期),因为绘图标题显示“奇怪的”十进制数字。
  • 嗨@AllanCameron,如果你有时间,你能在这里看看我的类似问题吗?谢谢! »»» stackoverflow.com/questions/62030888/…
猜你喜欢
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多