Developers Club geek daily blog

1 year, 7 months ago
In the first part:
  • visualization of networks: what for? how?
  • visualization parameters
  • best practices — an esthetics and performance
  • formats of data and preparation
  • the description of data sets which are used in examples
  • beginning of work with igraph

In this part: colors and fonts in diagrams R.

Short introduction of I: colors in diagrams R


Colors — it is beautiful, but the fact that they help to distinguish object types, gradation of property is far more important. In the majority of functions R it is possible to use names of flowers, RGB or hexadecimal values. In simple basic diagram R below x and y — coordinates of points, pch — the character for designation of points, cex — point size and col — color. To learn what parameters for creation of diagrams in R happen, execute command ?par.
plot(x=1:10, y=rep(5,10), pch=19, cex=3, col="dark red")
points(x=1:10, y=rep(6, 10), pch=19, cex=3, col="557799")
points(x=1:10, y=rep(4, 10), pch=19, cex=3, col=rgb(.25, .5, .3))

Visualization of static and dynamic networks on R, part 2

Apparently, RGB varies from 0 to 1 here. It is default setting for R, but it is possible to set also the range from 0 to 255, using command rgb(10, 100, 100, maxColorValue=255).

It is possible to set transparency of an element, using parameter alpha (from 0 to 1):
plot(x=1:5, y=rep(5,5), pch=19, cex=12, col=rgb(.25, .5, .3, alpha=.5), xlim=c(0,6))  

Visualization of static and dynamic networks on R, part 2

If hexadecimal representation of color is used, it is possible to set transparency coefficient, using adjustcolor from a packet grDevices. For the sake of interest we will also paint a diagram background in gray by means of function par() for installation of settings of graphics.
par(bg="gray40")
col.tr <- grDevices::adjustcolor("557799", alpha=0.7)
plot(x=1:5, y=rep(5,5), pch=19, cex=12, col=col.tr, xlim=c(0,6)) 

Visualization of static and dynamic networks on R, part 2

If you are going to use the built-in names of flowers, here is how it is possible to receive them all:
colors()                          # List all named colors
grep("blue", colors(), value=T)   # Colors that have "blue" in the name

In most cases it is necessary for us or several contrast colors, or shades of one color. In R there is embedded function of a palette which can generate it. For example:
pal1 <- heat.colors(5, alpha=1)   #  5 colors from the heat palette, opaque
pal2 <- rainbow(5, alpha=.5)      #  5 colors from the heat palette, transparent
plot(x=1:10, y=1:10, pch=19, cex=5, col=pal1)

Visualization of static and dynamic networks on R, part 2

plot(x=1:10, y=1:10, pch=19, cex=5, col=pal2)

Visualization of static and dynamic networks on R, part 2

We can also create own gradients with the help colorRampPalette. Pay attention that colorRampPalette returns function which can be used for generation of so many flowers from this palette how many it is necessary.
palf <- colorRampPalette(c("gray80", "dark red")) 
plot(x=10:1, y=1:10, pch=19, cex=5, col=palf(10))

Visualization of static and dynamic networks on R, part 2

To add transparency in colorRampPalette, it is necessary to use parameter alpha=TRUE:
palf <- colorRampPalette(c(rgb(1,1,1, .2),rgb(.8,0,0, .7)), alpha=TRUE)
plot(x=10:1, y=1:10, pch=19, cex=5, col=palf(10)) 

Visualization of static and dynamic networks on R, part 2

Finding of a good combination of flowers — the difficult task, at the same time the built-in palettes R are quite limited. Fortunately, this problem is solved also by other packets:
# If you don't have R ColorBrewer already, you will need to install it:
install.packages("RColorBrewer")
library(RColorBrewer)
display.brewer.all()

In this packet one main function — brewer.pal. To use, it is only necessary to select a necessary palette and amount of colors. Let's look at some palettes RColorBrewer:
display.brewer.pal(8, "Set3")

Visualization of static and dynamic networks on R, part 2

display.brewer.pal(8, "Spectral")

Visualization of static and dynamic networks on R, part 2

display.brewer.pal(8, "Blues")

Visualization of static and dynamic networks on R, part 2

Use of palettes RColorBrewer in graphs:
pal3 <- brewer.pal(10, "Set3") 
plot(x=10:1, y=10:1, pch=19, cex=4, col=pal3)

Visualization of static and dynamic networks on R, part 2

Short introduction of II: fonts in diagrams R


Use of different fonts in diagrams R can demand certain efforts. It is especially actual for users of Windows, users of Mac and Linux, most likely, can pass this section safely.

To import fonts from OS to R, we will use a packet extrafont:
install.packages("extrafont")
library(extrafont)

# Import system fonts - may take a while.
font_import() 
fonts() # See what font families are available to you now.
loadfonts(device = "win") # use device = "pdf" for pdf plot output. 

Now fonts are available, and it is possible to do something like that:
library(extrafont)
plot(net, vertex.size=30)
plot(net, vertex.size=30, vertex.label.family="Arial Black" )

Visualization of static and dynamic networks on R, part 2

When you save diagrams as PDF-files, too it is possible to set fonts:
# First you may have to let R know where to find ghostscript on your machine:
Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.10/bin/gswin64c.exe")

# pdf() will send all the plots we output before dev.off() to a pdf file: 
pdf(file="ArialBlack.pdf")
plot(net, vertex.size=30, vertex.label.family="Arial Black" )
dev.off()

embed_fonts("ArialBlack.pdf", outfile="ArialBlack_embed.pdf")

This article is a translation of the original post at habrahabr.ru/post/263947/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: sysmagazine.com@gmail.com.

We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.

comments powered by Disqus