How to draw multiple functions together in R using ggplot2? | StatswithR | Arnab Hazra
Автор: StatswithR
Загружено: 2020-09-09
Просмотров: 564
Here we explain how to generate a presentation/publication-quality plot in R/R-studio using ggplot2 where we want to draw multiple functions together. 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 #multiple #function #rstudio #rcoding #timeseries #continuous #discrete #discontinuous #normal #exponential
rm(list = ls())
library(ggplot2)
xx = seq(-5, 5, length.out = 1e4)
d1 = dnorm(xx, mean = 0, sd = 1)
d2 = dnorm(xx, mean = 0, sd = 1.5)
d3 = dnorm(xx, mean = 0, sd = 2)
#-------------
p = ggplot() + geom_line(aes(x = xx, y = d1, color = "black")) +
geom_line(aes(x = xx, y = d2, color = "red")) +
geom_line(aes(x = xx, y = d3, color = "blue"))
ggsave(p, filename = "multiple_lines_1.pdf", height = 6, width = 6)
#-------------
p0 = ggplot() + geom_line(aes(x = xx, y = d1, color = "black"), size = 1.5) +
geom_line(aes(x = xx, y = d2, color = "red"), size = 1.5) +
geom_line(aes(x = xx, y = d3, color = "blue"), size = 1.5)
ggsave(p0, filename = "multiple_lines_2.pdf", height = 6, width = 6)
#-------------
p = p0 + xlab("Value") + ylab("Density")
ggsave(p, filename = "multiple_lines_3.pdf", height = 6, width = 6)
#-------------
p = p0 + xlab("Value") + ylab("Density") +
ggtitle("Density comparison") +
theme(plot.title = element_text(hjust = 0.5))
ggsave(p, filename = "multiple_lines_4.pdf", height = 6, width = 6)
#-------------
p1 = p0 + xlab("Value") + ylab("Density") +
ggtitle("Density comparison") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
plot.title = element_text(size=20))
ggsave(p1, filename = "multiple_lines_5.pdf", height = 6, width = 6)
#-------------
p = p1 + scale_color_identity(breaks = c("black", "red", "blue"))
ggsave(p, filename = "multiple_lines_6.pdf", height = 6, width = 6)
#-------------
p = p1 + scale_color_identity(breaks = c("black", "red", "blue"),
guide = "legend")
ggsave(p, filename = "multiple_lines_7.pdf", height = 6, width = 6)
#-------------
p = p1 + scale_color_identity(breaks = c("black", "red", "blue"),
guide = "legend",
name = "SD variation",
labels = c("SD = 1", "SD = 1.5", "SD = 2"))
ggsave(p, filename = "multiple_lines_8.pdf", height = 6, width = 6)
#-------------
p2 = p1 + scale_color_identity(breaks = c("black", "red", "blue"),
guide = "legend",
name = "SD variation",
labels = c(expression(paste(sigma, " = 1", sep = "")),
expression(paste(sigma, " = 1.5", sep = "")),
expression(paste(sigma, " = 2", sep = ""))))
ggsave(p2, filename = "multiple_lines_9.pdf", height = 6, width = 6)
#-------------
p = p2 + theme(legend.text.align = 0)
ggsave(p, filename = "multiple_lines_10.pdf", height = 6, width = 6)
#-------------
p = p2 + theme(legend.text.align = 0,
legend.position = c(0.8, 0.8))
ggsave(p, filename = "multiple_lines_11.pdf", height = 6, width = 6)
#-------------
p = p2 + theme(legend.text.align = 0,
legend.position = c(0.8, 0.8),
legend.key = element_rect(fill = "transparent"))
ggsave(p, filename = "multiple_lines_12.pdf", height = 6, width = 6)
#-------------
p = p2 + theme(legend.text.align = 0,
legend.position = c(0.8, 0.8),
legend.key = element_rect(fill = "transparent"),
legend.text=element_text(size=20),
legend.title = element_text(size=20))
ggsave(p, filename = "multiple_lines_13.pdf", height = 6, width = 6)
#-------------
p = p2 + theme(legend.text.align = 0,
legend.position = c(0.8, 0.8),
legend.key = element_rect(fill = "transparent"),
legend.text=element_text(size=20),
legend.title = element_text(size=20),
legend.key.width = unit(1,"cm"))
ggsave(p, filename = "multiple_lines_14.pdf", height = 6, width = 6)
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: