
image.png
在一個交流討論群里看到有人討論這個圖,很早之前就看到過這個圖,當時記得有一個現(xiàn)成的R包可以做。如果讓自己使用ggplot2來實現(xiàn)當時還真沒有思路。
現(xiàn)在有一些思路,這個就是點 和 線段 的組合,把握好坐標位置就好了
但是這個圖里的線段是帶有弧度的,之前畫圖的線段都是直線,所以就查了一下ggplot2畫帶有弧度的線段的辦法,找到了參考資料
Line segments and curves — geom_segment ? ggplot2 (tidyverse.org)
沒有弧度的線段使用的是geom_segment()函數(shù)
有弧度可以使用geom_curve()函數(shù)
下面是一個小例子
library(ggplot2)
b <- ggplot(mtcars, aes(wt, mpg)) +
geom_point()
df <- data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 15.0)
b +
geom_curve(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "curve"), data = df) +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "segment"), data = df)

image.png
這里有一個參數(shù)可以控制弧度curvature
library(ggplot2)
b <- ggplot(mtcars, aes(wt, mpg)) +
geom_point()
df <- data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 15.0)
b +
geom_curve(aes(x = x1,
y = y1,
xend = x2,
yend = y2,
colour = "curve"),
curvature = 1,
data = df) +
geom_segment(aes(x = x1,
y = y1,
xend = x2,
yend = y2,
colour = "segment"),
data = df) -> b1
b +
geom_curve(aes(x = x1,
y = y1,
xend = x2,
yend = y2,
colour = "curve"),
curvature = -1,
data = df) +
geom_segment(aes(x = x1,
y = y1,
xend = x2,
yend = y2,
colour = "segment"),
data = df) -> b2
library(patchwork)
b1/b2

image.png
取值范圍是-1到1,正負數(shù)代表的是弧度的方向
好了今天的內(nèi)容暫時先到這里了
歡迎大家關注我的公眾號
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號 主要分享:1、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡單小例子;2、園藝植物相關轉錄組學、基因組學、群體遺傳學文獻閱讀筆記;3、生物信息學入門學習資料及自己的學習筆記!