CC BY-NC-ND 3.0
1:8
barplot(sample(10:15, 8, replace = TRUE), col = 1:8, names.arg = 1:8)
colors()
head(colors(), n = 20)
## [1] "white" "aliceblue" "antiquewhite" "antiquewhite1"
## [5] "antiquewhite2" "antiquewhite3" "antiquewhite4" "aquamarine"
## [9] "aquamarine1" "aquamarine2" "aquamarine3" "aquamarine4"
## [13] "azure" "azure1" "azure2" "azure3"
## [17] "azure4" "beige" "bisque" "bisque1"
colors()
colors()
barplot(sample(10:15, 8, replace = TRUE),
col = colors()[sample(1:657, size = 8)])
rgb()
graphBarplotCol <- function(n){
myX <- rnorm(n)
myY <- rnorm(n)
myCol <- c(2, 3, 4, sample(2:4, size = (n - 3), replace = TRUE))
myYCut <- cut(myY, breaks = -4:4)
myXCut <- cut(myX, breaks = -4:4)
myYCutCol <- table(myCol, myYCut)
myXCutCol <- table(myCol, myXCut)
rColX <- myXCutCol[1,] / (myXCutCol[1,] + myXCutCol[2,] +
myXCutCol[3,])
gColX <- myXCutCol[2,] / (myXCutCol[1,] + myXCutCol[2,] +
myXCutCol[3,])
bColX <- myXCutCol[3,] / (myXCutCol[1,] + myXCutCol[2,] +
myXCutCol[3,])
rColX[is.na(rColX)] <- 0
gColX[is.na(gColX)] <- 0
bColX[is.na(bColX)] <- 0
rColY <- myYCutCol[1,] / (myYCutCol[1,] + myYCutCol[2,] +
myYCutCol[3,])
gColY <- myYCutCol[2,] / (myYCutCol[1,] + myYCutCol[2,] +
myYCutCol[3,])
bColY <- myYCutCol[3,] / (myYCutCol[1,] + myYCutCol[2,] +
myYCutCol[3,])
rColY[is.na(rColY)] <- 0
gColY[is.na(gColY)] <- 0
bColY[is.na(bColY)] <- 0
op <- par(no.readonly = TRUE)
par(mar = c(2, 3, 1, 1))
layout(matrix(c(1, 1, 0,
2, 2, 3,
2, 2, 3), ncol = 3, byrow = TRUE))
barplot(table(myXCut), las = 1, col = rgb(rColX, gColX, bColX))
plot(x = myX, y = myY, col = myCol, pch = 16,
xlim = c(-4, 4), ylim = c(-4, 4), cex = 1.5,
panel.first = grid())
barplot(table(myYCut), las = 1, horiz = TRUE,
col = rgb(rColY, gColY, bColY))
par(op)
}
rgb()
boxplot(matrix(rnorm(1000), ncol = 10), main = "terrain.colors()",
col = terrain.colors(10), axes = FALSE)
boxplot(matrix(rnorm(1000), ncol = 10), main = "heat.colors()",
col = heat.colors(10), axes = FALSE)
boxplot(matrix(rnorm(1000), ncol = 10), main = "topo.colors()",
col = topo.colors(10), axes = FALSE)
boxplot(matrix(rnorm(1000), ncol = 10), main = "cm.colors()",
col = cm.colors(10), axes = FALSE)
boxplot(matrix(rnorm(1000), ncol = 10), main = "rainbow()",
col = rainbow(10), axes = FALSE)
colorRampPalette()
boxplot(matrix(rnorm(2500), ncol = 25),
col = colorRampPalette(c('blue', 'red'))(25), axes = FALSE)
colorRampPalette()
boxplot(matrix(rnorm(2500), ncol = 25),
col = colorRampPalette(c('blue', 'white', 'red'))(25),
axes = FALSE)
colorRampPalette()
boxplot(matrix(rnorm(2500), ncol = 25),
col = colorRampPalette(c(
rgb(255, 136, 0, maxColorValue = 255),
rgb(0, 94, 255, maxColorValue = 255)))(25),
axes = FALSE)
palettesForR()
pkgCheck <- function(x){
if (!require(x, character.only = TRUE)){
install.packages(x, dependencies = TRUE)
if(!require(x, character.only = TRUE)) {
stop()
}
}
}
pkgCheck("palettesForR")
palettesForR()
## [1] "Android_gpl" "Bears_gpl" "Bgold_gpl"
## [4] "Blues_gpl" "Borders_gpl" "Browns_gpl"
## [7] "Caramel_gpl" "Cascade_gpl" "China_gpl"
## [10] "Coldfire_gpl" "Cool_gpl" "Cranes_gpl"
## [13] "Dark_gpl" "Default_gpl" "Echo_gpl"
## [16] "Ega_gpl" "Firecode_gpl" "Gold_gpl"
## [19] "Grayblue_gpl" "Grays_gpl" "GrayViolet_gpl"
## [22] "Gray_gpl" "Greens_gpl" "Hilite_gpl"
## [25] "Inkscape_gpl" "Khaki_gpl" "LaTeX_gpl"
## [28] "Lights_gpl" "MATLAB_gpl" "Muted_gpl"
## [31] "Named_gpl" "News3_gpl" "Op2_gpl"
## [34] "Paintjet_gpl" "Pastels_gpl" "Plasma_gpl"
## [37] "Reds_gpl" "Royal_gpl" "SVG_gpl"
## [40] "Tango_gpl" "Topographic_gpl" "Visibone_gpl"
## [43] "Volcano_gpl" "Warm_gpl" "WebHex_gpl"
## [46] "WebSafe22_gpl" "Web_gpl" "Windows_gpl"
palettesForR()
showPalette(Echo_gpl)
palettesForR()
showPalette(GrayViolet_gpl)
palettesForR()
groupTest <- sample(1:3, size = 100, replace = TRUE)
valueTest <- sample(1:7, size = 100, replace = TRUE)
tableTest <- table(groupTest, valueTest)
print(tableTest)
## valueTest
## groupTest 1 2 3 4 5 6 7
## 1 5 4 4 7 1 6 6
## 2 5 3 4 3 3 5 4
## 3 7 7 5 4 5 8 4
palettesForR()
barplot(tableTest,
col = Echo_gpl, axes = FALSE, beside = TRUE)
palettesForR()
groupTest <- sample(1:3, size = 100, replace = TRUE)
valueTest <- sample(1:7, size = 100, replace = TRUE)
tableTest <- table(valueTest, groupTest)
print(tableTest)
## groupTest
## valueTest 1 2 3
## 1 3 6 2
## 2 4 6 5
## 3 6 11 6
## 4 1 7 4
## 5 7 4 3
## 6 2 3 9
## 7 2 5 4
palettesForR()
barplot(tableTest,
col = Echo_gpl[c(1, 4, 7, 10, 13, 16, 19)],
axes = FALSE, beside = TRUE)
palettesForR()
makeImpact <- function(myPal, numP = 300, impact = 0.33, multCex = 3){
myX <- sample(0:1000, size = numP, replace = TRUE)/1000
myY <- sample(0:1000, size = numP, replace = TRUE)/1000
distImpact <- sqrt((myX - impact)^2 + (myY - impact)^2)
dfXY <- data.frame(myX, myY, distImpact)
plot(x = dfXY$myX, y = dfXY$myY, axes = FALSE,
xlab = "", ylab = "", cex = dfXY$distImpact*multCex,
col = myPal, pch = 16)
}
palettesForR()
par(mfrow = c (1, 2), mar = c(0, 0, 0, 0))
makeImpact(myPal = Echo_gpl, numP = 3000, impact = 0.33)
makeImpact(myPal = Dark_gpl, numP = 3000, impact = 0.66)
palettesForR()
par(mfrow = c (1, 2), mar = c(0, 0, 0, 0))
makeImpact(myPal = Coldfire_gpl, numP = 3000, impact = 0.33)
makeImpact(myPal = Blues_gpl, numP = 3000, impact = 0.66)
RColorBrewer()
pkgCheck <- function(x){
if (!require(x, character.only = TRUE)){
install.packages(x, dependencies = TRUE)
if(!require(x, character.only = TRUE)) {
stop()
}
}
}
pkgCheck("RColorBrewer")
## Le chargement a nécessité le package : RColorBrewer
RColorBrewer()
display.brewer.all()
RColorBrewer()
boxplot(matrix(rnorm(1000), ncol = 10),
col = brewer.pal(10, "Paired"), axes = FALSE)
Hay muchos paquetes que contienen paletas. Por ejemplo :
ggplot2
… y el tidyverse (https://www.tidyverse.org/) y RStudio …https://resources.rstudio.com/rstudio-conf-2019/opening-keynote-tareef-kawaf
ggplot2
pkgCheck("cranlogs") # devtools::install_github("metacran/cranlogs")
getVerDate <- function(myPkg, startY){
pkgURL <- sprintf('https://cran.r-project.org/src/contrib/Archive/%s/', myPkg)
contURL <- readLines(pkgURL)
vecCont <- contURL[grep(contURL,
pattern = '(<tr><td valign=\"top\"><img src=\"/icons/compressed.gif\")')]
vecContSplit <- strsplit(vecCont, split = ">|<")
pkgVersion <- sapply(vecContSplit, "[[", 13)
pkgVersion <- sapply(strsplit(pkgVersion, split = "(tar.gz)|_"), "[[", 2)
pkgDate <- sapply(strsplit(sapply(vecContSplit, "[[", 19),
split = " "), "[[", 1)
vPkg <- data.frame(date = pkgDate, ver = pkgVersion)
vPkg <- vPkg[as.Date(vPkg$date,
format = "%Y-%m-%d") > as.Date(paste0(startY, "-01-01"),
format = "%Y-%m-%d"),]
downl <- cran_downloads(from = paste0(startY, "-01-01"),
to = format(Sys.time(), "%Y-%m-%d"), packages = myPkg)
downl <- downl[as.Date(downl$date,
format = "%Y-%m-%d") > as.Date(paste0(startY, "-01-01"),
format = "%Y-%m-%d"),]
return(list(vPkg, downl))
}
getImports <- function(myPkg) { ### a changer pour avoir les reverse dependencies (x3)
pkgURL <- sprintf('https://cran.r-project.org/web/packages/%s/', myPkg)
contURL <- readLines(pkgURL)
vecCont <- contURL[grep(contURL,
pattern = '(<td>Imports:</td>)') + 1]
vecContSplit <- strsplit(vecCont, split = ">|<")[[1]]
vecContSplit <- strsplit(vecContSplit[grep(vecContSplit,
pattern = '^(a href=)')], split = "/")
Imports <- sapply(vecContSplit, "[[", 2)
jj <- intersect(c("Depends", "Imports", "Suggests"), colnames(dcf))
val <- unlist(strsplit(dcf[, jj], ","), use.names=FALSE)
val <- gsub("\\s.*", "", trimws(val))
val[val != "R"]
}
dataD <- getVerDate(myPkg = "ggplot2", startY = 2006)
vPkg <- dataD[[1]]
downl <- dataD[[2]]
plot(x = downl$date, y = cumsum(downl$count), type = "l",
xlab = "Time", ylab = "Cumulative number of downloads", panel.first = grid())
points(x = as.Date(vPkg$date, format = "%Y-%m-%d"),
y = cumsum(downl$count)[as.Date(downl$date, format = "%Y-%m-%d") %in%
as.Date(vPkg$date, format = "%Y-%m-%d")], pch = 16)
yy <- sum(downl$count)
xx <- Sys.Date()
abline(h = yy, v = xx)
text(
x = as.Date("2012-01-01", format = "%Y-%m-%d"),
y = yy,
labels = paste0("Downloads = ", yy),
pos = 1, cex = 3)
ggplot2
## Le chargement a nécessité le package : cranlogs
devRate
;-)ggplot2
pkgCheck <- function(x){
if (!require(x, character.only = TRUE)){
install.packages(x, dependencies = TRUE)
if(!require(x, character.only = TRUE)) {
stop()
}
}
}
pkgCheck("ggplot2")
ggplot2
Recursos:
ggplot2
# ggplot(data = <DATA>) +
# <GEOM_FUNCTION>(
# mapping = aes(<MAPPINGS>),
# stat = <STAT>,
# position = <POSITION>
# )
plot
data(iris)
plot(x = iris$Sepal.Length, y = iris$Sepal.Width,
main = "base", pch = 16)
ggplot2
data(iris)
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
p + geom_point() + ggtitle("ggplot2")
plot
separar info.plot(x = iris$Sepal.Length, y = iris$Sepal.Width,
main = "base", pch = 16, col = iris$Species)
ggplot2
separar info.p <- ggplot(data = iris,
aes(x = Sepal.Length, y = Sepal.Width, colour = Species))
p + geom_point() + ggtitle("ggplot2")
plot
separar info.plot
separar info.op <- par(no.readonly = TRUE)
par(mar = c(4, 4, 2, 8))
plot(x = iris$Sepal.Length, y = iris$Sepal.Width,
axes = FALSE, pch = 16, col = iris$Species,
panel.first = {
rect(par("usr")[1],
par("usr")[3],
par("usr")[2],
par("usr")[4],
col = "lightgray", border = NA)
abline(v = 4:8, col = "white", lwd = 2, lty = 1)
abline(h = 2:5, col = "white", lwd = 2, lty = 1)
grid(col = "white", lwd = 1, lty = 1)
})
title("base", adj = 0, line = 0.5)
axis(1, col = NA, col.ticks = 1, cex.axis = 0.9)
axis(2, col = NA, col.ticks = 1, las = 1, cex.axis = 0.9)
par(xpd = TRUE)
legend(8.2, 3.5, legend = levels(iris$Species), bty = "n",
pch = 16, col = as.numeric(unique(iris$Species)),
title = "Species")
par(op)
ggplot2
reg. lineal# linear regressions
lmFits <- lapply(1:3, function(i){
fitSp1 <- lm(iris$Sepal.Width[as.numeric(iris$Species) == i] ~
iris$Sepal.Length[as.numeric(iris$Species) == i])
fStat1 <- summary(fitSp1)$fstatistic
rSq1 <- summary(fitSp1)$r.squared
pVal1 <- summary(fitSp1)$coefficients[2, 4]
stat1 <- paste0("F=", round(fStat1[1], digits = 2),
"; DF=", fStat1[2], "/", fStat1[3], "; r-sq=", round(rSq1, digits = 2),
"; p-val=", round(pVal1, digits = 6))
return(list(fitSp1, stat1))
})
ggplot2
reg. linealp <- ggplot(data = iris,
aes(x = Sepal.Length, y = Sepal.Width, colour = Species))
p <- p + geom_point() +
ggtitle("ggplot2") +
stat_smooth(method = "lm", se = FALSE)
ggplot2
reg. linealggplot2
reg. linealp <- p + annotate(geom = "text", x = 6, y = 2.250, label = lmFits[[1]][[2]], colour = 2)
p <- p + annotate(geom = "text", x = 6, y = 2.125, label = lmFits[[2]][[2]], colour = 3)
p + annotate(geom = "text", x = 6, y = 2.000, label = lmFits[[3]][[2]], colour = 4)
plot
reg. linealplot(x = iris$Sepal.Length, y = iris$Sepal.Width,
main = "base", pch = 16, col = iris$Species)
abline(lmFits[[1]][[1]], col = 1)
abline(lmFits[[2]][[1]], col = 2)
abline(lmFits[[3]][[1]], col = 3)
plot
reg. linealplot
reg. linealplot(x = iris$Sepal.Length, y = iris$Sepal.Width,
main = "base", pch = 16, col = iris$Species)
abline(lmFits[[1]][[1]], col = 1)
abline(lmFits[[2]][[1]], col = 2)
abline(lmFits[[3]][[1]], col = 3)
text(x = 5.5, y = 2.3, labels = lmFits[[1]][[2]], pos = 4)
text(x = 5.5, y = 2.15, labels = lmFits[[2]][[2]], pos = 4, col = 2)
text(x = 5.5, y = 2.0, labels = lmFits[[3]][[2]], pos = 4, col = 3)
plot
reg. linealplot
reg. linealop <- par(no.readonly = TRUE)
par(mar = c(4, 4, 2, 8))
plot(x = iris$Sepal.Length, y = iris$Sepal.Width,
axes = FALSE, pch = 16, col = iris$Species,
panel.first = {
rect(par("usr")[1],
par("usr")[3],
par("usr")[2],
par("usr")[4],
col = "lightgray", border = NA)
abline(v = 4:8, col = "white", lwd = 2, lty = 1)
abline(h = 2:5, col = "white", lwd = 2, lty = 1)
grid(col = "white", lwd = 1, lty = 1)
})
title("base", adj = 0, line = 0.5)
axis(1, col = NA, col.ticks = 1, cex.axis = 0.9)
axis(2, col = NA, col.ticks = 1, las = 1, cex.axis = 0.9)
clip(min(iris$Sepal.Length[iris$Species == "setosa"]),
max(iris$Sepal.Length[iris$Species == "setosa"]),
min(iris$Sepal.Width), max(iris$Sepal.Width))
abline(lmFits[[1]][[1]], col = 1, lwd = 2)
clip(min(iris$Sepal.Length[iris$Species == "versicolor"]),
max(iris$Sepal.Length[iris$Species == "versicolor"]),
min(iris$Sepal.Width), max(iris$Sepal.Width))
abline(lmFits[[2]][[1]], col = 2, lwd = 2)
clip(min(iris$Sepal.Length[iris$Species == "virginica"]),
max(iris$Sepal.Length[iris$Species == "virginica"]),
min(iris$Sepal.Width), max(iris$Sepal.Width))
abline(lmFits[[3]][[1]], col = 3, lwd = 2)
clip(min(iris$Sepal.Length), max(iris$Sepal.Length),
min(iris$Sepal.Width), max(iris$Sepal.Width))
text(x = 5.5, y = 2.35, labels = lmFits[[1]][[2]], pos = 4)
text(x = 5.5, y = 2.2, labels = lmFits[[2]][[2]], pos = 4, col = 2)
text(x = 5.5, y = 2.05, labels = lmFits[[3]][[2]], pos = 4, col = 3)
par(xpd = TRUE)
legend(8.2, 3.5, legend = levels(iris$Species), bty = "n",
pch = 16, col = as.numeric(unique(iris$Species)),
title = "Species")
par(op)
plot
reg. linealggplot2
Recursos:
plotly
Open Source Graphing LibraryR >> JavaScript graphing library plotly.js
Todos los ejemplos se encuentren en el sitio web de plotly.
plotly
pkgCheck <- function(x){
if (!require(x, character.only = TRUE)){
install.packages(x, dependencies = TRUE)
if(!require(x, character.only = TRUE)) {
stop()
}
}
}
pkgCheck("plotly")
pkgCheck("gapminder")
plotly
plotly
plotly
Este capítulo nos permitió ver otras opciones gráficas y, en particular, los paquetes ggplot2 y plotly. Existen libros específicos (en inglés) que cubren todos los aspectos de estos paquetes, aquí el objetivo es saber que existen estas opciones para usarlos si es necesario. Los sitios web “Data to Viz” y “r-graph gallery” (https://www.data-to-viz.com; https://www.r-graph-gallery.com/) son buenos recursos para tener ideas de las posibilidades que ofrece R en cuanto a representaciones gráficas.