ggplot2 练习杂记 EXCEL 曲线图

Excel中曲线图如下:

想要在R ggplot2 中实现同样效果的图形

代码

library(xlsx)

library(ggplot2)

library(scales)

 channel <- read.xlsx("c:/myR/test.xlsx",sheetName="Sheet1",encoding="UTF-8")
  
channel$Name<-factor(channel$Name,levels=c("一级商国产品","进口商进口产品","TOP100国产品"))

p<-ggplot(channel,aes(x=AP,y=diff,group=Name,colour=Name))+geom_point()+ geom_smooth(size=1)+facet_grid(.~Name) 
p<-p+scale_x_discrete(breaks=c("1","2","3","4","5"),labels=c("AP1","-","-","-","AP5")) p<-p+scale_y_continuous(limits=c(0,.07),breaks=c(0,0.01,0.02,0.03,0.05),labels=percent_format()) p<-p+theme( panel.grid.major=element_blank(),panel.grid.minor=element_blank(),legend.position="none",axis.text.y=element_text(colour="darkred",size=rel(0.7)),axis.text.x =element_text(colour="darkred",size=rel(0.7))) p<-p+labs(y=NULL,x=NULL) p

结果

大致的图形已经实现了,但细节没有做太多调整。刚开始学,很费劲,尤其需要注意

当x=AP,这个AP列为文本型的时候显示的曲线是折线图并不平滑,可能类似如下

好吧,就酱,我并不明白为什么,待深入学习后再解答这个问题。

原文地址:https://www.cnblogs.com/LearningForR/p/4646125.html