Linux Format - UK (2020-03)

(Antfer) #1
http://www.techradar.com/pro/linux March 2020 LXF260 95

Dates & time CODING ACADEMY


weekend. All the other days of the month are
characterised as weekday. The scale_fill_manual(values
=c(“weekday”=”darkgray”, “weekend”=”darkgreen”))
statement makes sure that weekend and weekday days
are coloured differently.

Customising your calendar
Now it’s time to print dates on the calendar using the
code of dates.R. The most important part of dates.R is
the following:
dates <- data.frame(date=seq(as.Date(‘2020-01-01’),as.
Date(‘2020-04-30’),by=1))
dates$month <- factor(strftime(dates$date,format=”%B
”),levels=c(“January”,“February”,“March”,“April”))
The dates variable is where you define the range of
dates that is going to be included in the output, whereas
the dates$month column is where you define the
names of the months that interest you.
Figure 4 (see page opposite) shows the output of the
previous commands. Although the generated output
here is much more impressive than the output that’s
shown in Figure 3, you will need to type more data and
make more modifications to the R code. However, if all
that you need is a simple calendar, then you will only
need to change the definition of the dates and
dates$month variables. Note that dates.R uses the
ggplot2 R package only.

Python heat maps
A heat map is a smart way of visualising a table of
numbers, where you substitute the real values with
coloured cells. You can easily understand a heat map if
you think of it as a table or spreadsheet that contains
colours instead of numbers.
Let’s look at a Python 3 script that creates a calendar
heat map. The Python 3 code is saved in heatMap.py.
The most important Python 3 code is the following:
I = 1.1 - np.cos(X.ravel()) + np.random.normal(0,.2, X.
size)
calmap(ax, 2020, I.reshape(53,7).T)
I = 1.1 - np.cos(X.ravel()) + np.random.normal(0,.2, X.
size)

calmap(ax, 2021, I.reshape(53,7).T)
Each calmap() entry corresponds to a year that is
going to be plotted. The I variable contains random
data that is going to be plotted as a heat map. Each year
has its own definition of the I variable.
Figure 5 (see below) shows the graphical output of
heatMap.py. Note that as heatMap.py uses randomly
generated data, which means that you will get a
different output each time you execute it.
In the files that come with this tutorial you are going
to find one named heatMap.R, which illustrates how you
can create a heat map in a R calendar. However, the R
implementation of the calendar heat map is much more
complex than the Python 3 version. Always use the best
tool for the job.
What you should keep from this tutorial – apart from
the fact that R is not just about statistics – is the
presented R scripts and R code, which you can take
away, modify and experiment with to produce the kind
of calendars you want.

POP US IN YOUR CALENDAR Subscribe now at http://bit.ly/LinuxFormat


Figure 5: Generated by the heatMap.py Python 3 script. Although R can also generate calendar
heat maps, the Python 3 code is much simpler.

Figure 6: This shows a small part of the output of the install.
packages(“tidyverse”) command. As tidyverse is in reality a meta
package, it will install lots of R packages, including ggplot2.

ABOUT R PACKAGES


Although R is a very capable programming language, it can be
enhanced using code from other sources. R uses the term Packages
to describe R code that gives you extra functionality.
There are two ways of installing an R package. If the package is
available on CRAN (The Comprehensive R Archive Network), then you
can install it using the install.packages() function. Otherwise, you
should download it on your Linux machine before installing it using
the same install.packages() function, but with different parameters. If
you execute the library() function without any arguments, you will get
a list of all installed packages. To get a more detailed output of all the
installed R packages, you can also use the installed.packages()
command. The update.packages() command will try to update all
installed CRAN packages to the latest version.
Some of the most famous R packages are: ggplot2 (https://
ggplot2.tidyverse.org), which can help you create impressive plots;
lubridate, which help you work with dates and times; ggcal (https://
github.com/jayjacobs/ggcal), which is used in this tutorial for
creating calendars; and dplyr, which is for working with data frame-
like objects. Additionally, there is tidyverse (www.tidyverse.org),
which is a collection of R packages designed for data science that also
includes ggplot2.
Figure 6 (see below) shows part of the output of install.packages().
Note that when using a Docker image, all packages are being installed
in that running container and will be lost the next time you restart it.

9992March 0 h2rexplhepinsow March 2020 LXF260 95


Dates & time CODING ACADEMY


weekend. All the other days of the month are
characterised as weekday. The scale_fill_manual(values
=c(“weekday”=”darkgray”, “weekend”=”darkgreen”))
statement makes sure that weekend and weekday days
are coloured differently.


Customising your calendar
Now it’s time to print dates on the calendar using the
code of dates.R. The most important part of dates.R is
the following:
dates <- data.frame(date=seq(as.Date(‘2020-01-01’),as.
Date(‘2020-04-30’),by=1))
dates$month <- factor(strftime(dates$date,format=”%B
”),levels=c(“January”,“February”,“March”,“April”))
The dates variable is where you define the range of
dates that is going to be included in the output, whereas
the dates$month column is where you define the
names of the months that interest you.
Figure 4 (see page opposite) shows the output of the
previous commands. Although the generated output
here is much more impressive than the output that’s
shown in Figure 3, you will need to type more data and
make more modifications to the R code. However, if all
that you need is a simple calendar, then you will only
need to change the definition of the dates and
dates$month variables. Note that dates.R uses the
ggplot2 R package only.


Python heat maps
A heat map is a smart way of visualising a table of
numbers, where you substitute the real values with
coloured cells. You can easily understand a heat map if
you think of it as a table or spreadsheet that contains
colours instead of numbers.
Let’s look at a Python 3 script that creates a calendar
heat map. The Python 3 code is saved in heatMap.py.
The most important Python 3 code is the following:
I = 1.1 - np.cos(X.ravel()) + np.random.normal(0,.2, X.
size)
calmap(ax, 2020, I.reshape(53,7).T)
I = 1.1 - np.cos(X.ravel()) + np.random.normal(0,.2, X.
size)


calmap(ax, 2021, I.reshape(53,7).T)
Each calmap() entry corresponds to a year that is
going to be plotted. The I variable contains random
data that is going to be plotted as a heat map. Each year
has its own definition of the I variable.
Figure 5 (see below) shows the graphical output of
heatMap.py. Note that as heatMap.py uses randomly
generated data, which means that you will get a
different output each time you execute it.
In the files that come with this tutorial you are going
to find one named heatMap.R, which illustrates how you
can create a heat map in a R calendar. However, the R
implementation of the calendar heat map is much more
complex than the Python 3 version. Always use the best
tool for the job.
What you should keep from this tutorial – apart from
the fact that R is not just about statistics – is the
presented R scripts and R code, which you can take
away, modify and experiment with to produce the kind
of calendars you want.

POP US IN YOUR CALENDAR Subscribe now at http://bit.ly/LinuxFormat


Figure 5: Generated by the heatMap.py Python 3 script. Although R can also generate calendar
heat maps, the Python 3 code is much simpler.

Figure 6: This shows a small part of the output of the install.
packages(“tidyverse”) command. As tidyverse is in reality a meta
package, it will install lots of R packages, including ggplot2.

ABOUTR PACKAGES


Although R is a very capable programming language, it can be
enhanced using code from other sources. R uses the term Packages
to describe R code that gives you extra functionality.
There are two ways of installing an R package. If the package is
available on CRAN (The Comprehensive R Archive Network), then you
can install it using the install.packages() function. Otherwise, you
should download it on your Linux machine before installing it using
the same install.packages() function, but with different parameters. If
you execute the library() function without any arguments, you will get
a list of all installed packages. To get a more detailed output of all the
installed R packages, you can also use the installed.packages()
command. The update.packages() command will try to update all
installed CRAN packages to the latest version.
Some of the most famous R packages are: ggplot2 (https://
ggplot2.tidyverse.org), which can help you create impressive plots;
lubridate, which help you work with dates and times; ggcal (https://
github.com/jayjacobs/ggcal), which is used in this tutorial for
creating calendars; and dplyr, which is for working with data frame-
like objects. Additionally, there is tidyverse (www.tidyverse.org),
which is a collection of R packages designed for data science that also
includes ggplot2.
Figure 6 (see below) shows part of the output of install.packages().
Note that when using a Docker image, all packages are being installed
in that running container and will be lost the next time you restart it.
Free download pdf