Skip to content

How to specify year-month-day for a plot

7 messages · Gregory Coats, Jim Lemon, Jeff Newmiller +1 more

#
Starting with year-month-day, for the variable gallons, I can easily plot the variable gallons, while disregarding the date. 
gallons <- c (15.973, 18.832, 17.392, 14.774, 19.248, 14.913, 15.226, 14.338, 18.777, 19.652)
plot (gallons, type="l", xlab="X label", ylab="Y label", col="blue?)

How do I direct R to plot the variable gallons, at the appropriate, irregularly-spaced places on the X axis, while paying attention to the year-month-day?

format = "%Y-%m-%d?
2020-01-05 15.973
2020-02-15 18.832
2020-03-10 17.392
2020-05-04 14.774
2020-06-21 19.248
2020-08-01 14.913
2020-08-27 15.226
2020-09-28 14.338
2020-11-09 18.777
2020-12-11 19.652
1 day later
#
Hi Gregory,
Here's a start:

gcdf<-read.table(text="2020-01-05 15.973
2020-02-15 18.832
2020-03-10 17.392
2020-05-04 14.774
2020-06-21 19.248
2020-08-01 14.913
2020-08-27 15.226
2020-09-28 14.338
2020-11-09 18.777
2020-12-11 19.652",
header=TRUE,stringsAsFactors=FALSE,
col.names=c("date","gallons"))
gcdf$date<-as.Date(gcdf$date,"%Y-%m-%d")
plot(gcdf$date,gcdf$gallons,main="Gallons by date",
xlab="Date",ylab="Gallons")

Jim

On Mon, Dec 14, 2020 at 9:33 AM Gregory Coats via R-help
<r-help at r-project.org> wrote:
#
By converting the character date data into a time-like type... e.g. ?as.Date and plotting y-vs-x.
On December 12, 2020 11:18:46 AM PST, Gregory Coats via R-help <r-help at r-project.org> wrote:

  
    
#
Hi Jim,
Thank you VERY much!
In what I tried, my values for the vertical Y were automatically understood by R.
But it appears that the string yyyy-mm-dd was NOT recognized by R as a date for the X axis, and gave a red error message.
My values for Year-Month-Day were NOT understood by R.
Is there a convenient way to tell R to interpret ?2020-12-13? as a date?

data <- data.frame(
  day = as.Date("2020-02-15", "2020-03-10", "2020-05-04", "2020-06-21", "2020-08-01", "2020-08-27", "2020-09-28", "2020-11-09", "2020-12-11")
  value = (15.973, 18.832, 17.392, 14.774, 19.248, 14.913, 15.226, 14.338, 18.777, 19.652))
p <- ggplot(data, aes(x=day, y=value))
  geom_line()
  xlab("X Label")
p
Error: unexpected symbol in:
"  day = as.Date("2020-02-15", "2020-03-10", "2020-05-04", "2020-06-21", "2020-08-01", "2020-08-27", "2020-09-28", "2020-11-09", "2020-12-11")
  value"

Greg Coats
gregcoats at me.com
Reston, Virginia USA

  
  
#
You left out some calls to c().  Note that
   (2,3,5)
is not valid syntax for making a vector of numbers; use
   c(2,3,5)
You also left out a comma and gave different lengths for day and value.
You also left out plus signs between the various components of your ggplot
expression.

Try
 data <- data.frame(
     day = as.Date(c("2020-02-15", "2020-03-10", "2020-05-04",
"2020-06-21", "2020-08-01", "2020-08-27", "2020-09-28", "2020-11-09",
"2020-12-11", "2020-12-12")),
     value = c(15.973, 18.832, 17.392, 14.774, 19.248, 14.913, 15.226,
14.338, 18.777, 19.652))

 p <- ggplot(data, aes(x=day, y=value))+
       geom_line()+
       xlab("X Label")
 p

On Sun, Dec 13, 2020 at 5:35 PM Gregory Coats via R-help <
r-help at r-project.org> wrote:

            

  
  
#
Hi Gregory,
On Mon, Dec 14, 2020 at 12:34 PM Gregory Coats <gregcoats at me.com> wrote:
Notice the as.Date command in the code I sent to you. this converts a
string to a date with a resolution of one day. If you want a higher
time resolution, use strptime or one of the other POSIX date
conversion functions.

Jim
#
Bill pointed out some errors in your code but you keep making the claim that YYYY-MM-DD is not recognized and I just want to make it completely clear that that is the default format for dates in R as it is an ISO standard. So focus on other issues until you get it working... this format is definitely not your problem.
On December 13, 2020 5:34:45 PM PST, Gregory Coats via R-help <r-help at r-project.org> wrote: