TODO
#
# Idea pilfered from http://gastonsanchez.com/work/rtist/matrix.html
#
x11()
par( bg='black', mar=c(0.05, 0.05, 0.05, 0.05))
plot(c(0,0), c(100, 100), xlim=c(0, 100), ylim=c(0,100))
for (x in 0:100) {
y_ = sample(30:100, 1)
points (
rep(x, y_),
100- 1:y_,
pch=sample(letters, y_, replace=TRUE),
col=hsv(0.35, 1, 1, runif(y_, 0.3)),
cex=runif(y_, 0.3)
)
}
z <- locator(1)
#
# Idea pilfered from http://gastonsanchez.com/work/rtist/blue_bubbles.html
#
x11()
x =rnorm(300)
y =rnorm(300)
diameters=rnorm(300) * 4 + 2
par(bg='black', mar=c(0,0,0,0))
plot(1, 1, xlim=c(-2, 2), ylim=c(-2, 2))
points(x, y,
pch =19,
cex =diameters,
col =hsv( 0.7 + rnorm(300, 0, 0.03),
alpha=0.5 + rnorm(300, 0, 0.1))
)
points (x, y,
pch =19,
cex =diameters/2,
col =rgb(1,1,1,0.1))
z <- locator(1)
vec <- c( 5, 7, -2, 8, -11, -4, 6, 9, -1, 0, 2)
pmax(vec, 0)
#
# [1] 5 7 0 8 0 0 6 9 0 0 2
# Dimension of data:
dim(iris)
# [1] 150 5
names(iris)
# [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
# Structure of data:
str(iris)
# 'data.frame': 150 obs. of 5 variables:
# $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
# $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
# $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
# $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
cat("\n\n")
attributes(iris)
# $names
# [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
#
# $row.names
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
# [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
# [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
# [91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
# [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
# [127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
# [145] 145 146 147 148 149 150
#
# $class
# [1] "data.frame"
cat("\n\n")
# First five rows of data:
head(iris)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 5.1 3.5 1.4 0.2 setosa
# 2 4.9 3.0 1.4 0.2 setosa
# 3 4.7 3.2 1.3 0.2 setosa
# 4 4.6 3.1 1.5 0.2 setosa
# 5 5.0 3.6 1.4 0.2 setosa
# 6 5.4 3.9 1.7 0.4 setosa
cat("\n\n")
# Last five rows of data:
tail(iris)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 145 6.7 3.3 5.7 2.5 virginica
# 146 6.7 3.0 5.2 2.3 virginica
# 147 6.3 2.5 5.0 1.9 virginica
# 148 6.5 3.0 5.2 2.0 virginica
# 149 6.2 3.4 5.4 2.3 virginica
# 150 5.9 3.0 5.1 1.8 virginica
x <- 1:100
y <- log(x)
pdf("save_chartes_into_files.pdf")
plot(x, y)
graphics.off()
postscript("save_chartes_into_files.ps")
plot(x, y)
graphics.off()
bmp("save_chartes_into_files.bmp")
plot(x, y)
graphics.off()
jpeg("save_chartes_into_files.jpeg")
plot(x, y)
graphics.off()
png("save_chartes_into_files.png")
plot(x, y)
graphics.off()
tiff("save_chartes_into_files.tiff")
plot(x, y)
graphics.off()
#
# http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r
#
dat_fram <- data.frame (
col_1 = factor (
c('Bar', 'Baz', 'Foo', 'Foo', 'Baz', 'Bar'),
levels = c('Foo', 'Bar', 'Baz'),
ordered = TRUE
),
col_2 = c( 1 , 1 , 1 , 2 , 2 , 2 ),
col_3 = c( 'A', 'B', 'A', 'C', 'B', 'A'),
col_4 = c( 18 , 17 , 16 , 11 , 12 , 13 )
)
cat("\n")
dat_fram
cat("\n\n")
dat_fram[with(dat_fram, order(-col_2, col_1)), ]
df <- data.frame (
col_1 = c('ABC', 'DEF', 'GHI', 'JKL', 'MNO'),
col_2 = c( 5 , 3 , 4 , 2 , 6 ),
col_3 = c( 'pq', 'vw', 'rs', 'xy', 'tu')
)
df
#
# col_1 col_2 col_3
# 1 ABC 5 pq
# 2 DEF 3 vw
# 3 GHI 4 rs
# 4 JKL 2 xy
# 5 MNO 6 tu
# sorting on col_2
df[order(df$col_2),]
#
# col_1 col_2 col_3
# 4 JKL 2 xy
# 2 DEF 3 vw
# 3 GHI 4 rs
# 1 ABC 5 pq
# 5 MNO 6 tu
x <- seq (-3, 3, by=.1)
y <- sin (x*x)
X11()
plot(function(x) sin(x*cos(x)), -5, 5)
system("python plot_fullscreen.py")
z <- locator(1)
#
# Compare with ->rainbow, ->terrain.colors, ->topo.colors
#
X11()
drawRainbow <- function(n) {
barplot(
rep(1,n),
col=heat.colors(n),
yaxt='n')
z <- locator(1)
}
drawRainbow ( 4)
drawRainbow ( 10)
drawRainbow ( 20)
drawRainbow ( 40)
#
# Compare with ->heat.colors, ->rainbow, ->terrain.colors
#
X11()
drawRainbow <- function(n) {
barplot(
rep(1,n),
col=topo.colors(n),
yaxt='n')
z <- locator(1)
}
drawRainbow ( 4)
drawRainbow ( 10)
drawRainbow ( 20)
drawRainbow ( 40)
#
# Compare with ->heat.colors, ->terrain.colors, ->topo.colors
#
X11()
drawRainbow <- function(n) {
barplot(
rep(1,n),
col=rainbow(n),
yaxt='n')
z <- locator(1)
}
drawRainbow ( 4)
drawRainbow ( 10)
drawRainbow ( 20)
drawRainbow ( 40)
X11()
x <- -3:3
y <- x^3 + 25*x^2-23*x
plot(x,y,
pch=16,
col=c('palegreen', 'powderblue', 'seashell3', 'sandybrown', 'darkgrey', 'darkred', 'darkorange')
)
z <- locator(1)
X11()
x <- seq(-1, 1, length.out=20)
y <- x^3-0.5*x
par(bg='light blue')
plot(x, y, pch=16, col='red', cex=1)
z <- locator(1)
#
# ?scatterplot3d::scatterplot3d
# -> example 6
# by Martin Maechler
library(scatterplot3d)
X11()
cubedraw <- function(res3d, min = 0, max = 255, cex = 2, text. = FALSE) {
# Purpose: Draw nice cube with corners
cube01 <- rbind(c(0,0,1), 0, c(1,0,0), c(1,1,0), 1, c(0,1,1), # < 6 outer
c(1,0,1), c(0,1,0)) # <- "inner": fore- & back-ground
cub <- min + (max-min)* cube01
# visibile corners + lines:
res3d$points3d(cub[c(1:6,1,7,3,7,5) ,], cex = cex, type = 'b', lty = 1)
# hidden corner + lines
res3d$points3d(cub[c(2,8,4,8,6), ], cex = cex, type = 'b', lty = 3)
if (text.) # debug
text(res3d$xyz.convert(cub), labels=1:nrow(cub), col='tomato', cex=2)
}
# 6 a) The named colors in R, i.e. colors()
cc <- colors()
crgb <- t(col2rgb(cc))
par(xpd = TRUE)
rr <- scatterplot3d(crgb, color = cc, box = FALSE, angle = 24,
xlim = c(-50, 300), ylim = c(-50, 300), zlim = c(-50, 300)
)
cubedraw(rr)
z <- locator(1)
# 6 b) The rainbow colors from rainbow(201)
rbc <- rainbow(201)
Rrb <- t(col2rgb(rbc))
rR <- scatterplot3d(Rrb, color = rbc, box = FALSE, angle = 24,
xlim = c(-50, 300), ylim = c(-50, 300), zlim = c(-50, 300))
cubedraw(rR)
rR$points3d(Rrb, col = rbc, pch = 16)
z <- locator(1)
X11()
# Eight colors, repeats from 9th
barplot(
rep(1,10),
col=1:10,
names.arg=
paste (1:10, rep(': ', 10), c('black', 'red', 'green3', 'blue', 'cyan', 'magenta', 'yellow', 'gray', 'black', 'red')),
las=2,
yaxt='n')
z <- locator(1)
# ---- Almost same thing, but using palette()
barplot(
rep(1,8),
col =palette(),
names.arg=palette(),
las=2,
yaxt='n')
z <- locator(1)
#
# Compare with ->heat.colors, ->rainbow, ->topo.colors
#
X11()
drawRainbow <- function(n) {
barplot(
rep(1,n),
col=terrain.colors(n),
yaxt='n')
z <- locator(1)
}
drawRainbow ( 4)
drawRainbow ( 10)
drawRainbow ( 20)
drawRainbow ( 40)
X11()
hue <- seq (0, 1, by=1/40)
pie(
rep(1, 40),
labels= formatC(hue, digits=3, format='f'),
cex = 0.75,
col = hsv(hue, 1, 1),
main ='HSV (S=1, V=1)'
)
z <- locator(1)
#
# http://stackoverflow.com/a/30970632/180275
#
x11()
x1 <- seq(50,200,length=1000)
a1 <- dnorm(x1,134,20)
b1 <- dnorm(x1,130,14)
c1 <- dnorm(x1,132,12)
d1 <- dnorm(x1,105,10)
dists <- list(a1,b1,c1,d1)
# specify color names then convert them to RGB+alpha values
col <- c("red","green","blue","yellow")
col.rgba <- rgb(t(col2rgb(col))/255, alpha=0.2)
plot(NA, NA, xlim=range(x1), ylim=range(unlist(dists)), xlab="Variable X", ylab="")
# loop through each distribution
for (i in 1:length(dists)) {
lines(x1, dists[[i]], type='h', lwd=2, col=col.rgba[i]) # add shaded region
lines(x1, dists[[i]], type='l') # add solid line at top
}
z <- locator(1) # wait for mouse click or enter pressed
X11()
t <- 0:314
df <- data.frame (
foo = 0.5*sin(t/100) + 0.25*sin(t/33+0.4) + 0.125*sin(t/99+0.55),
bar = cos(t/215),
baz = -0.4*(t/314)^3 +0.3*(t/314)^2+0.2*t/314
)
COLORS <- c('red' , 'blue' , 'green' )
LINE_TYPES <- c('solid', 'dotted', 'dashed')
matplot(
df,
type="l",
xlab="t",
ylab="f(t)",
col = COLORS,
lty = LINE_TYPES
)
legend(
"bottomleft",
c('foo', 'bar', 'baz'),
col = COLORS,
lty = LINE_TYPES
)
z <- locator(1)
X11()
x <- seq(-1, 1, length.out=50)
layout(
matrix (
c( 1, 1,
2, 3,
2, 4),
nrow=3, ncol=2,
byrow=TRUE
),
widths =c(2, 1),
heights=c(3, 4, 2)
)
plot(x, sin(x*3))
plot(x, x^2-0.5*x)
plot(x, 10^x/10)
plot(x, 0.5*sin(x) + 0.25*(sin(3*x)) + 0.125*(sin(5*x)))
z <- locator(1)
X11()
nofPoints <- 20
plot(1:nofPoints, pch=16, cex=1:nofPoints, col=rainbow(nofPoints))
z <- locator(1) # wait for mouse click or enter pressed
X11()
BOTTOM <- 1
LEFT <- 2
TOP <- 3
RIGHT <- 4
AXIS_LABEL_HORIZONTAL <- 1
plot (
0:10, 0:10,
pch = 16,
col = 0:10,
main = "MAIN label", col.main="gray",
axes = FALSE,
# --
xlab = "XLAB label",
ylab = "YLAB label", col.lab="blue"
)
mtext ("MTEXT (1: bottom)", BOTTOM, col="green" )
mtext ("MTEXT (2: left)" , LEFT , col="magenta")
mtext ("MTEXT (3: top)" , TOP , col="blue" )
mtext ("MTEXT (4: right)" , RIGHT , col="red" )
axis(BOTTOM,
col = "brown", # Color of «scale»
col.axis="rosybrown" # Color of numbers
)
axis(TOP,
col = "purple",
col.axis = "powderblue"
)
axis(RIGHT,
col = "papayawhip",
col.axis = "peachpuff2"
)
axis(LEFT,
col = "seashell1",
col.axis = "seashell4",
las = AXIS_LABEL_HORIZONTAL,
at = c( 0 , 2 , 4 , 6 , 8 , 10 ),
labels = c('Zero', 'Two', 'Four', 'Six', 'Eight', 'Ten')
)
z <- locator(1)
X11()
x <- seq(-1, 1, length.out=50)
par(mfrow=c(3, 2)) # 3 rows, 2 columns
plot(x, sin(x*3))
plot(x, cos(x))
plot(x, x^2-0.5*x)
plot(x, 10^x/10)
plot(x, 0.5*sin(x) + 0.25*(sin(3*x)) + 0.125*(sin(5*x)))
plot(x, -x)
z <- locator(1)
data <- data.frame (
foo = c( 3, 9, 5, 4),
bar = c( 4, 8, 6, 6),
baz = c( 2, 7, 3, 4)
)
row.names(data) <- c('abc', 'def', 'ghi', 'jkl')
rbind(data, Total = colSums(data))
#
# foo bar baz
# abc 3 4 2
# def 9 8 7
# ghi 5 6 3
# jkl 4 6 4
# Total 21 24 16