溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何用R語言ggplot2畫折線圖并添加誤差線

發(fā)布時間:2021-11-22 15:39:53 來源:億速云 閱讀:2633 作者:柒染 欄目:大數(shù)據(jù)

本篇文章給大家分享的是有關如何用R語言ggplot2畫折線圖并添加誤差線,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

折線圖添加誤差線是非常常用的一種可視化方法,今天的推文介紹一下使用R語言的ggplot2作圖的代碼。模仿的是論文 Phased diploid genome assemblies and pan-genomes provide insights into the genetic history of apple domestication 中的Figure3中的d圖

如何用R語言ggplot2畫折線圖并添加誤差線  
image.png
 第一步是準備數(shù)據(jù)
如何用R語言ggplot2畫折線圖并添加誤差線  
image.png

數(shù)據(jù)保存為csv格式

df<-read.csv("line.csv",header=T)
df
   基本的折線圖
library(ggplot2)
head(df)
ggplot(df,aes(x=time_point,y=value))+
  geom_line()+
  geom_point()+
  ylim(0,40)
 
如何用R語言ggplot2畫折線圖并添加誤差線  
image.png
 添加誤差線
ggplot(df,aes(x=time_point,y=value))+
  geom_line()+
  geom_point()+
  ylim(0,40)+
  geom_errorbar(aes(ymin=value-sd,
                    ymax=value+sd),
                width=0.2)
 
如何用R語言ggplot2畫折線圖并添加誤差線  
image.png
 這樣基本就做好了,接下來就是美化
  • 去掉灰色背景
  • 添加坐標軸線
  • 更改坐標軸的刻度和標簽
ggplot(df,aes(x=time_point,y=value))+
  geom_line()+
  geom_point()+
  ylim(0,40)+
  geom_errorbar(aes(ymin=value-sd,
                    ymax=value+sd),
                width=0.2)+
  theme(panel.background = element_blank(),
        axis.line.x = element_line(),
        axis.line.y = element_line())+
  scale_x_continuous(breaks = 1:13,
                     labels = df$label)+
  labs(x=NULL,y=NULL,title = "AAA")
 
如何用R語言ggplot2畫折線圖并添加誤差線  
image.png
 接下來是拼圖
p1<-ggplot(df,aes(x=time_point,y=value))+
  geom_line()+
  geom_point()+
  ylim(0,40)+
  geom_errorbar(aes(ymin=value-sd,
                    ymax=value+sd),
                width=0.2)+
  theme(panel.background = element_blank(),
        axis.line.x = element_line(),
        axis.line.y = element_line())+
  scale_x_continuous(breaks = 1:13,
                     labels = df$label)+
  labs(x=NULL,y=NULL,title = "AAA")
library(cowplot)
plot_grid(p1,p1,p1,p1,p1,p1,ncol=2,nrow=3)
 
如何用R語言ggplot2畫折線圖并添加誤差線   

以上就是如何用R語言ggplot2畫折線圖并添加誤差線,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI