R语言绘制面积图系列

临风暖阳 发表于 2022/11/27 02:43:18 2022/11/27
【摘要】 笔者使用R语言绘制面积图系列

library(ggplot2)
library(RColorBrewer)
library(reshape2)


#-------------------------图6-1-1 多数据系列图. (a)折线图-------------------------
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(size=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()) 

#-------------------------图6-1-1 多数据系列图.(b)面积图.-------------------------
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()) 

#--------------------------------------图6-1-2 填充面积折线图. (a)纯色填充-------------------
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")) 


#------------------------图6-1-2 填充面积折线图.(b)颜色映射填充.------------------------

x<-as.numeric(mydata$date)
newdata<-data.frame(spline(x,mydata$value,n=1000,method= "natural"))
newdata$date<-as.Date(newdata$x,origin = "1970-01-01")
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() )

#------------------------图6-1-2 填充面积折线图.(b)颜色映射填充.------------------------

x<-as.numeric(mydata$date)
newdata<-data.frame(spline(x,mydata$value,n=1000,method= "natural"))
newdata$date<-as.Date(newdata$x,origin = "1970-01-01")
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,'Set1'),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() )


#------------------------图6-1-3 夹层填充面积图. (a)单色------------------------------

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

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="#00FFFF"),size=0.75)+#color="black",
  geom_line(aes(y=AAPL,color="#7FFFD4"),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("#00FFFF", "#7FFFD4"))+
   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()) 

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="#0000FF"),size=0.75)+#color="black",
  geom_line(aes(y=AAPL,color="#8A2BE2"),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("#0000FF", "#8A2BE2"))+
   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()) 

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="#A52A2A"),size=0.75)+#color="black",
  geom_line(aes(y=AAPL,color="#DEB887"),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("#A52A2A", "#DEB887"))+
   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()) 

#------------------------图6-1-3 夹层填充面积图.  (b)多色------------------------------
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()) 

ggplot(mydata1, aes(x =date))+
  geom_ribbon( aes(ymin=ymin1, ymax=ymax1),alpha=0.5,fill="#5F9EA0",color=NA)+#,fill = AMZN > AAPL
  geom_ribbon( aes(ymin=ymin2, ymax=ymax2),alpha=0.5,fill="#7FFF00",color=NA)+#,fill = AMZN > AAPL
  #geom_area(aes(fill=variable),alpha=0.5,position="identity")+ 
  geom_line(aes(y=AMZN,color="#5F9EA0"),size=0.75)+#color="black",
  geom_line(aes(y=AAPL,color="#7FFF00"),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("#5F9EA0", "#7FFF00"))+
  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()) 

ggplot(mydata1, aes(x =date))+
  geom_ribbon( aes(ymin=ymin1, ymax=ymax1),alpha=0.5,fill="#D2691E",color=NA)+#,fill = AMZN > AAPL
  geom_ribbon( aes(ymin=ymin2, ymax=ymax2),alpha=0.5,fill="#6495ED",color=NA)+#,fill = AMZN > AAPL
  #geom_area(aes(fill=variable),alpha=0.5,position="identity")+ 
  geom_line(aes(y=AMZN,color="#D2691E"),size=0.75)+#color="black",
  geom_line(aes(y=AAPL,color="#6495ED"),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("#D2691E", "#6495ED"))+
  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()) 

#--------------------------------图6-1-3 夹层填充面积图.(c)颜色映射填充.------------------------------------------
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()) 

#--------------------------------图6-1-3 夹层填充面积图.(c)颜色映射填充.------------------------------------------
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,'Set1'),name = "Value")+
  theme(legend.position = c(0.15,0.8),
        legend.background = element_blank()) 

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区),文章链接,文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。