[R] Change the colours of some of the labels of the x-axis

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu May 7 17:41:41 CEST 2020


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 using 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 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