[R] Need help plotting

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Sep 20 10:51:33 CEST 2022


Hello,

Now with data, here are base R and ggplot2 plots.


b <- read.table(text=
                 "Dtime DNO2 DVOC     Dpm10    Dpm2.5 Dpm1 Mtime MNO2 
MVOC    Mpm10 Mpm2.5 Mpm1
  18:00   28  164  81.34773 24.695435   14 18:00   19  151 3.000000 
  2    1
  18:01   27  163  74.44034 23.751198   14 18:01   20  148 3.000000 
  2    1
  18:02   30  160  72.21975 22.463129   13 18:02   19  150 3.000000 
  2    1",
               header=TRUE,stringsAsFactors=FALSE)


# This base R plot needs package chron
library(chron)

# first make a real time class column
b$Dtime2 <- paste(b$Dtime, "00", sep = ":")
b$Dtime2 <- as.times(b$Dtime2)

# get the plot height and make room for
# the legend by adding 2 to y max
ylim <- range(unlist(b[c("DNO2", "MNO2")])) + c(0, 2)

# now plot one line
plot(DNO2 ~ Dtime2, b, type = "b", col = "orange", ylim = ylim)
# add the second line
points(MNO2 ~ Dtime2, b, type = "b", col = "skyblue")
# and the horizontal legend at the top center
legend("top", legend = c("DNO2", "MNO2"), horiz = TRUE,
        lty = "solid", col = c("orange", "skyblue"))


#-------

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

b %>%
   mutate(Dtime = paste(Sys.Date(), Dtime),
                 Dtime = as.POSIXct(Dtime)) %>%
   select(Dtime, DNO2, MNO2) %>%
   # reshape to long format
   pivot_longer(-Dtime, names_to = "NO2") %>%
   # now plot
   ggplot(aes(Dtime, value, color = NO2)) +
   geom_line() +
   geom_point() +
   scale_color_manual(values = c("orange", "skyblue")) +
   # make datetime labels
   scale_x_datetime(date_breaks = "1 mins", date_labels = "%H:%M") +
   theme_bw()


Hope this helps,

Rui Barradas


Às 08:37 de 20/09/2022, Jim Lemon escreveu:
> Hi David,
> I'm back home again. Try this:
> 
> b<-read.table(text=
> "Dtime DNO2 DVOC     Dpm10    Dpm2.5 Dpm1 Mtime MNO2 MVOC    Mpm10 Mpm2.5 Mpm1
>   18:00   28  164  81.34773 24.695435   14 18:00   19  151 3.000000      2    1
>   18:01   27  163  74.44034 23.751198   14 18:01   20  148 3.000000      2    1
>   18:02   30  160  72.21975 22.463129   13 18:02   19  150 3.000000      2    1",
>   header=TRUE,stringsAsFactors=FALSE)
> b$POSIXtime<-strptime(paste("2022-09-20",b$Dtime),"%Y-%m-%d %H:%M")
> png("DFPNO2.png")
> plot(b$POSIXtime,b$DNO2,type="b",main="NO2 readings (2022-09-20)",
>   xlab="Date/time",ylab="NO2",ylim=range(c(b$DNO2,b$MNO2)),
>   pch=19,col="red")
>   points(b$POSIXtime,b$MNO2,type="b",pch=19,col="blue")
>   legend("center",legend=c("DNO2","MNO2"),pch=19,col=c("red","blue"))
> dev.off()
> 
> If you have more than one day, it will show up on the x axis. You can
> also format the tick labels if you want the full dates for only one
> day.
> 
> Jim
> 
> PS thanks Avi
> 
> On Tue, Sep 20, 2022 at 4:49 PM Parkhurst, David <parkhurs using indiana.edu> wrote:
>>
>> Thank you.
>>
>> DFP (iPad)
>>
>>> On Sep 19, 2022, at 8:15 AM, Ebert,Timothy Aaron <tebert using ufl.edu> wrote:
>>>
>>> My version of this email has a bunch of ? that I do not know how to interpret. Emails to this group need to be in plain text. HTML content is deleted or converted and impossible or at least difficult to interpret.
>>>
>>> Do not share confidential data. Please change some numbers or variable names and share that.
>>> If this helps:
>>> 1) Make sure your time variable is a datetime object.
>>> 2) At least in ggplot it should now behave as expected.
>>> ggplot(df, aes(y=NO2, x=datetime)) + geom_point()
>>>
>>> That will be a start as a scatterplot, but the graph can be customized or changed if scatterplot was not desired.
>>>
>>> Tim
>>>
>>> -----Original Message-----
>>> From: R-help <r-help-bounces using r-project.org> On Behalf Of Parkhurst, David
>>> Sent: Sunday, September 18, 2022 4:27 PM
>>> To: r-help using r-project.org
>>> Subject: [R] Need help plotting
>>>
>>> [External Email]
>>>
>>> I�ve been retired since �06 and have forgotten most of R.  Now I have a use for it, with some data from Bloomington�s Environmental Commission.
>>>
>>> I have a dataframe (obtained from read.csv) that contains numerous columns, including time (in Excel�s 18:00 format), and DNO2, and MNO2 from two air quality instruments.
>>>
>>> I�d like a plot of both the NO2 measurements against time.  I be happy to use either ordinary R plots or ggplot2 ones, if that would be a better way.  I�d much appreciate help.
>>>
>>>         [[alternative HTML version deleted]]
>>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list