0

many issues why this is happening seem to be because of comma and point usage for decimals. My dataframe does use points for showing decimals. The general structure is like this:

  longitude latitude o3_col3(ppb)
1       -10       35     57.05763          
2       -10       34     61.53371
3       -10       33     51.24268
4       -10       32     50.26531
5       -10       31     50.26531
6       -10       30     42.59164

And I try to plot using ggplot like this:

ggplot(ascplot, aes(x=longitude, y=latitude, fill = as.numeric("o3_col3(ppb)"))) + 
  geom_raster() + 
  scale_fill_gradientn(colours=rainbow(7))

First the issue was the column name being in brackets, the second was:

Error: Discrete value supplied to continuous scale

But seemed to be fixed by using as.numeric().

However now the following error shows up:

Warning message:
In FUN(X[[i]], ...) : NAs introduced by coercion

I checked all the values in the column, but none of them are NA or in any way different than one another. Anyone know what could cause this issue?

My goal is to plot the ozone column value on the lat/lon positions.

B.Quaink
  • 456
  • 1
  • 4
  • 18
  • 1
    Be sure to share data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It's not clear what data types are being used for your data. But using double quotes around column names just returns the name, not the column values. Maybe you want ```fill = as.numeric(`o3_col3(ppb)`)``` But there is very likely some value on that column that is not numeric. It seems unlikely that R would lie. – MrFlick Apr 14 '20 at 18:32
  • You can use this existing answer to find non-numeric values: https://stackoverflow.com/questions/21196106/finding-non-numeric-data-in-an-r-data-frame-or-vector – MrFlick Apr 14 '20 at 18:34

1 Answers1

0

It was the most simple solution:

changed

# plot raster
ggplot(ascplot, aes(x=longitude, y=latitude, fill = as.numeric("o3_col3(ppb)"))) + 
  geom_raster() + 
  scale_fill_gradientn(colours=rainbow(7))

to

# plot raster
ggplot(ascplot, aes(x=longitude, y=latitude, fill = as.numeric(ascplot$"o3_col3(ppb)"))) + 
  geom_raster() + 
  scale_fill_gradientn(colours=rainbow(7))

Didn't know you needed to index in the data.frame again when using quotation marks in as.numeric().

B.Quaink
  • 456
  • 1
  • 4
  • 18