[R] [R studio] Plotting of line chart for each columns at 1 page
Jim Lemon
drjimlemon @ending from gm@il@com
Tue Dec 18 02:56:20 CET 2018
Hi Subhamitra,
As for the error that you mention, it was probably:
Error in axis(1, at = year_mids, labels = 3 - 1 - 1994:3 - 8 - 2017) :
'at' and 'labels' lengths differ, 24 != 1992
Anything more than a passing glance reveals that you didn't read the
explanation I sent about the arguments passed to the "axis" function.
Perhaps it will be rewarding to read the help page for the "axis" function
in the "graphics" package.
Your confusion about the logic (really simple arithmetic) of assigning
positions for the year labels may be allayed by the following. Think back
to those grade school problems that read:
"If I have m apples to give to n people, how many must I give each person
so that all will receive the same number and I will have the fewest apples
left?"
I'm sure that you remember that this can be solved in a number of ways. You
can divide m/n and drop the remainder. So, from 03-01-2002 to 03-08-2017 in
EMs2.1:
diff(as.Date(c("03-01-2002","03-08-2017"),"%d-%m-%Y"))
Time difference of 5691 days
# plus 1 for all of the days included
# calculate the number of years
5692/365.25
[1] 15.58385
So if there had been an observation each day, you would have the trivial
task of dividing the number of days by the number of years to get the tick
increments:
5692/15.58385
365.2499
Of course you don't have that many observations and you are trying to get
the number of observations, not days, in each year. By making the
assumption that the missing observations are spread evenly over the years,
you can simply replace the number of days with the number of observations.
At the moment I don't have that as I unrared your data at home. But you do
have it and I will call it nobs:
# this calculates the number of observations per year
nobs/15.58385
<obs_per_year>
will yield the number of observations in each year. So you have your tick
increments. Now for the offset. If you want the year ticks to appear at the
middle of each year, you will want to start at 182 minus the two days
missing in January or 180. So your new year_mids will be:
year_mids<-seq(180,nobs,obs_per_year)
Your years are 2002:2017 for EMs2.1, so:
axis(1,year_mids,2002:2017)
may well be what you want for axis ticks. As you can see, the "m apples to
n people" approach gives you the answer. The only missing part was the
offset, or where to start handing out apples. You might want to have
another look at the help pages for "axis" and "seq" (or ":") which will
show you why your axis command failed badly. Good luck.
Jim
On Mon, Dec 17, 2018 at 6:12 PM Subhamitra Patra <subhamitra.patra using gmail.com>
wrote:
> Hello Sir,
>
> Thank you very much for your excellent guidance to a new R learner.
>
> I tried with your suggested code and got the expected results, but for the
> 2 CSV files (i.e. EMs2.1. and EMs.3.1), the date column is not coming in
> the X-axis (shown in the last row of the attached result Pdf file). I
> think I need to increase more or less than 229 in the year-mids because for
> both the CSV files, starting date is 03-01-2002 and 04-07-2001
> (date-month-year) for EMs 2.1. and EMs 3.1. respectively. *Sir, hence I
> am quite confused for the logic behind the fixing of year_mids*. For your
> convenience, I am attaching both the code and result file.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list