Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot in the x-axis a rotate labels in R [duplicate]

Tags:

plot

r

I've to plot these data:

day                 temperature
02/01/2012 13:30:00     10 
10/01/2012 20:30:00      8  
15/01/2012 13:30:00     12
25/01/2012 20:30:00      6
02/02/2012 13:30:00      5
10/02/2012 20:30:00      3
15/02/2012 13:30:00      6
25/02/2012 20:30:00     -1
02/03/2012 13:30:00      4
10/03/2012 20:30:00     -2
15/03/2012 13:30:00      7
25/03/2012 20:30:00      1

in the x-axis I want to label only the month and the day (e.g. Jan 02 ) rotating of 45 degree. How can I do this using the command plot()?

like image 706
user2287830 Avatar asked Sep 12 '13 14:09

user2287830


2 Answers

Using base graphics:

tab <- read.table(textConnection('
"02/01/2012 13:30:00"     10
"10/01/2012 20:30:00"      8
"15/01/2012 13:30:00"     12
"25/01/2012 20:30:00"      6
"02/02/2012 13:30:00"      5
"10/02/2012 20:30:00"      3
"15/02/2012 13:30:00"      6
"25/02/2012 20:30:00"     -1
"02/03/2012 13:30:00"      4
"10/03/2012 20:30:00"     -2
"15/03/2012 13:30:00"      7
"25/03/2012 20:30:00"      1'))
tab[, 1] <- as.POSIXct(tab[, 1], format = "%d/%m/%Y %H:%M:%S")

plot(V2 ~ V1, data=tab, xaxt="n")
tck <- axis(1, labels=FALSE)
labels <- format(as.POSIXct(tck, origin="1970-01-01"), "%b %d")
text(tck, par("usr")[3], labels=labels, srt=315,
     xpd=TRUE, adj=c(-0.2,1.2), cex=0.9)

R plot

like image 145
rcs Avatar answered Sep 21 '22 02:09

rcs


with ggplot:

tab <- read.table(textConnection("
02/01/2012 13:30:00     10 
10/01/2012 20:30:00      8  
15/01/2012 13:30:00     12
25/01/2012 20:30:00      6
02/02/2012 13:30:00      5
10/02/2012 20:30:00      3
15/02/2012 13:30:00      6
25/02/2012 20:30:00     -1
02/03/2012 13:30:00      4
10/03/2012 20:30:00     -2
15/03/2012 13:30:00      7
25/03/2012 20:30:00      1"))



tab[, 1] <- as.Date(tab[, 1], format = "%d/%m/%Y")

library(ggplot2)
ggplot(tab) + geom_point(aes(x = V1, y = V3)) + 
        theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

like image 35
adibender Avatar answered Sep 21 '22 02:09

adibender