Kody z rozdziału 5. Graficzna prezentacja danych ,,Przewodnika po programie R’’ wydanie 4.
Aby zainstalować i włączyć pakiet Przewodnik
wykonaj poniższe dwie liniki.
devtools::install_github("pbiecek/PrzewodnikPakiet")
library("Przewodnik")
library("PBImisc")
plot(MDRD12~MDRD7, data = kidney)
library("lattice")
xyplot(MDRD12~MDRD7, data = kidney)
library("ggplot2")
qplot(MDRD7, MDRD12, data = kidney)
library("PBImisc")
library("lattice")
xyplot(MDRD12 ~ MDRD7 | discrepancy.DR, data = kidney)
xyplot(MDRD12 ~ MDRD7 | discrepancy.DR, data = kidney, type=c("p","smooth","r"), col="grey", pch=16, ylab="MDRD 30d", xlab="MDRD 7d")
histogram(~MDRD12 | therapy, data = kidney)
histogram(~MDRD12 | equal.count(donor.age,4), data = kidney)
densityplot(~MDRD12, group = therapy, data = kidney,
plot.points = FALSE)
densityplot(~MDRD12, group=therapy, auto.key = TRUE, data = kidney)
densityplot(~MDRD12, group=therapy, data = kidney, auto.key = list(space = "right", columns = 1))
xyplot(MDRD12 + MDRD36 ~ MDRD7 | discrepancy.DR==0, data=kidney, type=c("p","smooth","g"), cex=0.5, auto.key = TRUE)
splom(kidney[,c(9,11,13,15)], type=c("smooth","p"), pch='.')
stripplot(factor(discrepancy.AB)~MDRD7, data = kidney, jitter.data = TRUE, alpha = 0.5)
discrepancy <- equal.count(kidney$discrepancy.AB, number=3)
bwplot(therapy~MDRD12|discrepancy, data=kidney, varwidth = TRUE)
library("Przewodnik")
(wPlec <- table(daneSoc$wyksztalcenie, daneSoc$plec))
##
## kobieta mezczyzna
## podstawowe 22 71
## srednie 16 39
## wyzsze 10 24
## zawodowe 7 15
dotplot(wPlec, groups=FALSE, origin=0, type = c("p","h"))
dotplot(wPlec, type="o", auto.key = list(space="right"))
attach(daneSoc)
tabela <- as.data.frame(table(wyksztalcenie, plec, praca ))
barchart(wyksztalcenie~Freq|plec, groups=praca, auto.key=TRUE, data=tabela)
parallel(~kidney[,c(9:16)], groups=MDRD7<30, data=kidney, alpha=0.2, horizontal.axis = FALSE, scales = list(x = list(rot = 90)))
histogram(~MDRD7 | therapy, data = kidney)
densityplot(~MDRD7 | factor(diabetes), groups=therapy, data=kidney, bw = 8, plot.points="rug", auto.key = TRUE)
library("latticeExtra")
ecdfplot(~MDRD7 | factor(diabetes), groups=therapy, data=kidney, auto.key=list(space="right"))
qq(diabetes ~ MDRD7 | therapy, distribution=qnorm, data=kidney)
qqmath( ~ MDRD7 | factor(diabetes), groups=therapy, data=kidney)
cloud(MDRD7 ~ MDRD30 + MDRD12 | diabetes, data = kidney)
library("MASS")
siatka <- kde2d(kidney$MDRD7, kidney$MDRD30, n=50)
siatka <- data.frame(expand.grid(MDRD7=siatka$x,MDRD30=siatka$y), z=c(siatka$z))
levelplot(z~MDRD7*MDRD30, siatka, cuts=20, colorkey=T,region=T)
contourplot(z~MDRD7*MDRD30, siatka, cuts=20)
wireframe(z ~ MDRD7 * MDRD30, siatka, cuts=20, shade=TRUE)
wireframe(z ~ MDRD7 * MDRD30, siatka, cuts=20, shade=FALSE)
xyplot(Petal.Length ~ Sepal.Length | Species, data=iris, scales = list(x = "free", y = "sliced"))
tabela <- as.data.frame(table(daneSoc$wyksztalcenie, daneSoc$plec, daneSoc$praca))
wykres <- barchart(wyksztalcenie ~ Freq | plec, groups = praca, auto.key=TRUE, data=tabela)
wykres$panel
## [1] "panel.barchart"
nasz.panel <- function(..., border) {
panel.grid(h=0, v=-1)
panel.barchart(..., border="transparent")
panel.text(list(...)$x+1, as.numeric(list(...)$y) -0.5 +
as.numeric(list(...)$groups[list(...)$subscripts])/3,
as.numeric(list(...)$x))
}
update(wykres, panel=nasz.panel, scales=list(x="free"), origin=0)
xyplot(cisnienie.skurczowe ~ cisnienie.rozkurczowe | plec,
data = daneSoc,
panel = function(x,y,...) {
lpoints(cisnienie.rozkurczowe,cisnienie.skurczowe,
pch=19, col='grey', cex=0.5)
panel.xyplot(x,y,pch='+', cex=2)
}
)
trellis.par.get("plot.line")
## $alpha
## [1] 1
##
## $col
## [1] "#0080ff"
##
## $lty
## [1] 1
##
## $lwd
## [1] 1
# trellis.par.set(plot.line = list(lwd = 3))
wykres <- xyplot(MDRD7 ~ MDRD12, data = kidney, pch = 19)
plot(wykres, split = c(1,1,2,1))
plot(wykres, split = c(2,1,2,2), newpage = FALSE)
plot(wykres, split = c(3,2,4,2), newpage = FALSE)
plot(wykres, split = c(4,2,4,2), newpage = FALSE)
wykres <- xyplot(MDRD7 ~ MDRD12, data = kidney, pch = 19)
plot(wykres, position=c(0,0,.8,.8))
plot(wykres, position=c(0.35,0.35,.9,.9), newpage = FALSE)
plot(wykres, position=c(0.7,0.7,1,1), newpage = FALSE)
form <- sunspot.year~1:length(sunspot.year)
xyplot(form, type="l", aspect="xy", xlab="", subset=1:140)
xyplot(form, type="l", aspect="xy", xlab="", subset=141:280)
xyplot(MDRD12~MDRD7, kidney, pch=19, aspect="iso")
xyplot(MDRD12~MDRD7, kidney, pch=19, aspect="fill")
library("Przewodnik")
head(countries)
## country birth.rate death.rate population continent
## 1 Afghanistan 34.1 7.7 30552 Asia
## 2 Albania 12.9 9.4 3173 Europe
## 3 Algeria 24.3 5.7 39208 Africa
## 4 Andorra 8.9 8.4 79 Europe
## 5 Angola 44.1 13.9 21472 Africa
## 6 Antigua and Barbuda 16.5 6.8 90 Americas
ggplot(countries, aes(birth.rate, death.rate)) +
geom_point() +
geom_smooth(se = FALSE, size = 3)
ggplot(countries, aes(x=continent, y=birth.rate, label=country))+
geom_violin(aes(fill=continent)) +
geom_jitter(position=position_jitter(width = .45)) +
geom_rug(sides = "l")
ggplot(countries, aes(x = birth.rate, y = death.rate,
color = continent, size = population)) +
geom_point()
ggplot(countries, aes(x = birth.rate, y = death.rate,
color = birth.rate)) + geom_point(size=3)
szkielet <- ggplot(countries, aes(continent, birth.rate,
color=continent, fill=continent))
szkielet + geom_point()
szkielet + geom_boxplot()
szkielet + geom_dotplot(binaxis = "y", stackdir = "center")
szkielet + geom_violin(scale="width")
ggplot(countries, aes(continent)) + geom_bar()
ggplot(countries, aes(birth.rate, death.rate)) +
geom_point() + geom_smooth() +
geom_smooth(method="lm", se=FALSE, color="red", size=5)
ggplot(countries, aes(x = birth.rate, y = death.rate)) +
stat_ellipse() + geom_point() +
facet_grid(~continent)
ggplot(countries, aes(x = birth.rate, y = death.rate)) +
geom_point(data=countries[,-5], size=0.5, color="grey") +
stat_ellipse(color="red4") + geom_point(size=2, color="red") +
facet_grid(~continent)
pl <- ggplot(countries, aes(x = birth.rate, y = death.rate,
shape = continent)) + geom_point()
pl + scale_shape_manual(values = LETTERS)
pl + scale_shape_discrete(solid = FALSE)
pl <- ggplot(countries, aes(x = birth.rate, y = death.rate)) +
geom_point()
pl + scale_x_reverse() + scale_y_reverse()
pl + scale_x_continuous(breaks = c(1,2,5,10,20,50), limits=c(0,50))
pl <- ggplot(countries, aes(x = birth.rate, y = death.rate)) +
geom_point() + geom_smooth(se = FALSE, size = 2)
pl + coord_trans(y = "sqrt", x = "sqrt")
pl + coord_fixed()
pl + coord_flip()
library("ggthemes")
pl + theme_bw() + ggtitle("theme_bw")
pl + theme_tufte() + ggtitle("theme_tufte")
library(grid)
vp1 <- viewport()
vp2 <- viewport(width=0.4, height=0.4, x=0.75, y=0.7)
vp3 <- viewport(width=0.4, height=0.4, x=0.75, y=0.3)
print(pl, vp = vp1)
print(pl, vp = vp2)
print(pl, vp = vp3)
class(pl)
## [1] "gg" "ggplot"
summary(pl)
## data: country, birth.rate, death.rate, population, continent
## [185x5]
## mapping: x = birth.rate, y = death.rate
## faceting: <ggproto object: Class FacetNull, Facet>
## compute_layout: function
## draw_back: function
## draw_front: function
## draw_labels: function
## draw_panels: function
## finish_data: function
## init_scales: function
## map: function
## map_data: function
## params: list
## render_back: function
## render_front: function
## render_panels: function
## setup_data: function
## setup_params: function
## shrink: TRUE
## train: function
## train_positions: function
## train_scales: function
## vars: function
## super: <ggproto object: Class FacetNull, Facet>
## -----------------------------------
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
##
## geom_smooth: na.rm = FALSE
## stat_smooth: na.rm = FALSE, method = auto, formula = y ~ x, se = FALSE
## position_identity
x <- seq(-2*pi, 2*pi, by = 0.3)
plot(x, sin(x), type="b",main="Wykres sin(x) i cos(x)",col="red")
lines(x, cos(x), col="blue", type="l")
curve(sin, from = -2*pi, to = 2*pi)
curve(x^2 - sin(x^2), -2, 2)
plot(0, xlim=c(-2,2), ylim=c(-2,2), type="n", xlab="", ylab="", main="Wariacje nt. funkcji abline()")
abline(0, 0)
for (i in 1:10)
abline(0, i)
abline(h=-1, lwd=3, col="red")
abline(v=-1, lwd=3, lty=2, col="blue")
text( 1.7, 0.2, "a=0, b=0")
text( 1.7, 1.1, "a=1, b=0")
text( 1.3, 1.7, "a=2, b=0")
text( 1.7,-0.8, "h = -1")
text(-0.6, 1.1, "v = -1")
ruchBrowna <- matrix(rnorm(800), 200, 4)
ruchBrowna <- apply(ruchBrowna, 2, cumsum)
matplot(ruchBrowna, type="l", lwd=2, lty=1)
plusk <- function(x,y,s1,s2)
sin(sqrt((x-s1)^2+(y-s2)^2)/4)/(abs(x-s1)+abs(y-s2)+25)
mat1 <- outer(1:200, 1:200, plusk, s1=100, s2=50)
mat2 <- outer(1:200, 1:200, plusk, s1=50, s2=100)
mat3 <- outer(1:200, 1:200, plusk, s1=20, s2=20)
par(bty="n", mar=c(0,0,0,0))
image(mat1+mat2+mat3, col=heat.colors(250))
t(col2rgb("orange"))
## red green blue
## [1,] 255 165 0
boxplot(rnorm(100), main="Taki sobie boxplot")
boxplot(rnorm(100))
title(main="Taki sobie boxplot")
(matryca <- rbind(c(1,2,3), c(4,5,3)))
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 3
# layout(matryca, widths=c(2,2,1), heights=c(2,2))
zm1 <- rbinom(200,5,0.5)
zm2 <- rbinom(200,5,0.5)
sunflowerplot(zm1, zm2)
library(car)
pairs(~wiek+cisnienie.skurczowe+cisnienie.rozkurczowe, data=daneSoc,pch=21, cex=1.5)
scatterplotMatrix(daneSoc[,c(1,6,7)], pch=19)
library(GGally)
ggpairs(daneSoc[,c(2,3,6,7)])
coplot(cisnienie.skurczowe~cisnienie.rozkurczowe|plec+wiek,
data = daneSoc)
library(scatterplot3d)
scatterplot3d(daneSoc$wiek, daneSoc$cisnienie.skurczowe, daneSoc$cisnienie.rozkurczowe)
#scatter3d(cisnienie.rozkurczowe, wzrost, cisnienie.skurczowe, groups=plec)
library("ellipse")
kor <- cor(daneO[,c(1,2,3,4,8,9)], use = "pai")
plotcorr(kor, numbers = TRUE)
plotcorr(kor, col=rgb((kor+1)/2,0,0))
dataEllipse(daneSoc$cisnienie.skurczowe, daneSoc$cisnienie.rozkurczowe)
dataEllipse(daneSoc$cisnienie.skurczowe, daneSoc$cisnienie.rozkurczowe,
levels=(1:10)/10, col=c("red",rep("black",204)), pch=19)
library(vcd)
attach(daneSoc)
fourfold(table(plec, praca))
fourfold(table(plec, praca, wyksztalcenie))
library(ks)
library(MASS)
danetmp <- daneSoc[, c(6,7)]
mcov <- Hscv(danetmp)
gest2d <- kde(danetmp, H=mcov)
plot(gest2d, cont=c(5,25,50,75,95))
heatmap(kor, margins=c(7,7), symm=TRUE)
parcoord(daneSoc[,c(1,6,7)], col=c("red","green"))
parcoord(daneSoc[,c(1,6,7)], col=c("red","green"))
# library("devtools")
# install_github("ramnathv/rCharts")
library("Przewodnik")
przezycia2009 <- przezycia[przezycia$Year == 2009 &
przezycia$Age != "110+",]
head(przezycia2009[,c(1,2,3,7,11)])
## Year Age mx dx Gender
## 5662 2009 0 0.00510 507 Female
## 5663 2009 1 0.00032 32 Female
## 5664 2009 2 0.00018 18 Female
## 5665 2009 3 0.00018 18 Female
## 5666 2009 4 0.00016 16 Female
## 5667 2009 5 0.00009 9 Female
library("rCharts")
p1 <- nPlot(mx ~ Age, group = "Gender", data = przezycia2009, type = "lineChart")
p1$chart(yScale = "#! d3.scale.log() !#")
p1$xAxis(axisLabel = 'Wiek')
p1$yAxis(axisLabel = 'Smiertelnosc')
p1$set(width = 750, height = 590)
# p1
p2 <- nPlot(dx ~ Age, group = "Gender", data = przezycia2009, type = "multiBarChart")
p2$chart(stacked = TRUE)
p2$chart(tooltipContent = "#! function(key, x, y, e){
return 'Liczba osÓb: ' + e.point.dx
} !#")
p2$set(width = 750, height = 590)
# p2
map1 <- Leaflet$new()
map1$setView(c(52.22, 21), zoom = 12)
map1$marker(c(52.22223, 21.00728),
bindPopup = "Politechnika Warszawska, MiNI")
map1$marker(c(52.21167, 20.9815),
bindPopup = "Uniwersytet Warszawski, MIM")
map1$marker(c(52.23925, 21.01742),
bindPopup = "Uniwersytet Warszawski, Centrala")
# map