GGPlot Examples Best Reference

library(tidyverse)
library(ggpubr) 
theme_set(
  theme_bw() + 
    theme(legend.position = "top")
)
library("ggpubr")
p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  geom_smooth(method = lm) +
  stat_cor(method = "pearson", label.x = 20)
p

在这里插入图片描述

library(tidyverse)
library(ggpubr) 
theme_set(
  theme_bw() + 
    theme(legend.position = "top")
)
library(ggforce)
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point() +
  facet_zoom(x = Species == "versicolor")

在这里插入图片描述

library(tidyverse)
library(ggpubr) 
theme_set(
  theme_bw() + 
    theme(legend.position = "top")
)
# Encircle setosa group
library("ggalt")
circle.df <- iris %>% filter(Species == "setosa")
ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_point(aes(colour = Species)) + 
  geom_encircle(data = circle.df, linetype = 2)

在这里插入图片描述

library(tidyverse)
library(ggpubr) 
theme_set(
  theme_bw() + 
    theme(legend.position = "top")
)
# Basic scatter plot
ggplot(mpg, aes(cty, hwy)) +
  geom_point(size = 0.5)

在这里插入图片描述

library(tidyverse)
library(ggpubr) 
theme_set(
  theme_bw() + 
    theme(legend.position = "top")
)

# Jittered points
ggplot(mpg, aes(cty, hwy)) +
  geom_jitter(size = 0.5, width = 0.5)

在这里插入图片描述

library(tidyverse)
library(ggpubr) 
theme_set(
  theme_bw() + 
    theme(legend.position = "top")
)

ggplot(mpg, aes(cty, hwy)) +
  geom_count()

在这里插入图片描述

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())

在这里插入图片描述

ggplot(iris, aes(Sepal.Length)) +
  stat_ecdf(aes(color = Species)) +
  scale_color_viridis_d()

在这里插入图片描述

library(ggridges)
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(aes(fill = Species)) +
  scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))

在这里插入图片描述

df <- mtcars %>%
  rownames_to_column() %>%
  as_data_frame() %>%
  mutate(cyl = as.factor(cyl)) %>%
  select(rowname, wt, mpg, cyl)
# Basic bar plots
ggplot(df, aes(x = rowname, y = mpg)) +
  geom_col() +
  rotate_x_text(angle = 45)

在这里插入图片描述

df <- mtcars %>%
  rownames_to_column() %>%
  as_data_frame() %>%
  mutate(cyl = as.factor(cyl)) %>%
  select(rowname, wt, mpg, cyl)
# Reorder row names by mpg values
ggplot(df, aes(x = reorder(rowname, mpg), y = mpg)) +
  geom_col()  +
  rotate_x_text(angle = 45)

在这里插入图片描述

df <- mtcars %>%
  rownames_to_column() %>%
  as_data_frame() %>%
  mutate(cyl = as.factor(cyl)) %>%
  select(rowname, wt, mpg, cyl)
# Horizontal bar plots, 
# change fill color by groups and add text labels
ggplot(df, aes(x = reorder(rowname, mpg), y = mpg)) +
  geom_col( aes(fill = cyl)) + 
  geom_text(aes(label = mpg), nudge_y = 2) + 
  coord_flip() +
  scale_fill_viridis_d()

在这里插入图片描述

df <- mtcars %>%
  rownames_to_column() %>%
  as_data_frame() %>%
  mutate(cyl = as.factor(cyl)) %>%
  select(rowname, wt, mpg, cyl)
df2 <- df %>% 
  arrange(cyl, mpg) %>%
  mutate(rowname = factor(rowname, levels = rowname))

ggplot(df2, aes(x = rowname, y = mpg)) +
  geom_col( aes(fill = cyl)) + 
  scale_fill_viridis_d() +
  rotate_x_text(45)

在这里插入图片描述

df <- mtcars %>%
  rownames_to_column() %>%
  as_data_frame() %>%
  mutate(cyl = as.factor(cyl)) %>%
  select(rowname, wt, mpg, cyl)
df2 <- df %>% 
  arrange(cyl, mpg) %>%
  mutate(rowname = factor(rowname, levels = rowname))

ggplot(df2, aes(x = rowname, y = mpg)) +
  geom_segment(
    aes(x = rowname, xend = rowname, y = 0, yend = mpg), 
    color = "lightgray"
  ) + 
  geom_point(aes(color = cyl), size = 3) +
  scale_color_viridis_d() +
  theme_pubclean() +
  rotate_x_text(45)

在这里插入图片描述

# 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))

在这里插入图片描述

# Raw data
df <- ToothGrowth %>% mutate(dose = as.factor(dose))
head(df, 3)
# Summary statistics
df.summary <- df %>%
  group_by(dose) %>%
  summarise(sd = sd(len, na.rm = TRUE), len = mean(len))
df.summary
# (1) Line plot
ggplot(df.summary, aes(dose, len)) +
  geom_line(aes(group = 1)) +
  geom_errorbar( aes(ymin = len-sd, ymax = len+sd),width = 0.2) +
  geom_point(size = 2)

在这里插入图片描述

# Raw data
df <- ToothGrowth %>% mutate(dose = as.factor(dose))
head(df, 3)
# Summary statistics
df.summary <- df %>%
  group_by(dose) %>%
  summarise(sd = sd(len, na.rm = TRUE), len = mean(len))
df.summary

# (2) Bar plot
ggplot(df.summary, aes(dose, len)) +
  geom_bar(stat = "identity", fill = "lightgray", color = "black") +
  geom_errorbar(aes(ymin = len, ymax = len+sd), width = 0.2) 

在这里插入图片描述

# Data preparation
df.summary2 <- df %>%
  group_by(dose, supp) %>%
  summarise( sd = sd(len), len = mean(len))
df.summary2
# (1) Line plot + error bars
ggplot(df.summary2, aes(dose, len)) +
  geom_line(aes(linetype = supp, group = supp))+
  geom_point()+
  geom_errorbar(
    aes(ymin = len-sd, ymax = len+sd, group = supp),
    width = 0.2
  )

在这里插入图片描述

# Data preparation
df.summary2 <- df %>%
  group_by(dose, supp) %>%
  summarise( sd = sd(len), len = mean(len))
df.summary2
# (2) Bar plots + upper error bars.
ggplot(df.summary2, aes(dose, len)) +
  geom_bar(aes(fill = supp), stat = "identity",
           position = position_dodge(0.8), width = 0.7)+
  geom_errorbar(
    aes(ymin = len, ymax = len+sd, group = supp),
    width = 0.2, position = position_dodge(0.8)
  )+
  scale_fill_manual(values = c("grey80", "grey30"))

在这里插入图片描述

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# Basic
ggplot(ToothGrowth, aes(dose, len)) +
  geom_boxplot()

在这里插入图片描述

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

# Box plot + violin plot
ggplot(ToothGrowth, aes(dose, len)) +
  geom_violin(trim = FALSE) +
  geom_boxplot(width = 0.2)

在这里插入图片描述

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

# Add jittered points
ggplot(ToothGrowth, aes(dose, len)) +
  geom_boxplot() +
  geom_jitter(width = 0.2)

在这里插入图片描述

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

# Dot plot + box plot
ggplot(ToothGrowth, aes(dose, len)) +
  geom_boxplot() +
  geom_dotplot(binaxis = "y", stackdir = "center")

在这里插入图片描述

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# Box plots
ggplot(ToothGrowth, aes(dose, len)) +
  geom_boxplot(aes(color = supp)) +
  scale_color_viridis_d()

在这里插入图片描述

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# Add jittered points
ggplot(ToothGrowth, aes(dose, len, color = supp)) +
  geom_boxplot() +
  geom_jitter(position = position_jitterdodge(jitter.width = 0.2)) +
  scale_color_viridis_d()

在这里插入图片描述

# 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")

在这里插入图片描述
原文链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值