set.seed(12)
n <- 100
x <- runif(n)
eps <- rnorm(n, 0, 0.25)
y <- 2 + 3 * x^2 + eps
plot(x, y, pch = 19, col = "black")
plot(y ~ x, pch = 19, col = "black") # Equivalent```
```r
set.seed(1)
x3 <- 0:10
y3 <- (0:10) ^ 1.45 + rnorm(11)
y4 <- (0:10) ^ 1.15 + rnorm(11)
y5 <- (0:10) ^ 1.05 + rnorm(11)
plot(x3, y3, type = "b", col = 2 , lwd = 3, pch = 1)
lines(x3, y4, type = "b", col = 3 , lwd = 3, pch = 1)
lines(x3, y5, type = "b", col = 4 , lwd = 3, pch = 1)```
```r
arrowsPlot <- function(x, y, lwd = 1, col = 1, angle = 20, length = 0.2) {
invisible(sapply(1:length(x),
function(i) arrows(x[i], y[i], x[i + 1], y[i + 1], lwd = lwd,
col = col, angle = angle, length = length)))
}
plot(x3, y3, col = 2, lwd = 3, pch = "")
arrowsPlot(x3, y3, col = 2, lwd = 3)
lines(x3, y4, col = 3, lwd = 3)
arrowsPlot(x3, y4, col = 3, lwd = 3)
lines(x3, y5, col = 4 , lwd = 3)
arrowsPlot(x3, y5, col = 4 , lwd = 3)```
```r
# Sample data
x4 <- ifelse(x3 < 5, x3, rev(x3 / 3))
y5 <- ifelse(y3 < 5, y3 ^ 3, rev(y3 + 5))
# Creating the connected scatterplot
plot(x4, y5, yaxt = "n", xaxt = "n", pch = "",
xlab = "Popularity", ylab = "Sells", xlim = c(-1, 5.5))
arrowsPlot(x4, y5, col = 3, lwd = 3)
# Adding the years to each point
text(x4 + 0.3, y5, 1970:1980)```
```r
smoothScatter(x, y, pch = 19,
transformation = function(x) x ^ 0.5, # Scale
colramp = colorRampPalette(c("#f7f7f7", "aquamarine"))) # Colors```
```r
smoothScatter(x, y, transformation = function(x) x ^ 0.4,
colramp = colorRampPalette(c("#000099", "#00FEFF", "#45FE4F",
"#FCFF00", "#FF9400", "#FF3100")))```
```r
library(MASS)
kern <- kde2d(x, y)
contour(kern, drawlabels = FALSE, nlevels = 6,
col = rev(heat.colors(6)), add = TRUE, lwd = 3)```
```r
library(scatterplot3d)
set.seed(2)
x <- rnorm(1000)
y <- rnorm(1000)
z <- rnorm(1000)
scatterplot3d(x, y, z, pch = 19, color = "blue")```
```r
# install.packages("ggplot2")
library(ggplot2)
my_df <- data.frame(x = x, y = y, group = group)
ggplot(my_df, aes(x = x, y = y)) +
geom_point(aes(colour = group)) + # Points and color by group
scale_color_discrete("Groups") + # Change legend title
xlab("Variable X") + # X-axis label
ylab("Variable Y") + # Y-axis label
theme(axis.line = element_line(colour = "black", # Changes the default theme
size = 0.24))```