Data visualization in R: Plot Types

R offers nice graphic functions to visualize you data. Within this article I want to show you the basic and therefore very often used possibility to create a graphic. Furthermore you will learn a first parameter to adapt the presentation output to your needs. In further articles I want to introduce a lot more parameters and show the ways to customize the data visualization.

 
The data plot

The plot function is the main function for graphics. It supports data representation for single points, a single vector, a pair of vectors and many other R objects.

There are many other graphic functions which are specific to some tasks. Most of them take the same arguments as the plot function.

 
My first plot

The R visualization functionality should be shown based on the correlation between the size and population of countries. The data in this example is based on real data with size in thousand square kilometers and population in thousand inhabitants.

To show a data plot we have to create the two data vectors and call the plot function with the vectors as parameters.

size <- c(10,29,33,45,51,56,70,103,110,132,146,238,243,301,338,357,543)

population <- c(1806,3162,11142,1339,3834,4267,4589,320,7305,11280,480,21327,63228,60918,5414,81890,65697)

plot(size, population)

 
This will create the following scatter plot.

R06_a

 
It is also possible to write the function in the following format:

plot(population~size)

 
This will create the same graphic but there is a difference in the data interpretation. Instead of two vectors we now have the data as formula where the population is a function of the size.

 
Plot types

You may easily change the data presentation by setting the plot type. The following types are supported:

  • ’n‘ for none: Nothing is printed. This could be useful if you want to print the plot layout and add some content later on, with additional functions like ‚line‘.
  • ‚p‘ for points: To create a scatter plot.
  • ‚l‘ for lines
  • ‚b‘ for both: Lines and points.
  • ‚o‘ for both overlayed: Lines and points, where the lines are drawn through the points
  • ‚h‘ for histogram
  • ’s‘ and ‚S‘ for steps

 
The following example will create all plot types within one graphic. The function par is used to create this multi-paneled plotting window. Furthermore the parameter main is set to add a title to each plot.

par(mfrow = c(2,4))
plot(size, population, type = ’n‘, main = ’none‘)
plot(size, population, type = ‚p‘, main = ‚points‘)
plot(size, population, type = ‚l‘, main = ‚lines‘)
plot(size, population, type = ‚b‘, main = ‚both‘)
plot(size, population, type = ‚o‘, main = ‚both overlayed‘)
plot(size, population, type = ‚h‘, main = ‚histogram‘)
plot(size, population, type = ’s‘, main = ’steps (long)‘)
plot(size, population, type = ‚S‘, main = ’steps (short)‘)

 
R06_b

 
Summary

By using the plot function and define the plot type, you are able to create some basic data graphics. Within the next articles I want to show you some more possibilities to customize the data plot.

Advertisements
Dieser Beitrag wurde unter .NET, C#, R veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s