'plot' error: need fininte 'xlim' values

structure(list(Year = c("1950-51", "1951-52", "1952-53", "1953-54", 
"1954-55", "1955-56", "1956-57", "1957-58", "1958-59", "1959-60", 
"1960-61", "1961-62", "1962-63", "1963-64", "1964-65", "1965-66", 
"1966-67", "1967-68", "1968-69", "1969-70"), `Agriculture,forestry &fishing, mining and quarrying` = c(309778.081546566, 
315918.293114384, 325748.471153637, 349834.544607348, 360336.86170064, 
357640.194633914, 377021.228125322, 362088.596299249, 397223.302964248, 
394455.403756676, 422754.990087714, 424483.79148971, 419584.620969311, 
429578.352910062, 466909.881806579, 422166.454131611, 417423.429966446, 
475450.041127082, 475749.349223126, 505790.267172764), `Manufacturing, construction, electricity, gas and water supply` = c(71024.8329899426, 
74333.4571993094, 74000.0985650007, 78547.5537451879, 85485.4267328931, 
95514.8714575616, 104117.261544362, 102133.766652698, 109738.2221733, 
117482.72509848, 130234.697312003, 139207.469485798, 147826.046222797, 
163647.118969018, 175735.389015723, 181527.452003134, 188409.308730927, 
194810.85878802, 204686.626707986, 220467.656091401), `Trade, hotels, transport & communication` = c(35645.9358458439, 
36601.0549560834, 37790.2266243294, 39200.7231651127, 41736.23705468, 
44782.7592740203, 48076.0422179412, 49615.9572574612, 52121.8115108537, 
55381.6456801213, 60107.5119185363, 64029.6429031004, 67823.6180644857, 
72616.8858435318, 77495.3867131017, 78949.6156093551, 81030.2495534046, 
84542.7709470881, 88390.8780463258, 93178.3656349837), `Financial,  real estate and professional services` = c(60307.6425354584, 
61717.3421574957, 63848.0769681943, 65017.8978112565, 67034.3838623835, 
69307.3392369919, 70638.2326527219, 72909.9012033563, 74809.5148079799, 
77242.6681929456, 79089.2860093156, 81968.8276570792, 84619.6215907816, 
87201.291492371, 89652.4342735789, 92347.0160717823, 94386.1884641951, 
97071.4002719004, 100978.78044394, 104678.014035297), `Public administartion, defence and other services` = c(36061.4849394274, 
37111.7002937935, 37819.1899816502, 39051.1870056221, 40554.8628476613, 
41843.7338843571, 43641.5442162544, 45811.9084332629, 47822.8245695787, 
50027.6548507253, 52579.0233035853, 55177.5582947975, 59577.8162752905, 
63898.0896203925, 68541.6698327853, 71204.5376388472, 74688.0076129847, 
77679.4124961418, 81377.4307921611, 86322.8499729197), `Gross value added at basic prices` = c(479209.863520491, 
490398.990748315, 504315.537600103, 535010.186318987, 557723.77925299, 
572009.19843097, 604570.564679371, 597260.185851202, 642586.770175701, 
656648.63036811, 703137.721417578, 724957.078409297, 740293.224819461, 
777772.888750421, 836758.927093302, 806176.057867204, 814380.672637655, 
880654.733910147, 903636.597626835, 962522.034426209)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))
plot(gva_sub$Year,gva_sub$`Gross value added at basic prices`,type = 'l') 

This gives me error as :
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

ggplot(gva_sub)+geom_line(aes(x=Year, y= `Gross value added at basic prices`) )

Gives me this error:
Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : **
** invalid graphics state

The basic difficulty is that your Year column is characters. The plot() function cannot plot using characters on the x axis. You can plot such data with ggplot, but your graphics device probably got confused with the failed plot() call. Running dev.off() would probably fix that. You will still have a problem using ggplot() because it will not know how to group the data. The code below shows how to make a plot, rather an ugly one, in ggplot with your original data and it shows how making a numeric column for your Year make the plotting much easier.

gva_sub <- structure(list(Year = c("1950-51", "1951-52", "1952-53", "1953-54", 
                                   "1954-55", "1955-56", "1956-57", "1957-58", "1958-59", "1959-60", 
                                   "1960-61", "1961-62", "1962-63", "1963-64", "1964-65", "1965-66", 
                                   "1966-67", "1967-68", "1968-69", "1969-70"), 
                          `Agriculture,forestry &fishing, mining and quarrying` = c(309778.081546566, 315918.293114384, 325748.471153637, 349834.544607348, 360336.86170064, 
                                                                                    357640.194633914, 377021.228125322, 362088.596299249, 397223.302964248, 
                                                                                    394455.403756676, 422754.990087714, 424483.79148971, 419584.620969311, 
                                                                                    429578.352910062, 466909.881806579, 422166.454131611, 417423.429966446, 
                                                                                    475450.041127082, 475749.349223126, 505790.267172764), 
                          `Manufacturing, construction, electricity, gas and water supply` = c(71024.8329899426, 74333.4571993094, 74000.0985650007, 78547.5537451879, 85485.4267328931, 
                                                                                               95514.8714575616, 104117.261544362, 102133.766652698, 109738.2221733, 
                                                                                               117482.72509848, 130234.697312003, 139207.469485798, 147826.046222797, 
                                                                                               163647.118969018, 175735.389015723, 181527.452003134, 188409.308730927, 
                                                                                               194810.85878802, 204686.626707986, 220467.656091401), 
                          `Trade, hotels, transport & communication` = c(35645.9358458439, 36601.0549560834, 37790.2266243294, 39200.7231651127, 41736.23705468, 
                                                                         44782.7592740203, 48076.0422179412, 49615.9572574612, 52121.8115108537, 
                                                                         55381.6456801213, 60107.5119185363, 64029.6429031004, 67823.6180644857, 
                                                                         72616.8858435318, 77495.3867131017, 78949.6156093551, 81030.2495534046, 
                                                                         84542.7709470881, 88390.8780463258, 93178.3656349837), 
                          `Financial,  real estate and professional services` = c(60307.6425354584, 61717.3421574957, 63848.0769681943, 65017.8978112565, 67034.3838623835, 
                                                                                  69307.3392369919, 70638.2326527219, 72909.9012033563, 74809.5148079799, 
                                                                                  77242.6681929456, 79089.2860093156, 81968.8276570792, 84619.6215907816, 
                                                                                  87201.291492371, 89652.4342735789, 92347.0160717823, 94386.1884641951, 
                                                                                  97071.4002719004, 100978.78044394, 104678.014035297), 
                          `Public administartion, defence and other services` = c(36061.4849394274, 37111.7002937935, 37819.1899816502, 39051.1870056221, 40554.8628476613, 
                                                                                  41843.7338843571, 43641.5442162544, 45811.9084332629, 47822.8245695787, 
                                                                                  50027.6548507253, 52579.0233035853, 55177.5582947975, 59577.8162752905, 
                                                                                  63898.0896203925, 68541.6698327853, 71204.5376388472, 74688.0076129847, 
                                                                                  77679.4124961418, 81377.4307921611, 86322.8499729197), 
                          `Gross value added at basic prices` = c(479209.863520491, 490398.990748315, 504315.537600103, 535010.186318987, 557723.77925299, 
                                                                  572009.19843097, 604570.564679371, 597260.185851202, 642586.770175701, 
                                                                  656648.63036811, 703137.721417578, 724957.078409297, 740293.224819461, 
                                                                  777772.888750421, 836758.927093302, 806176.057867204, 814380.672637655, 
                                                                  880654.733910147, 903636.597626835, 962522.034426209)), 
                     row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(stringr)
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.3.3

ggplot(gva_sub)+
geom_line(aes(x=Year, y= `Gross value added at basic prices`, group = 1))


gva_sub <- gva_sub |> mutate(Year_num = as.numeric(str_extract(Year, "^\\d{4}")))
ggplot(gva_sub)+geom_line(aes(x=Year_num, y= `Gross value added at basic prices`))

plot(gva_sub$Year_num, gva_sub$`Gross value added at basic prices`,type = 'l')

Created on 2024-08-31 with reprex v2.0.2

1 Like

Yo why did you use double \ instead of just single \ in str_extract.

If you use a single , you get an error.

gva_sub <- gva_sub |> mutate(Year_num = as.numeric(str_extract(Year, "^\d{4}")))
Error: '\d' is an unrecognized escape in character string (<input>:1:73)

As I understand it, this happens because the code goes through two interpreters, first one for R and then one for the reg ex engine. With a double backslash, the first slash informs the first interpreter that the second backslash is to be taken literally, so the reg ex engine receives \d. With only one slash, the R interpreter tries to find a meaning for \d, doesn't find it, and throws an error.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.