Dalam analisis statistik, warna dapat digunakan untuk mewakili nilai numerik yang berbeda. Salah satu contoh penggunaan warna dalam R adalah dengan menggunakan paket "ggplot2" dan "gclus". Berikut ini adalah beberapa contoh penggunaannya:
# Non-parametric mean color
col.mean = "green")
Other alternative is to use the cpairs function of the gclus package.
# install.packages("gclus")
library(gclus)
data <- mtcars[c(1, 3, 5, 6)] # Some numeric variables
# cpairs(data) # pairs() alternative
corr <- abs(cor(data)) # Correlation in absolute value
corr
colors <- dmat.color(corr)
order <- order.single(corr)
cpairs(data, order, panel.colors = colors, gap = 0.5,
main = "Sorted and colored variables by correlation")
Scatter Plot dalam ggplot2
Scatter plot dapat dibuat dengan menggunakan fungsi geom_point
dan mengalikan data dengan warna menggunakan fungsi aes
. Berikut adalah contoh penggunaannya:
# 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))
Scatter Plot 3D
Scatter plot 3D dapat dibuat dengan menggunakan paket "scatterplot3d" dan "rgl". Berikut adalah contoh penggunaannya:
# install.packages("scatterplot3d")
library(scatterplot3d)
set.seed(2)
x <- rnorm(1000)
y <- rnorm(1000)
z <- rnorm(1000)
scatterplot3d(x, y, z, pch = 19, color = "blue")
An alternative is to use the plot3d function of the rgl package, that allows an interactive visualization. You can rotate, zoom in and zoom out the scattergram. This is very useful when looking for patterns in three-dimensional data.
# install.packages("rgl")
library(rgl)
plot3d(x, y, z, # Data
type = "s", # Type of the plot
radius = 0.1, # Radius of the observations
col = "lightblue", # Color of the observations
xlab ="X axis lab", # Label of the X axis
ylab = "Y axis lab", # Label of the Y axis
zlab = "Z axis lab") # Label of the Z axis
Overlay Scatter Plot
Dalam beberapa kasus, kita perlu mengoverlay scatter plot untuk membandingkan data yang berbeda. Berikut adalah contoh penggunaannya:
# To overlay scatterplots in R
# import the required libraries
library(ggplot2)
library(reshape2)
# assign data
a1=rnorm(10)
a2=rnorm(10)
a3=rnorm(10)
# create a dataframe from combined data
# and set count to however many points are in each dataset
df = data.frame(a1, a2, a3, count = c(1:10))
# melt the dataframe
df.m = melt(df, id.vars ="count", measure.vars = c("a1","a2","a3"))
# take a look at what melt() does to get an idea of what is going on
df.m
# plot out the melted dataframe using ggplot
ggplot(df.m, aes(count, value, colour = variable)) + geom_point() + ylim(-3,3)
# swapping the axis
ggplot(df.m, aes(value, count, colour = variable)) + geom_point() + xlim(-3,3)
Dalam beberapa kasus, kita perlu menggunakan list untuk mengoverlay scatter plot. Berikut adalah contoh penggunaannya:
a1 = rnorm(10)
a2 = rnorm(25)
a3 = rnorm(17)
a_list = list(a1, a2, a3)
a_df = do.call("rbind", lapply(a_list,
function(x) data.frame(value = x, group = "group")))
ggplot(a_df, aes(x = value, y = value)) +
geom_point() +
theme(axis.line = element_line(colour = "black"))