English 中文(简体)
R, ggplot2 - x 轴上的时间
原标题:R, ggplot2 - time on x axis
  • 时间:2024-07-25 13:27:00
  •  标签:
  • r
  • ggplot2
In R I have a data frame with bid and ask prices for a single day. Here is an example: TimeHM; Bid_Price1; Ask_Price1 08:12; 101; 102 08:13; 101; 102 08:14; 102; 104 ... 17:05; 104; 105 So, each new row is a new minute. Variable TimeHM is a character, while Bid_Price1 and Ask_Price1 are numeric. I would like to plot Bid and ask prices for the whole day, and I would like to mark “10:00” with a vertical line. Here is my code: ggplot(MyData, aes(x=TimeHM)) + geom_line(aes(y=Bid_Price1, group=1)) + geom_line(aes(y=Ask_Price1, group=1)) + geom_vline(xintercept = "10:00", color="red", linetype="dashed") + xlab("Time") + ylab("Price") + theme_classic(base_size = 20) It works (also the vertical line), and “group=1” is also necessary for some reason. But the problem is that on the x-axis it labels each observation (for each minute). Instead I want to label some specific points in time, e.g. 9:00; 12:00; 15:00. I have tried different solutions by googling. I guess the problem is that it does not see TimeHM as time. So, I tried the following before the ggplot: MyData$TimeHM <- as.POSIXct(strptime(MyData$TimeHM, format="%H:%M")) But then for some reason it adds the date of today, and then uses the time. For example, the first line suddenly becomes “07-25-2024 08:12:00”. Then I tried to add one of the following to the ggplot: #scale_x_datetime(labels = date_format("%H:%M")) #scale_x_date(labels = date_format("%H:%M")) #scale_x_datetime(labels = label_date("%H:%M")) It simply gives me an error. Somehow I once managed to plot it with scale_x_datetime, but both curves (bid and ask) were shifted to the left such that the first price was before 8:00 (while the dataset starts after 8:00). I have also tried with as_hms. Can someone help? What is the right way to do it? Thank you!
问题回答
Use dplyr s mutate to convert TimeHM to a datetime in a simple pipe, and scales::label_time to format the labels on scale_x_time(): library(ggplot2) df <- data.frame( TimeHM = c("08:12", "09:13", "10:14", "11:15"), Bid_Price1 = c(100, 101, 102, 105), Ask_Price1 = c(102, 103, 104, 106) ) df |> dplyr::mutate(TimeHM = as.POSIXct(TimeHM, format = "%H:%M", tz = "UTC")) |> ggplot(aes(x = TimeHM)) + geom_line(aes(y = Bid_Price1, group = 1), color = "#df2f9f") + geom_line(aes(y = Ask_Price1, group = 1), color = "#106faf")+ geom_vline(xintercept = as.POSIXct("10:00", format = "%H:%M", tz = "UTC"), color = "red", linetype = "dashed")+ scale_y_continuous(limits = c(90, 110)) + scale_x_time( labels = scales::label_time(format = "%H:%M"), breaks = as.POSIXct(c("09:00", "12:00", "15:00"), format = "%H:%M", tz = "UTC"), limits = as.POSIXct(c("08:00", "15:59"), format = "%H:%M", tz = "UTC") )
Coerce the time column to class "POSIXct" and scale_x_datetime will take care of the labels. As for the two lines, it was asked before several times. This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format. And this post on how to separate the lines. In the OP the aesthetic used is group, others (color, linetype) will further differentiate the lines and add a legend to the plot. MyData <- read.csv2(text = "TimeHM; Bid_Price1; Ask_Price1 08:12; 101; 102 08:13; 101; 102 08:14; 102; 104 17:05; 104; 105") suppressPackageStartupMessages({ library(dplyr) library(ggplot2) }) x0 <- as.POSIXct(paste(Sys.Date(), "10:00")) MyData %>% mutate(TimeHM = as.POSIXct(paste(Sys.Date(), TimeHM))) %>% tidyr::pivot_longer(-TimeHM) %>% ggplot(aes(TimeHM, value, group = name)) + geom_line() + geom_vline(xintercept = x0, color = "red", linetype = "dashed") + scale_x_datetime(date_breaks = "3 hours", date_labels = "%H:%M") + xlab("Time") + ylab("Price") + theme_classic(base_size = 20) Created on 2024-07-25 with reprex v2.1.0




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签