欢迎关注微信公众号(医学生物信息学),医学生的生信笔记,记录学习过程。
折线图主要应用于时间序列数据的可视化。在折线图中,X 轴包括类别型或者序数型变量,分别对应文本坐标轴和序数坐标轴(如日期坐标轴)两种类型;Y 轴为数值型变量。
面积图是在折线图的基础之上形成的,它将折线图中的折线与自变量坐标轴之间的区域使用颜色填充,这样可以更好地突出趋势信息。跟折线图一样,面积图可显示某时间段内量化数值的变化和发展,最常用来显示趋势,而非表示具体数值。
ggplot2
包的geom_line()
函数可以绘制折线图,geom_area()
函数可以绘制面积图,使用geom_bar()
函数结合geom_line()
函数可以绘制颜色映射填充的面积图。
折线图
library(ggplot2)
library(RColorBrewer)
library(reshape2)
mydata<-read.csv("Line_Data.csv",stringsAsFactors=FALSE)
mydata$date<-as.Date(mydata$date)
mydata<-melt(mydata,id="date")
ggplot(mydata, aes(x =date, y = value,color=variable) )+
#geom_area(fill="#FF6B5E",alpha=0.75)+
geom_line(linewidth=1)+
scale_x_date(date_labels = "%Y",date_breaks = "2 year")+
xlab("Year")+
ylab("Value")+
theme( axis.title=element_text(size=10,face="plain",color="black"),
axis.text = element_text(size=10,face="plain",color="black"),
legend.position = c(0.15,0.8),
legend.background = element_blank())
面积图
library(ggplot2)
library(RColorBrewer)
library(reshape2)
mydata<-read.csv("Line_Data.csv",stringsAsFactors=FALSE)
mydata$date<-as.Date(mydata$date)
mydata<-melt(mydata,id="date")
ggplot(mydata, aes(x =date, y = value,group=variable) )+
geom_area(aes(fill=variable),alpha=0.5,position="identity")+
geom_line(aes(color=variable),size=0.75)+#color="black",
scale_x_date(date_labels = "%Y",date_breaks = "2 year")+
xlab("Year")+
ylab("Value")+
theme( axis.title=element_text(size=10,face="plain",color="black"),
axis.text = element_text(size=10,face="plain",color="black"),
legend.position = c(0.15,0.8),
legend.background = element_blank())
mydata<-read.csv("Area_Data.csv",stringsAsFactors=FALSE)
mydata$date<-as.Date(mydata$date)
ggplot(mydata, aes(x =date, y = value) )+
geom_area(fill="#FF6B5E",alpha=0.75)+
geom_line(color="black",size=0.75)+
scale_x_date(date_labels = "%Y",date_breaks = "2 year")+
xlab("Year")+
ylab("Value")+
theme( axis.title=element_text(size=10,face="plain",color="black"),
axis.text = element_text(size=10,face="plain",color="black"))
newdata<-data.frame(spline(as.numeric(mydata$date),mydata$value,n=1000,method= "natural"))
newdata$date<-as.Date(newdata$x,origin = "1970-01-01")
ggplot(newdata, aes(x =date, y = y) )+
geom_bar(aes(fill=y,colour=y),stat = "identity",alpha=1,width = 1)+
geom_line(color="black",size=0.5)+
scale_color_gradientn(colours= brewer.pal(9,'Reds'),name = "Value")+
scale_x_date(date_labels = "%Y",date_breaks = "2 year")
ggplot(newdata, aes(x =date, y = y) )+ #geom_area(fill="#FF6B5E",alpha=0.75)
geom_bar(aes(fill=y,colour=y),stat = "identity",alpha=1,width = 1)+
geom_line(color="black",size=0.5)+
scale_color_gradientn(colours=brewer.pal(9,'Reds'),name = "Value")+
scale_x_date(date_labels = "%Y",date_breaks = "2 year")+
xlab("Year")+
ylab("Value")+
guides(fill=FALSE)+
theme( axis.title=element_text(size=10,face="plain",color="black"),
axis.text = element_text(size=10,face="plain",color="black"),
legend.position = c(0.12,0.75),
legend.background = element_blank() )
夹层填充面积图
mydata<-read.csv("Line_Data.csv",stringsAsFactors=FALSE)
mydata$date<-as.Date(mydata$date)
mydata1<-mydata
mydata1$ymin<-apply(mydata1[,c(2,3)], 1, min)
mydata1$ymax<-apply(mydata1[,c(2,3)], 1, max)
ggplot(mydata1, aes(x =date))+
geom_ribbon( aes(ymin=ymin, ymax=ymax),alpha=0.5,fill="white",color=NA)+
#geom_area(aes(fill=variable),alpha=0.5,position="identity")+
geom_line(aes(y=AMZN,color="#FF6B5E"),size=0.75)+#color="black",
geom_line(aes(y=AAPL,color="#00B2F6"),size=0.75)+#color="black",
scale_x_date(date_labels = "%Y",date_breaks = "2 year")+
xlab("Year")+
ylab("Value")+
scale_colour_manual(name = "Variable",
labels = c("AMZN", "AAPL"),
values = c("#FF6B5E", "#00B2F6"))+
theme( axis.title=element_text(size=10,face="plain",color="black"),
axis.text = element_text(size=10,face="plain",color="black"),
legend.position = c(0.15,0.8),
legend.background = element_blank())
mydata1$ymin<-apply(mydata1[,c(2,3)], 1, min)
mydata1$ymax<-apply(mydata1[,c(2,3)], 1, max)
mydata1$ymin1<-mydata1$ymin
mydata1$ymin1[as.integer((mydata1$AAPL-mydata1$AMZN)>0)]=NA
mydata1$ymax1<-mydata1$ymax
mydata1$ymax1[as.integer((mydata1$AAPL-mydata1$AMZN)>0)==0]=NA
mydata1$ymin2<-mydata1$ymin
mydata1$ymin2[as.integer((mydata1$AAPL-mydata1$AMZN)<=0)==0]=NA
mydata1$ymax2<-mydata1$ymax
mydata1$ymax2[as.integer((mydata1$AAPL-mydata1$AMZN)<=0)==0]=NA
ggplot(mydata1, aes(x =date))+
geom_ribbon( aes(ymin=ymin1, ymax=ymax1),alpha=0.5,fill="#FF6B5E",color=NA)+#,fill = AMZN > AAPL
geom_ribbon( aes(ymin=ymin2, ymax=ymax2),alpha=0.5,fill="#00B2F6",color=NA)+#,fill = AMZN > AAPL
#geom_area(aes(fill=variable),alpha=0.5,position="identity")+
geom_line(aes(y=AMZN,color="#FF6B5E"),size=0.75)+#color="black",
geom_line(aes(y=AAPL,color="#00B2F6"),size=0.75)+#color="black",
scale_x_date(date_labels = "%Y",date_breaks = "2 year")+
xlab("Year")+
ylab("Value")+
scale_colour_manual(name = "Variable",
labels = c("AMZN", "AAPL"),
values = c("#FF6B5E", "#00B2F6"))+
theme( axis.title=element_text(size=10,face="plain",color="black"),
axis.text = element_text(size=10,face="plain",color="black"),
legend.position = c(0.15,0.8),
legend.background = element_blank())
library(ggridges)
ggplot(mydata1, aes(x=date)) +
geom_ridgeline_gradient( aes(y=ymin, height = ymax-ymin, fill = ymax-ymin)) +
geom_line(aes(y=AMZN),color="black",size=0.75)+#color="black",
geom_line(aes(y=AAPL),color="black",size=0.75)+#color="black",
scale_fill_gradientn(colours= brewer.pal(9,'RdBu'),name = "Value")+
theme(legend.position = c(0.15,0.8),
legend.background = element_blank())
堆积面积图
堆积柱形图和堆积面积图的差别在于,堆积面积图的 X 轴上只能表示连续数据(时间或者数值),堆积柱形图的 X 轴上只能表示分类数据。
ggplot2
包的geom_area()
函数可以绘制面积图系列。position="stack"
表示多数据系列的堆叠,position="full"
表示多数据系列以百分比的形式堆叠。
library(ggplot2)
mydata<-read.csv("StackedArea_Data.csv",stringsAsFactors=FALSE)
mydata$Date<-as.Date(mydata$Date)
mydata<-melt(mydata,id="Date")
ggplot(mydata, aes(x =Date, y = value,fill=variable) )+
geom_area(position="stack",alpha=1)+
geom_line(position="stack",size=0.25,color="black")+
scale_x_date(date_labels = "%Y",date_breaks = "2 year")
ggplot(mydata, aes(x =Date, y = value,fill=variable) )+
geom_area(position="stack",alpha=1)+
geom_line(position="stack",size=0.25,color="black")+
scale_x_date(date_labels = "%Y",date_breaks = "2 year")+
xlab("Year")+
ylab("Value")+
theme( axis.title=element_text(size=10,face="plain",color="black"),
axis.text = element_text(size=10,face="plain",color="black"),
legend.position = "right",
legend.background = element_blank())
ggplot(mydata, aes(x =Date, y = value,fill=variable) )+
geom_area(position="fill",alpha=1)+
geom_line(position="fill",size=0.25,color="black")+
scale_x_date(date_labels = "%Y",date_breaks = "2 year")+
xlab("Year")+
ylab("Value")+
theme( axis.title=element_text(size=10,face="plain",color="black"),
axis.text = element_text(size=10,face="plain",color="black"),
legend.position = "right",
legend.background = element_blank())
library(gcookbook)
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) +
geom_area(colour = "black", size = .2, alpha = .4) +
scale_fill_brewer(palette = "Blues")
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup, order = dplyr::desc(AgeGroup))) +
geom_area(colour = NA, alpha = .4) +
scale_fill_brewer(palette = "Blues") +
geom_line(position = "stack", size = .2)
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) +
geom_area(position = "fill", colour = "black", size = .2, alpha = .4) +
scale_fill_brewer(palette = "Blues")
With position="fill"
, the y values will be scaled to go from 0 to 1. To print the labels as percentages, use scale_y_continuous(labels = scales::percent)
:
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) +
geom_area(position = "fill", colour = "black", size = .2, alpha = .4) +
scale_fill_brewer(palette = "Blues") +
scale_y_continuous(labels = scales::percent)
参考资料
[1] https://r-graphics.org/recipe-bar-graph-labels
[2] https://github.com/EasyChart/Beautiful-Visualization-with-R
[3] R语言数据可视化之美:专业图表绘制指南(增强版) (张杰)