4  Podstawy grafiki w środowisku R

4.1 Funkcja plot

Code
plot(rivers)

Code
plot(cars, main='Prędkość i odległość do zatrzymania samochodu',xlab='Prędkość', ylab='Odległość')

Code
plot(iris[-5],col=iris[,5])

Code
plot(chickwts$weight~chickwts$feed)

Code
plot(AirPassengers)

Code
plot(Titanic)

Code
plot(Titanic,shade=TRUE)

Code
library(TeachingDemos)
plot(dice(12,4))

4.2 Histogram

Code
hist(rivers, main = '')

Code
hist(rivers,breaks=c(0,400,800,1200,2000,4000),xlab='Długość w milach',ylab='Gęstość',main='')

4.3 Wykres punktowy

Code
par(mfrow=c(2,1))
dotchart(VADeaths)
dotchart(t(VADeaths))

4.4 Wykres słupkowy

Code
par(mfrow=c(2,1))
barplot(VADeaths,
         col = c("blue", "lightblue", "lightcyan", 
                 "lavender", "pink"),
        legend = rownames(VADeaths))
barplot(VADeaths,
         col = c("blue", "lightblue", "lightcyan", 
                 "lavender", "pink"),
        legend = rownames(VADeaths), beside = TRUE)

4.5 Wykres kołowy

Code
pie(table(mtcars$gear),labels=c('3 biegi','4 biegi', '5 biegów'),main='')

4.6 Układ wykresów

Code
# Umieszczenie kilku wykresów w jednym obszarze graficznym.
library(ggplot2)
par(mfrow=c(4,1))
plot(price ~ carat, data = diamonds,  xlab = "Waga (karaty)", ylab = "Cena", col = "darkblue", pch = 20)
hist(diamonds$price, breaks = 30, col = "skyblue",  main='', xlab = "Cena", ylab = "Liczba diamentów")
boxplot(price ~cut , data = diamonds,  xlab = "Typ szlifu", ylab = "Cena", col = c("#FF9999", "#FFCC99", "#FFFF99", "#CCFF99", "#99FF99"))
avg_price <- tapply(diamonds$price, diamonds$color, mean)
barplot(avg_price,  xlab = "Kolor", ylab = "Średnia cena", ylim=c(0,7000),col = viridis::viridis(length(avg_price)))
legend("topleft", legend = names(avg_price), fill = viridis::viridis(length(avg_price)))

4.7 Palety kolorystyczne

Code
paleta<- heat.colors(5)     # paleta z 5 kolorami
x <- seq(0, 2*pi, length.out=100)
y <- seq(0, 2*pi, length.out=100)
z <- outer(x, y, function(x, y) sin(x)*sin(y))
heatmap(z, Colv=NA, Rowv=NA, col=paleta, scale="none", xlab="", ylab="", labRow = FALSE, labCol = FALSE)

Code
paleta<- topo.colors(8)     # paleta z 8 kolorami
x <- seq(0, 2*pi, length.out=100)
y <- seq(0, 2*pi, length.out=100)
z <- outer(x, y, function(x, y) sin(x)*sin(y))
heatmap(z, Colv=NA, Rowv=NA, col=paleta, scale="none", xlab="", ylab="", labRow = FALSE, labCol = FALSE)

Code
paleta<- cm.colors(8)   # paleta z 8 kolorami
x <- seq(0, 2*pi, length.out=100)
y <- seq(0, 2*pi, length.out=100)
z <- outer(x, y, function(x, y) sin(x)*sin(y))
heatmap(z, Colv=NA, Rowv=NA, col=paleta, scale="none", xlab="", ylab="", labRow = FALSE, labCol = FALSE)

Code
paleta<- terrain.colors(6)  # paleta z 6 kolorami
x <- seq(0, 2*pi, length.out=100)
y <- seq(0, 2*pi, length.out=100)
z <- outer(x, y, function(x, y) sin(x)*sin(y))
heatmap(z, Colv=NA, Rowv=NA, col=paleta, scale="none", xlab="", ylab="", labRow = FALSE, labCol = FALSE)

Code
paleta<- rainbow(20)    
x <- seq(0, 8*pi, length.out=100)
y <- seq(0, 8*pi, length.out=100)
z <- outer(x, y, function(x, y) sin(x)*sin(y))
heatmap(z, Colv=NA, Rowv=NA, col=paleta, scale="none", xlab="", ylab="", labRow = FALSE, labCol = FALSE)

RColorBrewer

Code
library(RColorBrewer)
x <- seq(0, 2*pi, length.out=100)
y <- seq(0, 2*pi, length.out=100)
z <- outer(x, y, function(x, y) sin(x)*sin(y))
palette <- brewer.pal(9, "RdYlBu")
heatmap(z, Colv=NA, Rowv=NA, col=palette, scale="none", xlab="", ylab="", labRow = FALSE, labCol = FALSE)

viridis

Code
library(viridis)
x <- y <- seq(-2*pi, 2*pi, len = 80)
r <- sqrt(outer(2*x^2, y^2, "+"))
filled.contour(sin(r^2)*exp(-r/(68*pi)), 
               axes=FALSE,
               color.palette=viridis,
               asp=1)

wesanderson

Code
library(wesanderson)
x <- seq(0, 4*pi, length.out=100)
y <- seq(0, 4*pi, length.out=100)
z <- outer(x, y, function(x, y) sin(x)*sin(y))
palette <- wes_palette("GrandBudapest1", n = 4)
heatmap(z, Colv=NA, Rowv=NA, col=palette, scale="none", xlab="", ylab="", labRow = FALSE, labCol = FALSE)

colorspace

Code
library(colorspace)
x <- seq(0, 4*pi, length.out=100)
y <- seq(0, 4*pi, length.out=100)
z <- outer(x, y, function(x, y) sin(x)*sin(y))
palette <- sequential_hcl(20, palette = "Blues")
heatmap(z, Colv=NA, Rowv=NA, col=palette, scale="none", xlab="", ylab="", labRow = FALSE, labCol = FALSE)