美文网首页plot
R绘图_ggplot2绘制Dot Plot

R绘图_ggplot2绘制Dot Plot

作者: 谢俊飞 | 来源:发表于2020-02-14 17:46 被阅读0次

ggplot2在线学习:STHDA :Statistical tools for high-throughput data analysis.
ggplot2使用说明:https://ggplot2.tidyverse.org/reference/

  • 火狐截图_2020-02-11T08-36-22.554Z.png

Dot Plot

英文:http://www.sthda.com/english/wiki/ggplot2-dot-plot-quick-start-guide-r-software-and-data-visualization

根据说明文档,运行代码……

#convert the variable dose from a numeric to a factor variable
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)

#Basic dot plot
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_dotplot(binaxis = 'y', stackdir = 'center')
p
#change the dot size and stack ratio
ggplot(ToothGrowth, aes(x=dose, y= len)) +
  geom_dotplot(binaxis = 'y', stackdir = 'center',
               stackratio = 1.5, dotsize = 1.2)
#Rotate the dot plot
p + coord_flip()
#chose the which items to display
p + scale_x_discrete(limit = c("0.5","2"))

#dot plot with mean points
p + stat_summary(fun.y = mean, geom = "point",shape = 8,
                 size = 3, color = "red")

#add basic box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_boxplot() +
  geom_dotplot(binaxis = 'y', stackdir = "center") 
#add notched box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_boxplot(notch = TRUE) +
  geom_dotplot(binaxis = 'y', stackdir = "center") 
#add violin plot
ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_violin(trim = FALSE) +
  geom_dotplot(binaxis = 'y', stackdir = "center") 
#add mean and standar deviation
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_dotplot(binaxis = 'y', stackdir = 'center') 
p + stat_summary(fun.data = "mean_sdl",fun.args = list(mult = 1),
                 geom = "crossbar", width = 0.5)
p + stat_summary(fun.data = mean_sdl,fun.args = list(mult=1),
                 geom="pointrange", color ="red")
#function to produce summary statistic(mean and +/- sd)
data_summary <- function(x){
  m <- mean(x)
  ymin <- m - sd(x)
  ymax <- m + sd(x)
  return(c(y=m,ymin=ymin, ymax=ymax))
}

p + stat_summary(fun.data = data_summary, color = "blue")
 #change the dot plot colors by group
#use the single fill color
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_dotplot(binaxis='y', stackdir='center', fill="#FFAAD4")
# Change dot plot colors by groups
p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_dotplot(binaxis='y', stackdir='center')
p

# Use custom color palettes
p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Use brewer color palettes
p+scale_fill_brewer(palette="Dark2")
# Use grey scale
p + scale_fill_grey() + theme_classic()

#Change the legend position
p + theme(legend.position="top")
p + theme(legend.position="bottom")
p + theme(legend.position="none") # Remove legend


p + scale_x_discrete(limits=c("2", "0.5", "1"))

#dot plot with multiple groups
#change dot plot color by group
ggplot(ToothGrowth,aes(x=dose, y=len, fill=supp))+
  geom_dotplot(binaxis = 'y', stackdir = "center")
#change the position : interval between dot plot of the same group
p <- ggplot(ToothGrowth,aes(x=dose, y=len, fill=supp))+
  geom_dotplot(binaxis = 'y', stackdir = "center",
               position = position_dodge(0.8))
p
#Change dot plot colors and add box plots :
# Change colors
p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Add box plots
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
  geom_boxplot(fill="white")+
  geom_dotplot(binaxis='y', stackdir='center')
# Change the position
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
  geom_boxplot(position=position_dodge(0.8))+
  geom_dotplot(binaxis='y', stackdir='center', 
               position=position_dodge(0.8))
#customized dot plots
# Basic dot plot
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot()+
  geom_dotplot(binaxis='y', stackdir='center')+
  labs(title="Plot of length  by dose",x="Dose (mg)", y = "Length")+
  theme_classic()
# Change color by groups
dp <-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + 
  geom_dotplot(binaxis='y', stackdir='center')+
  labs(title="Plot of length  by dose",x="Dose (mg)", y = "Length")
dp + theme_classic()

#Change fill colors manually :
# Continuous colors.
dp + scale_fill_brewer(palette="Blues") + theme_classic()
# Discrete colors
dp + scale_fill_brewer(palette="Dark2") + theme_minimal()
# Gradient colors
dp + scale_fill_brewer(palette="RdBu") + theme_minimal()

相关文章

网友评论

    本文标题:R绘图_ggplot2绘制Dot Plot

    本文链接:https://www.haomeiwen.com/subject/gobcfhtx.html