Jazyk R a knihovna Lattice

Inicializace knihovny a zobrazení nápovědy

In [1]:
library(package = "lattice")
In [2]:
help(lattice)

Grafy v knihovně Lattice

Jednoduchý graf typu x-y

In [5]:
input <- mtcars[,c('wt','mpg')]
 
xyplot(input$wt~input$mpg)

Rozdělení hodnot podle zvolené vlastnosti

In [6]:
library(lattice)
 
input <- mtcars[,c('wt','mpg','gear')]
 
xyplot(wt~mpg,
       data = input,
       group=gear)

Přidání legendy do grafu

In [8]:
input <- mtcars[,c('wt','mpg','gear')]
 
xyplot(wt~mpg,
       data = input,
       group=gear,
       auto.key=TRUE)

Vylepšení popisku grafu

In [9]:
input <- mtcars[,c('wt','mpg','gear')]
 
xyplot(wt~mpg,
       data = input,
       group=gear,
       auto.key=TRUE,
       xlab = "Miles/(US) gallons",
       ylab = "Weight (1000lbs)")

Regresní přímky a interpolace hodnotami

In [10]:
input <- mtcars[,c('wt','mpg','gear')]
 
xyplot(wt~mpg,
       data = input,
       type = c("p", "r"),
       xlab = "Miles/(US) gallons",
       ylab = "Weight (1000lbs)")
In [11]:
input <- mtcars[,c('wt','mpg','gear')]
 
xyplot(wt~mpg,
       data = input,
       type = c("p", "smooth"),
       xlab = "Miles/(US) gallons",
       ylab = "Weight (1000lbs)")

Mřížka v grafech, regresní přímky či interpolace při rozdělení hodnot do skupin

In [12]:
input <- mtcars[,c('wt','mpg','gear')]
 
xyplot(wt~mpg,
       data = input,
       type = c("p", "g", "smooth"),
       xlab = "Miles/(US) gallons",
       ylab = "Weight (1000lbs)")
In [13]:
input <- mtcars[,c('wt','mpg','gear')]
 
xyplot(wt~mpg,
       data = input,
       group=gear,
       auto.key=TRUE,
       type = c("p", "r"),
       xlab = "Miles/(US) gallons",
       ylab = "Weight (1000lbs)")
In [15]:
input <- mtcars[,c('wt','mpg','gear')]
 
xyplot(wt~mpg,
       data = input,
       group = gear,
       auto.key = TRUE,
       type = c("p", "smooth"),
       xlab = "Miles/(US) gallons",
       ylab = "Weight (1000lbs)")

Samostatné grafy řízené třetí veličinou

In [17]:
input <- mtcars[,c('wt','mpg','gear')]
 
xyplot(wt~mpg | gear,
       data = input,
       type = c("p", "r"),
       xlab = "Miles/(US) gallons",
       ylab = "Weight (1000lbs)")

Změny uspořádání grafů v mřížce

In [18]:
data("iris")
head(iris)
 
input <- iris[,c('Sepal.Length','Sepal.Width','Petal.Length','Petal.Width','Species')]
 
xyplot(Sepal.Length ~ Petal.Length | Species,
       group = Species,
       data = input,
       type = c("p", "smooth"),
       scales = "free")
A data.frame: 6 × 5
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
<dbl><dbl><dbl><dbl><fct>
15.13.51.40.2setosa
24.93.01.40.2setosa
34.73.21.30.2setosa
44.63.11.50.2setosa
55.03.61.40.2setosa
65.43.91.70.4setosa
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“pseudoinverse used at 1.5”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“neighborhood radius 0.1”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“reciprocal condition number  0”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“pseudoinverse used at 1.5”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“neighborhood radius 0.1”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“reciprocal condition number  0”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“pseudoinverse used at 1.5”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“neighborhood radius 0.1”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“reciprocal condition number  0”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“pseudoinverse used at 1.5”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“neighborhood radius 0.1”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“reciprocal condition number  0”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“pseudoinverse used at 1.5”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“neighborhood radius 0.1”
Warning message in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
“reciprocal condition number  0”

Zvýraznění výšek barvou (výšková mapa)

In [19]:
x <- seq(1, 2*pi, length.out=20)
y <- seq(1, 2*pi, length.out=20)
 
r1 <- as.vector(sqrt(outer(x^2, y^2, "+")))
r2 <- as.vector(sqrt(outer((x-3)^2, (y-3)^2, "+")))
 
grid <- expand.grid(x=x, y=y)
grid$z <- sin(r1) + sin(r2)
 
levelplot(z ~ x*y,
          data=grid,
          xlab="X",
          main="")

Modifikace barvové palety výškové mapy

In [20]:
x <- seq(1, 2*pi, length.out=200)
y <- seq(1, 2*pi, length.out=200)
 
r1 <- as.vector(sqrt(outer(x^2, y^2, "+")))
r2 <- as.vector(sqrt(outer((x-3)^2, (y-3)^2, "+")))
 
grid <- expand.grid(x=x, y=y)
grid$z <- sin(r1) + sin(r2)
 
levelplot(z ~ x*y,
          data=grid,
          xlab="",
          ylab="",
          main="",
          col.regions = terrain.colors(100))

Přidání kontur (vrstevnic) do výškové mapy

In [21]:
x <- seq(1.0, 2*pi, length.out=200)
y <- seq(1.0, 2*pi, length.out=200)
 
r1 <- as.vector(sqrt(outer(x^2, y^2, "+")))
r2 <- as.vector(sqrt(outer((x-3)^2, (y-3)^2, "+")))
 
grid <- expand.grid(x=x, y=y)
grid$z <- sin(r1) + sin(r2)
 
levelplot(z ~ x*y,
          data=grid,
          xlab="",
          ylab="",
          main="",
          contour=TRUE,
          col.regions = terrain.colors(20000))

Skutečný graf s konturami

In [22]:
x <- seq(1.0, 2*pi, length.out=200)
y <- seq(1.0, 2*pi, length.out=200)
 
r1 <- as.vector(sqrt(outer(x^2, y^2, "+")))
r2 <- as.vector(sqrt(outer((x-3)^2, (y-3)^2, "+")))
 
grid <- expand.grid(x=x, y=y)
grid$z <- sin(r1) + sin(r2)
 
contourplot(z ~ x * y,
            grid,
            col.regions = topo.colors(20),
            at = c(-Inf, seq(-0.8, 0.8, by = 0.2), Inf))
In [23]:
x <- seq(1.0, 2*pi, length.out=200)
y <- seq(1.0, 2*pi, length.out=200)
 
r1 <- as.vector(sqrt(outer(x^2, y^2, "+")))
r2 <- as.vector(sqrt(outer((x-3)^2, (y-3)^2, "+")))
 
grid <- expand.grid(x=x, y=y)
grid$z <- sin(r1) + sin(r2)
 
contourplot(z ~ x * y, grid, col.regions = topo.colors(20),
          at = c(-Inf, seq(-2.0, 2.0, by = 0.1), Inf))

Nastavení

In [24]:
show.settings()
In [ ]: