How to create a nice scatter plot in R using ggplot2? | StatswithR | Arnab Hazra
Автор: StatswithR
Загружено: 2020-08-24
Просмотров: 466
Here we explain how to generate a presentation/publication-quality scatter plot plot 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 #scatterplot #rstudio #rcoding #normal
rm(list = ls())
library(readr)
library(ggplot2)
mydir = "/home/hazraa/Desktop/youtube/scatterplot"
setwd(mydir)
gretoeflfile = "datasets_14872_228180_Admission_Predict.csv"
gretoefldata = read_csv(gretoeflfile)
gre = gretoefldata$`GRE Score`
toefl = gretoefldata$`TOEFL Score`
p = ggplot() + geom_point(aes(x = toefl, y = gre), size = 2)
ggsave(p, filename = "scatter_ggplot1.pdf", width = 6, height = 6)
p = ggplot() + geom_point(aes(x = toefl, y = gre), size = 2) +
xlab("TOEFL Score") + ylab("GRE Score")
ggsave(p, filename = "scatter_ggplot2.pdf", width = 6, height = 6)
p = ggplot() + geom_point(aes(x = toefl, y = gre), size = 2) +
xlab("TOEFL Score") + ylab("GRE Score") +
ggtitle("TOEFL versus GRE")
ggsave(p, filename = "scatter_ggplot3.pdf", width = 6, height = 6)
p = ggplot() + geom_point(aes(x = toefl, y = gre), size = 2) +
xlab("TOEFL Score") + ylab("GRE Score") +
ggtitle("TOEFL versus GRE") +
theme(plot.title = element_text(hjust = 0.5))
ggsave(p, filename = "scatter_ggplot4.pdf", width = 6, height = 6)
p = ggplot() + geom_point(aes(x = toefl, y = gre), size = 2) +
xlab("TOEFL Score") + ylab("GRE Score") +
ggtitle("TOEFL versus GRE") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=15))
ggsave(p, filename = "scatter_ggplot5.pdf", width = 6, height = 6)
final.data = data.frame(gre = gre, toefl = toefl)
p = ggplot(data = final.data, aes(x = toefl, y = gre)) + geom_point() +
xlab("TOEFL Score") + ylab("GRE Score") +
ggtitle("TOEFL versus GRE") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=15)) +
geom_smooth()
ggsave(p, filename = "scatter_ggplot6.pdf", width = 6, height = 6)
p = ggplot(final.data, aes(x = toefl, y = gre)) + geom_point(size = 2) +
xlab("TOEFL Score") + ylab("GRE Score") +
ggtitle("TOEFL versus GRE") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=15)) +
geom_smooth(method=lm)
ggsave(p, filename = "scatter_ggplot7.pdf", width = 6, height = 6)
p = ggplot(final.data, aes(x = toefl, y = gre)) + geom_point(size = 2) +
xlab("TOEFL Score") + ylab("GRE Score") +
ggtitle("TOEFL versus GRE") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=15)) +
geom_smooth(method=lm, se=FALSE)
ggsave(p, filename = "scatter_ggplot8.pdf", width = 6, height = 6)
p = ggplot(final.data, aes(x = toefl, y = gre)) + geom_point(size = 2) +
xlab("TOEFL Score") + ylab("GRE Score") +
ggtitle("TOEFL versus GRE") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=15)) +
geom_smooth(method = "lm", formula = y ~ poly(x, 3))
ggsave(p, filename = "scatter_ggplot9.pdf", width = 6, height = 6)
library(mgcv)
p = ggplot(final.data, aes(x = toefl, y = gre)) + geom_point(size = 2) +
xlab("TOEFL Score") + ylab("GRE Score") +
ggtitle("TOEFL versus GRE") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=15),
axis.title=element_text(size=15),
plot.title = element_text(size=15)) +
geom_smooth(method = gam, formula = y ~ s(x))
ggsave(p, filename = "scatter_ggplot10.pdf", width = 6, height = 6)
Music used
Quiet Place by Jonny Easton
Link: • Quiet Place - Soft Piano Music - Royalty Free
Check out his channel
Link: / jonnyeaston
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: