How to draw a bivariate continuous function in R using ggplot2? | Heatmap | StatswithR | Arnab Hazra
Автор: StatswithR
Загружено: 2020-10-09
Просмотров: 710
Here we explain how to generate a presentation/publication-quality plot of a bivariate continuous function, or a heatmap, in R/R-studio using ggplot2. The codes for the steps explained in the video are as follows. Copy and paste them into R, run them one-by-one and try to understand what each argument is doing.
#datascience #datavisualization #visualization #ggplot2 #tidyverse #function #rstudio #rcoding #timeseries #continuous #bivariate #normal #heatmap
mu = c(1, 2)
sigma = c(1, 1.25)
rho = 0.5
covmat = diag(sigma^2)
covmat[1, 2] = covmat[2, 1] = rho * prod(sigma)
mu
covmat
x = seq(from = mu[1] - 3 * sigma[1], to = mu[1] + 3 * sigma[1], by = 0.05)
y = seq(from = mu[2] - 3 * sigma[2], to = mu[2] + 3 * sigma[2], by = 0.05)
loc = expand.grid(x=x, y=y)
#install.packages("emdbook")
library(emdbook)
norm2dens = apply(loc, 1, dmvnorm, mu = mu, Sigma = covmat)
library(ggplot2)
p = ggplot() + geom_tile(aes(x = loc[ , 1], y = loc[ , 2], fill = norm2dens))
ggsave(p, filename = "bivardens_ggplot1.pdf", height = 8, width = 8)
p = ggplot() + geom_tile(aes(x = loc[ , 1], y = loc[ , 2], fill = norm2dens)) +
coord_fixed(ratio = 1)
ggsave(p, filename = "bivardens_ggplot2.pdf", height = 8, width = 8)
p = ggplot() + geom_tile(aes(x = loc[ , 1], y = loc[ , 2], fill = norm2dens),
width = 0.06, height = 0.06) +
coord_fixed(ratio = 1)
ggsave(p, filename = "bivardens_ggplot3.pdf", height = 8, width = 8)
p = ggplot() + geom_tile(aes(x = loc[ , 1], y = loc[ , 2], fill = norm2dens),
width = 0.06, height = 0.06) +
coord_fixed(ratio = 1) +
xlab(expression(X[1])) + ylab(expression(X[2]))
ggsave(p, filename = "bivardens_ggplot4.pdf", height = 8, width = 8)
p0 = ggplot() + geom_tile(aes(x = loc[ , 1], y = loc[ , 2], fill = norm2dens),
width = 0.06, height = 0.06) +
coord_fixed(ratio = 1) +
xlab(expression(X[1])) + ylab(expression(X[2])) +
ggtitle(expression(paste("Density of BVN(", mu[1], "=1, ", mu[2], "=2, ",
sigma[1], "=1, ", sigma[2], "=1.25, ", rho, "=0.5)", sep = ""))) +
theme(plot.title = element_text(hjust = 0.5))
ggsave(p0, filename = "bivardens_ggplot5.pdf", height = 8, width = 8)
p = p0 + theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=18))
ggsave(p, filename = "bivardens_ggplot6.pdf", height = 8, width = 8)
library(viridis)
p = p0 + theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=18)) +
scale_fill_viridis()
ggsave(p, filename = "bivardens_ggplot7.pdf", height = 8, width = 8)
p = p0 + theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=18)) +
scale_fill_viridis(name = "Density") +
theme(legend.title = element_text(size=18, hjust = 0.5),
legend.text=element_text(size=15))
ggsave(p, filename = "bivardens_ggplot8.pdf", height = 8, width = 8)
p = p0 + theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=18)) +
scale_fill_viridis(name = "Density") +
theme(legend.title = element_text(size=18, hjust = 0.5),
legend.text=element_text(size=15),
legend.key.height = unit(1.5,"cm"),
legend.key.width = unit(1.5,"cm"))
ggsave(p, filename = "bivardens_ggplot9.pdf", height = 8, width = 8)
p = p0 + theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=18)) +
scale_fill_viridis(name = "Density") +
theme(legend.title = element_text(size=18, hjust = 0.5),
legend.text=element_text(size=15),
legend.key.height = unit(1.5,"cm"),
legend.key.width = unit(1.5,"cm")) +
scale_x_continuous(expand = expansion(mult = c(0, 0))) +
scale_y_continuous(expand = expansion(mult = c(0, 0)))
ggsave(p, filename = "bivardens_ggplot10.pdf", height = 8, width = 8)

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: