溫馨提示×

溫馨提示×

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

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

如何利用R語言的ggplot2包繪制折線圖

發布時間:2021-12-14 10:45:17 來源:億速云 閱讀:1483 作者:小新 欄目:大數據

這篇文章將為大家詳細講解有關如何利用R語言的ggplot2包繪制折線圖,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

一  繪制單條折線圖

載入數據及函數包

library(ggplot2)df <- data.frame(dose=c("A", "B", "C"), len=c(5.16, 10.10, 30))head(df)  dose   len1    A  5.162    B 10.103    C 30.00

1.1  繪制基本的折線圖

ggplot(data=df, aes(x=dose, y=len, group=1)) +geom_line()

如何利用R語言的ggplot2包繪制折線圖

1.2 添加點,并更改線型 和顏色

ggplot(data=df, aes(x=dose, y=len, group=1)) +geom_line(linetype = "dashed",color="red")+ geom_point()

如何利用R語言的ggplot2包繪制折線圖

1.3 添加箭頭

library(grid)ggplot(data=df, aes(x=dose, y=len, group=1))+geom_line(arrow = arrow())+geom_point()#自定義箭頭類型myarrow=arrow(angle = 15, ends = "both", type = "closed")ggplot(data=df, aes(x=dose, y=len, group=1)) +geom_line(arrow=myarrow)+geom_point()

如何利用R語言的ggplot2包繪制折線圖

1.4 附贈

ggplot(data=df, aes(x=dose, y=len, group=1)) + geom_step()+ geom_point()

如何利用R語言的ggplot2包繪制折線圖

注:因為橫坐標的屬性為因子(離散型的字符轉換為因子),所以需要添加‘group = 1’的設置。

二 繪制多條折線圖

設置數據

df2 <- data.frame(supp=rep(c("Case", "Control"), each=3), dose=rep(c("A", "B", "C"),2),len=c(6.8, 15, 38, 5.16, 10.10, 30))head(df2)     supp dose   len1    Case    A  6.802    Case    B 15.003    Case    C 38.004 Control    A  5.165 Control    B 10.106 Control    C 30.00

2.1 繪制多條折線圖,更改線型

ggplot(data=df2, aes(x=dose, y=len, group=supp))+  geom_line(linetype="dashed", color="blue", size=1.2)+  geom_point(color="red", size=3)

如何利用R語言的ggplot2包繪制折線圖

2.2 分組更改線型和點的形狀

ggplot(df2, aes(x=dose, y=len, group=supp)) +  geom_line(aes(linetype=supp))+  geom_point(aes(shape=supp))

2.3 自定義更改線型

ggplot(df2, aes(x=dose, y=len, group=supp)) +  geom_line(aes(linetype=supp))+  geom_point()+  scale_linetype_manual(values=c("twodash", "dotted"))

如何利用R語言的ggplot2包繪制折線圖

2.4 更改顏色

p <- ggplot(df2, aes(x=dose, y=len, group=supp)) +  geom_line(aes(color=supp))+  geom_point(aes(color=supp))p

如何利用R語言的ggplot2包繪制折線圖

其他自定義顏色方式:

# Use custom color palettesp+scale_color_manual(values=c("#E69F00", "#56B4E9"))# Use brewer color palettesp+scale_color_brewer(palette="Dark2")# Use grey scalep +scale_color_grey() + theme_classic()

如何利用R語言的ggplot2包繪制折線圖

2.5 添加誤差棒

利用ToothGrowth數據集,首先分組計算每一分組的均值和標準差,整理成如下格式:

supp dose   len       sd1   OJ  0.5 13.23 4.4597092   OJ  1.0 22.70 3.9109533   OJ  2.0 26.06 2.6550584   VC  0.5  7.98 2.7466345   VC  1.0 16.77 2.5153096   VC  2.0 26.14 4.797731

繪制添加誤差棒的折線圖

ggplot(df3, aes(x=dose, y=len, group=supp, color=supp)) +     geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1) +    geom_line() +geom_point()+   scale_color_brewer(palette="Paired")+theme_minimal()

如何利用R語言的ggplot2包繪制折線圖

注:可以使用position_dodge 參數,防止errorbars重疊

三 折線圖匯總展示

ggplot(df3, aes(x=dose, y=len, group = supp, color=supp))+     geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1,     position=position_dodge(0.05)) +    geom_line(aes(linetype=supp)) +     geom_point(aes(shape=supp))+    labs(title="Plot of lengthby dose",x="Dose (mg)", y = "Length")+    theme_classic()+scale_color_manual(values=c('#999999','#E69F00'))

如何利用R語言的ggplot2包繪制折線圖

關于“如何利用R語言的ggplot2包繪制折線圖”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

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

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女