Stefano Sofia
2020-May-07 14:09 UTC
[R] Change the colours of some of the labels of the x-axis
Dear R users, in a plot I need to change the colours of some of the labels of the x-axis. In the example below reported, I would like to have the labels of the last and second-last date in red. I tried to find the solution searching the web, but I could not understand the hints and I was not sure I could adapt them to my example. There might be an easy way to do that? Thank you for your attention and your help Stefano first_day <- "2005-01-23-09-00" last_day <- "2005-01-27-09-00" first_day_POSIX <- as.POSIXct(first_day, format="%Y-%m-%d-%H-%M") last_day_POSIX <- as.POSIXct(last_day, format="%Y-%m-%d-%H-%M") df_plot <- data.frame(data_POSIX=seq(first_day_POSIX, last_day_POSIX, by="30 mins")) df_plot$hs1 <- 5 df_plot$hs2 <- 7 plot(df_plot$data_POSIX, df_plot$hs1, type="b", ylim=c(0, 10), col="blue", xaxt="n", axes=F, main="", xlab="", ylab="Snow height") lines(df_plot$data_POSIX, df_plot$hs2, ylim=c(0, 10), col="red", type="b") axis.POSIXct(1, at=seq(trunc(as.POSIXct(first_day, format="%Y-%m-%d-%H-%M"), "days"), trunc(as.POSIXct(last_day, format="%Y-%m-%d-%H-%M"), "days"), by="12 hours"), format="h%H-%d-%m-%y", pos=0) axis(side=2, at=seq(0, 15, 5)) (oo) --oOO--( )--OOo---------------- Stefano Sofia PhD Civil Protection - Marche Region Meteo Section Snow Section Via del Colle Ameno 5 60126 Torrette di Ancona, Ancona Uff: 071 806 7743 E-mail: stefano.sofia at regione.marche.it ---Oo---------oO---------------- ________________________________ AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu? contenere informazioni confidenziali, pertanto ? destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si ? il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si ? ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell?art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit? ed urgenza, la risposta al presente messaggio di posta elettronica pu? essere visionata da persone estranee al destinatario. IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system. -- Questo messaggio stato analizzato da Libra ESVA ed risultato non infetto. This message was scanned by Libra ESVA and is believed to be clean. [[alternative HTML version deleted]]
Rui Barradas
2020-May-07 15:41 UTC
[R] Change the colours of some of the labels of the x-axis
Hello, You cannot pass a vector of colors to col.axis, you need to plot the axis twice, once the normal axis, like in your code, then overplot just those last 2 labels. xlabels <- seq(trunc(as.POSIXct(first_day, format="%Y-%m-%d-%H-%M"), "days"), trunc(as.POSIXct(last_day, format="%Y-%m-%d-%H-%M"), "days"), by="12 hours") plot(df_plot$data_POSIX, df_plot$hs1, type="b", xlim = c(min(xlabels), max(xlabels)), ylim=c(0, 10), col="blue", xaxt="n", axes=F, main="", xlab="", ylab="Snow height") lines(df_plot$data_POSIX, df_plot$hs2, ylim=c(0, 10), col="red", type="b") axis.POSIXct(1, at = xlabels, format="h%H-%d-%m-%y", pos=0, las = 2) axis.POSIXct(1, at = tail(xlabels, 2), format="h%H-%d-%m-%y", pos=0, las = 2, col.axis = "red") axis(side=2, at=seq(0, 15, 5)) Hope this helps, Rui Barradas ?s 15:09 de 07/05/20, Stefano Sofia escreveu:> Dear R users, > in a plot I need to change the colours of some of the labels of the x-axis. > In the example below reported, I would like to have the labels of the last and second-last date in red. > I tried to find the solution searching the web, but I could not understand the hints and I was not sure I could adapt them to my example. > There might be an easy way to do that? > > Thank you for your attention and your help > Stefano > > first_day <- "2005-01-23-09-00" > last_day <- "2005-01-27-09-00" > first_day_POSIX <- as.POSIXct(first_day, format="%Y-%m-%d-%H-%M") > last_day_POSIX <- as.POSIXct(last_day, format="%Y-%m-%d-%H-%M") > df_plot <- data.frame(data_POSIX=seq(first_day_POSIX, last_day_POSIX, by="30 mins")) > df_plot$hs1 <- 5 > df_plot$hs2 <- 7 > > plot(df_plot$data_POSIX, df_plot$hs1, type="b", ylim=c(0, 10), col="blue", xaxt="n", axes=F, main="", xlab="", ylab="Snow height") > lines(df_plot$data_POSIX, df_plot$hs2, ylim=c(0, 10), col="red", type="b") > axis.POSIXct(1, at=seq(trunc(as.POSIXct(first_day, format="%Y-%m-%d-%H-%M"), "days"), trunc(as.POSIXct(last_day, format="%Y-%m-%d-%H-%M"), "days"), by="12 hours"), format="h%H-%d-%m-%y", pos=0) > axis(side=2, at=seq(0, 15, 5)) > > > (oo) > --oOO--( )--OOo---------------- > Stefano Sofia PhD > Civil Protection - Marche Region > Meteo Section > Snow Section > Via del Colle Ameno 5 > 60126 Torrette di Ancona, Ancona > Uff: 071 806 7743 > E-mail: stefano.sofia at regione.marche.it > ---Oo---------oO---------------- > > ________________________________ > > AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu? contenere informazioni confidenziali, pertanto ? destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si ? il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si ? ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell?art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit? ed urgenza, la risposta al presente messaggio di posta elettronica pu? essere visionata da persone estranee al destinatario. > IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system. > > -- > Questo messaggio stato analizzato da Libra ESVA ed risultato non infetto. > This message was scanned by Libra ESVA and is believed to be clean. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
Stefano Sofia
2020-May-09 09:32 UTC
[R] Change the colours of some of the labels of the x-axis
Thank you very much. Stefano (oo) --oOO--( )--OOo---------------- Stefano Sofia PhD Civil Protection - Marche Region Meteo Section Snow Section Via del Colle Ameno 5 60126 Torrette di Ancona, Ancona Uff: 071 806 7743 E-mail: stefano.sofia at regione.marche.it ---Oo---------oO---------------- ________________________________________ Da: Rui Barradas [ruipbarradas at sapo.pt] Inviato: gioved? 7 maggio 2020 17.41 A: Stefano Sofia; r-help at r-project.org Oggetto: Re: [R] Change the colours of some of the labels of the x-axis Hello, You cannot pass a vector of colors to col.axis, you need to plot the axis twice, once the normal axis, like in your code, then overplot just those last 2 labels. xlabels <- seq(trunc(as.POSIXct(first_day, format="%Y-%m-%d-%H-%M"), "days"), trunc(as.POSIXct(last_day, format="%Y-%m-%d-%H-%M"), "days"), by="12 hours") plot(df_plot$data_POSIX, df_plot$hs1, type="b", xlim = c(min(xlabels), max(xlabels)), ylim=c(0, 10), col="blue", xaxt="n", axes=F, main="", xlab="", ylab="Snow height") lines(df_plot$data_POSIX, df_plot$hs2, ylim=c(0, 10), col="red", type="b") axis.POSIXct(1, at = xlabels, format="h%H-%d-%m-%y", pos=0, las = 2) axis.POSIXct(1, at = tail(xlabels, 2), format="h%H-%d-%m-%y", pos=0, las = 2, col.axis = "red") axis(side=2, at=seq(0, 15, 5)) Hope this helps, Rui Barradas ?s 15:09 de 07/05/20, Stefano Sofia escreveu:> Dear R users, > in a plot I need to change the colours of some of the labels of the x-axis. > In the example below reported, I would like to have the labels of the last and second-last date in red. > I tried to find the solution searching the web, but I could not understand the hints and I was not sure I could adapt them to my example. > There might be an easy way to do that? > > Thank you for your attention and your help > Stefano > > first_day <- "2005-01-23-09-00" > last_day <- "2005-01-27-09-00" > first_day_POSIX <- as.POSIXct(first_day, format="%Y-%m-%d-%H-%M") > last_day_POSIX <- as.POSIXct(last_day, format="%Y-%m-%d-%H-%M") > df_plot <- data.frame(data_POSIX=seq(first_day_POSIX, last_day_POSIX, by="30 mins")) > df_plot$hs1 <- 5 > df_plot$hs2 <- 7 > > plot(df_plot$data_POSIX, df_plot$hs1, type="b", ylim=c(0, 10), col="blue", xaxt="n", axes=F, main="", xlab="", ylab="Snow height") > lines(df_plot$data_POSIX, df_plot$hs2, ylim=c(0, 10), col="red", type="b") > axis.POSIXct(1, at=seq(trunc(as.POSIXct(first_day, format="%Y-%m-%d-%H-%M"), "days"), trunc(as.POSIXct(last_day, format="%Y-%m-%d-%H-%M"), "days"), by="12 hours"), format="h%H-%d-%m-%y", pos=0) > axis(side=2, at=seq(0, 15, 5)) > > > (oo) > --oOO--( )--OOo---------------- > Stefano Sofia PhD > Civil Protection - Marche Region > Meteo Section > Snow Section > Via del Colle Ameno 5 > 60126 Torrette di Ancona, Ancona > Uff: 071 806 7743 > E-mail: stefano.sofia at regione.marche.it > ---Oo---------oO---------------- > > ________________________________ > > AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu? contenere informazioni confidenziali, pertanto ? destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si ? il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si ? ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell?art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit? ed urgenza, la risposta al presente messaggio di posta elettronica pu? essere visionata da persone estranee al destinatario. > IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system. > > -- > Questo messaggio stato analizzato da Libra ESVA ed risultato non infetto. > This message was scanned by Libra ESVA and is believed to be clean. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Questo messaggio stato analizzato con Libra ESVA ed risultato non infetto. ________________________________ AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu? contenere informazioni confidenziali, pertanto ? destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si ? il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si ? ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell?art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit? ed urgenza, la risposta al presente messaggio di posta elettronica pu? essere visionata da persone estranee al destinatario. IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system. -- Questo messaggio stato analizzato da Libra ESVA ed risultato non infetto. This message was scanned by Libra ESVA and is believed to be clean.