简体   繁体   中英

How can I plot x axis values from a .csv file?

I have date1 column which contains a date row. This row contains date in format dd/mm/yyyy

To plot the date values directly I'm using :

axis(1, at=1:5, lab=c("01/01/2013" , "01/02/2013" , "01/03/2013" , 
, "01/04/2013" , , "01/05/2013"))

To read the values from a .csv file I require something like ? : axis(1, at=1:5, lab=c(date1$date)) I think I need to remove the 'at' parameter as the number of dates is dynamic.

Here is the entire code :

g_range <- range(0, date1$links)
plot(date1$links, type="o", col="blue", ylim=g_range, 
   axes=FALSE, ann=FALSE)
axis(1, at=1:5, lab=c("01/01/2013" , "01/02/2013" , "01/03/2013" , 
, "01/04/2013" , , "01/05/2013"))
box()
title(main="Additions", col.main="red", font.main=4)
axis(2, las=1, at=50*0:g_range[2])

This is the format of the file that is being read :

value , links 
18/03/2013,100 
19/03/2013,200 
20/03/2013,300 
21/03/2013,400 
22/03/2013,500

I'm currently reading the links column, but want to update the code to also read the date instead as now the date values are being set within the r code itself. By dynamic I mean the .csv file can contain a variable list of date & links values, so in above example there are 5 value/links pairs but there could be 6 or 7 or 8 etc...

It is not clear what do you want to do , wince you don't give a reproducible example, and it is not clear what is dynamic. I think , You can use the function axis.Date() take an object containing dates and produce an axis with appropriate labels.

here an example:

在此处输入图片说明

I create some dummy plot...

random.dates <- seq(as.Date("01/01/2013",format='%d/%m/%Y'),
                    as.Date("01/05/2013",format='%d/%m/%Y'),length.out=50)
plot(random.dates, 1:50, xaxt="n",main='pretty axes dates',type='o')

Then I coerce your vector of string to aa vector of dates

at =as.Date(c("01/01/2013" , "01/02/2013" , "01/03/2013"  , 
              "01/04/2013" ,  "01/05/2013"),format='%d/%m/%Y')

Note in the call of axis.Date the argument format is not the same that the one used to coerce string to dates...

axis.Date(1,at=at,format='%d/%m/%Y')

EDIT*

Ho to read a csv file:

dat <- read.table(text='value , links 
18/03/2013,100 
19/03/2013,200 
20/03/2013,300 
21/03/2013,400 
22/03/2013,500',header=TRUE,sep=',')


dat$value <- as.Date(dat$value,format='%d/%m/%Y')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM