library(tidyverse)
library(ggpubr)
theme_set(
theme_bw()+
theme(legend.position ="top"))
ggplot(mtcars, aes(mpg, wt))+
geom_point(aes(size = qsec), alpha =0.5)+
scale_size(range = c(0.5,12))# Adjust the range of points size
library(ggpubr)# Grouped Scatter plot with marginal density plots
ggscatterhist(
iris, x ="Sepal.Length", y ="Sepal.Width",
color ="Species", size =3, alpha =0.6,
palette = c("#00AFBB","#E7B800","#FC4E07"),
margin.params = list(fill ="Species", color ="black", size =0.2))
library(ggpubr)# Use box plot as marginal plots
ggscatterhist(
iris, x ="Sepal.Length", y ="Sepal.Width",
color ="Species", size =3, alpha =0.6,
palette = c("#00AFBB","#E7B800","#FC4E07"),
margin.plot ="boxplot",
ggtheme = theme_bw())
# Basic density plot
ggplot(iris, aes(Sepal.Length))+
geom_density()
# Add mean line
ggplot(iris, aes(Sepal.Length))+
geom_density(fill ="lightgray")+
geom_vline(aes(xintercept = mean(Sepal.Length)), linetype =2)
# Change line color by groups
ggplot(iris, aes(Sepal.Length, color = Species))+
geom_density()+
scale_color_viridis_d()
# Add mean line by groups
mu <- iris %>%
group_by(Species)%>%
summarise(grp.mean = mean(Sepal.Length))
ggplot(iris, aes(Sepal.Length, color = Species))+
geom_density()+
geom_vline(aes(xintercept = grp.mean, color = Species),
data = mu, linetype =2)+
scale_color_viridis_d()
# Basic histogram with mean line
ggplot(iris, aes(Sepal.Length))+
geom_histogram(bins =20, fill ="white", color ="black")+
geom_vline(aes(xintercept = mean(Sepal.Length)), linetype =2)
# Add density curves
ggplot(iris, aes(Sepal.Length, stat(density)))+
geom_histogram(bins =20, fill ="white", color ="black")+
geom_density()+
geom_vline(aes(xintercept = mean(Sepal.Length)), linetype =2)
ggplot(iris, aes(Sepal.Length))+
geom_histogram(aes(fill = Species, color = Species), bins =20,
position ="identity", alpha =0.5)+
scale_fill_viridis_d()+
scale_color_viridis_d()
library(ggpubr)
ggqqplot(iris, x ="Sepal.Length",
ggtheme = theme_bw())
# Data
df3 <- data.frame(supp=rep(c("VC","OJ"), each=3),
dose=rep(c("D0.5","D1","D2"),2),
len=c(6.8,15,33,4.2,10,29.5))# Stacked bar plots of y = counts by x = cut,# colored by the variable color
ggplot(df3, aes(x = dose, y = len))+
geom_col(aes(color = supp, fill = supp), position = position_stack())+
scale_color_manual(values = c("#0073C2FF","#EFC000FF"))+
scale_fill_manual(values = c("#0073C2FF","#EFC000FF"))
# Data
df3 <- data.frame(supp=rep(c("VC","OJ"), each=3),
dose=rep(c("D0.5","D1","D2"),2),
len=c(6.8,15,33,4.2,10,29.5))# Use position = position_dodge()
ggplot(df3, aes(x = dose, y = len))+
geom_col(aes(color = supp, fill = supp), position = position_dodge(0.8), width =0.7)+
scale_color_manual(values = c("#0073C2FF","#EFC000FF"))+
scale_fill_manual(values = c("#0073C2FF","#EFC000FF"))
# Data
df3 <- data.frame(supp=rep(c("VC","OJ"), each=3),
dose=rep(c("D0.5","D1","D2"),2),
len=c(6.8,15,33,4.2,10,29.5))# Line plot
ggplot(df3, aes(x = dose, y = len, group = supp))+
geom_line(aes(linetype = supp))+
geom_point(aes(shape = supp))
# Data preparation
df <- economics %>%
select(date, psavert, uempmed)%>%
gather(key ="variable", value ="value",-date)
head(df,3)# Multiple line plot
ggplot(df, aes(x = date, y = value))+
geom_line(aes(color = variable), size =1)+
scale_color_manual(values = c("#00AFBB","#E7B800"))+
theme_minimal()
library(GGally)
ggpairs(iris[,-5])+ theme_bw()
library(factoextra)
USArrests %>%
scale()%>%# Scale the data
dist()%>%# Compute distance matrix
hclust(method ="ward.D2")%>%# Hierarchical clustering
fviz_dend(cex =0.5, k =4, palette ="jco")# Visualize and cut # into 4 groups
library(ggpubr)# Data preparation
housetasks <- read.delim(
system.file("demo-data/housetasks.txt", package ="ggpubr"),
row.names =1)
head(housetasks,4)# Visualization
ggballoonplot(housetasks, fill ="value")+
scale_fill_viridis_c(option ="C")