简体   繁体   中英

How to make a scatter plot using ggplot2?

I am trying to make a data vs. Time graph for some Methane emissions data I have. The code so far looks like this:

CH4 <- as.numeric(Aeris_2_Data$CH4)
Aeris_2_Data$Date.Time <- as.POSIXct(Aeris_2_Data$Time_Stamp, tz = "", "%m/%d/%Y %H:%M:%S")

ggplot(Aeris_2_Data, aes(x = Aeris_2_Data$Date.Time, y = as.numeric(CH4)) + geom_point() + labs(x = "Time", y = "CH4 [ppm]") + ggtitle("Methane Over Time")

My data looks like this:

head(Aeris_2_Data) and this: an extension of head

I am trying to map CH4 over time as you can probably see from the small code fragment I've managed so far. but I keep getting the error:

Error in seq.int(0, to0 - from, by) : 'to' must be a finite number

Everything seems to match the ggplot info I remember and also found online. What is going wrong? My guess is to do with the formatting of the time data, which is in the format %m/%d/%Y %H:%M:%S and stored as a character in the csv file I am pulling from. How do I properly format that to change it? Thanks in advance.

There are two errors in your code :

  • date format is "%m/%d/%Y %H:%M" and not "%m/%d/%Y %H:%M:%S"
  • one ) is missing after aes()

Additionnaly as mentioned is the comments you should better use Date.Time and transform CH4 as numeric directly into the data.frame

The code should be:

Aeris_2_Data$CH4 <- as.numeric(Aeris_2_Data$CH4)
Aeris_2_Data$Date.Time <- as.POSIXct(Aeris_2_Data$Time_Stamp, tz = "", "%m/%d/%Y %H:%M")

ggplot(Aeris_2_Data, aes(x = Date.Time, y = as.numeric(CH4))) + geom_point() + labs(x = "Time", y = "CH4 [ppm]") + ggtitle("Methane Over Time")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM