03 ggplot基本图形
2022-12-09 本文已影响0人
Jachin111
散点图
library(ggplot2)
df1 <- read.csv("scatter_plot_example.csv",header=T)
head(df1)
image.png
ggplot(data=df1,aes(x=var1,y=var2)) + geom_point()
image.png
ggplot(data=df1,aes(x=var1,y=var2)) +
geom_point(color="red",size=10,shape=18,alpha=0.5)
image.png
ggplot(data=df1,aes(x=var1,y=var2,color=var3,shape=var3)) +
geom_point(size=5)
image.png
ggplot(data=df1,aes(x=var1,y=var2,color=var3,shape=var3)) +
geom_point(size=5) +
scale_color_manual(values=c("steelblue","yellowgreen","violetred1"))
image.png
ggplot(data=df1,aes(x=var1,y=var2,color=var3,shape=var3)) +
geom_point(size=5) +
scale_color_manual(values=c("steelblue","yellowgreen","violetred1")) +
scale_shape_manual(values=c(9,10,11))
image.png
气泡图
df2 <- read.csv("bubble_plot_example.csv",header=T)
head(df2)
image.png
ggplot(data=df2,aes(x=var1,y=var2,color=var3,size=var4)) +
geom_point(alpha=0.8) +
scale_size_continuous(range=c(1,50)) +
theme_bw() +
theme(legend.position="none") +
ylim(0,0.4) +
scale_color_manual(values=c("steelblue","yellowgreen","violetred1"))
image.png
折线图
df3 <- read.csv("line_example.csv",header=T)
head(df3)
image.png
ggplot(data=df3,aes(x=time_point,y=value)) +
geom_line() +
geom_point() +
geom_errorbar(aes(ymin=value-sd,ymax=value+sd),width=0.15) +
theme_bw()
image.png
柱形图
df4 <- read.csv("bar_plot_example.csv",header=T)
head(df4)
image.png
ggplot(data=df4,aes(x=var1,y=var2)) + geom_col()
image.png
ggplot(data=df4,aes(x=var1,y=var2)) +
geom_col(color="red",fill="lightblue")
image.png
ggplot(data=df4,aes(x=var1,y=var2)) +
geom_col(aes(fill=var1)) +
theme_bw() +
scale_fill_manual(values=c("#008fd5","#ff2700","#77ab43","#ffd700")) +
scale_y_continuous(expand=c(0,0),limits=c(0,8))
image.png
df5 <- read.csv("bar_plot_example_1.csv",header=T)
head(df5)
image.png
ggplot(data=df5,aes(x=var3,y=var2,fill=var1)) +
geom_bar(stat="identity") +
theme_bw() +
scale_fill_manual(values=c("steelblue","yellowgreen","violetred1")) +
scale_y_continuous(expand=c(0,0),limits=c(0,20))
image.png
ggplot(data=df5,aes(x=var3,y=var2,fill=var1)) +
geom_bar(stat="identity",position="dodge") +
scale_fill_manual(values=c("steelblue","yellowgreen","violetred1")) +
scale_y_continuous(expand=c(0,0),limits=c(0,10)) +
theme_bw()
image.png
箱线图
df6 <- read.csv("ToothGrowth.csv",header=T)
head(df6)
table(df6$dose)
image.png
df6$dose <- factor(df6$dose,levels=c("0.5","1","2"))
ggplot(data=df6,aes(x=dose,y=len,fill=dose)) +
geom_boxplot() +
scale_fill_manual(values=c("steelblue","yellowgreen","violetred1")) +
theme_bw() +
stat_boxplot(geom="errorbar",width=0.2)
image.png
小提琴图
ggplot(data=df6,aes(x=dose,y=len,fill=dose)) +
geom_violin() +
theme_bw() +
scale_fill_manual(values=c("steelblue","yellowgreen","violetred1"))
image.png
ggplot(data=df6,aes(x=dose,y=len,fill=dose)) +
geom_violin() +
geom_boxplot(width=0.3) +
scale_fill_manual(values=c("steelblue","yellowgreen","violetred1")) +
theme_bw() +
stat_boxplot(geom="errorbar",width=0.2)
image.png
热图
df7 <- read.csv("pheatmap_example_data.csv",header=T)
head(df7)
image.png
library(reshape2)
df7.1 <- melt(df7,id.vars='gene_name')
head(df7.1)
ggplot(data=df7.1,aes(x=variable,y=gene_name,fill=value)) +
geom_tile(color="black") +
theme(axis.text.x=element_text(angle=90,hjust=0.5,vjust=0.5),axis.ticks=element_blank()) +
scale_fill_gradient(low="steelblue",high="violetred1") +
labs(x=NULL,y=NULL) +
geom_text(aes(label=round(value,1)))
image.png