r语言 - 如何在 ggplot 中注释geom_segment箭头



我有一个数据帧:

df_sites <- structure(list(x = c(1.04092250164696, -0.383065216420003, 0.396244810279309, 
0.970078841220606, 1.70624019153651, 3.16514402752826, 0.683787687531189, 
0.00206174639359557, 0.885459199930364, 0.990634067372794, 0.228548628266029, 
5.12827669944002, 0.0950586619539368, -0.275846514997531, 1.5525132408558, 
-1.29950430377717, -0.990922674400145, 0.185830660119637, 0.00602127943634668, 
-1.02247155743703, -0.251974618425098, 1.87788540164332, 1.28325669941297, 
1.02150538568984, -0.865622294371786, -1.96452990510675, -0.524866180755096, 
2.17941326700128, -1.34324588367972, -1.81439562296687, -1.13470999575871, 
-0.493658775981049, -0.296149601541577, 0.447503914837335, -0.269452469430389, 
0.0127337699647291, -1.04287439571777, -0.613105026144241, -1.3890917214799, 
-1.90860630718699, -1.16104734632228, -0.584089855574213, -1.2278237710839, 
-0.937664406699838, 1.09181991754655, -0.565406792755387, -0.58204838078486, 
0.842304932110318), y = c(-3.45147995394394, 2.29349807839102, 
0.174644402446899, 3.8468101986443, 2.6412842200453, -0.0665028396276639, 
2.05491741522117, 0.165875878990559, -0.25539122973085, 1.74130285620058, 
0.396659954165391, -1.65827015730937, 1.17736501075071, -3.72087159136532, 
1.89896109873428, 1.68766224921712, -2.92368548480463, -2.42481488216442, 
2.20648524060166, -0.486513106980203, 2.05729614246768, 2.51807338395106, 
1.9974880289267, -2.67208900165781, -0.749156762561599, 1.93100782500476, 
-4.15965374769117, 3.64156647300722, -2.7010471123406, 0.198076035987165, 
1.62736086278764, -1.03740092888219, -3.89989372202828, -0.213429351502094, 
-0.408170753360095, -1.61011027424538, -0.213306102694109, -0.154504840231308, 
0.118730504697768, 1.91054431185776, 0.255125262080179, 0.612701198243207, 
-1.21511378377373, 3.29282161162431, 2.50675599190964, -3.80136136529774, 
-1.28545510252701, 3.02158440057367), Sites = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "M", class = "factor")), row.names = c("M.T1.R1.S1.16S.S50", 
"M.T1.R1.S2.16S.S62", "M.T1.R1.S3.16S.S74", "M.T1.R2.S1.16S.S86", 
"M.T1.R2.S2.16S.S3", "M.T1.R2.S3.16S.S15", "M.T1.R3.S1.16S.S27", 
"M.T1.R3.S2.16S.S39", "M.T1.R3.S3.16S.S51", "M.T1.R4.S1.16S.S63", 
"M.T1.R4.S2.16S.S75", "M.T1.R4.S3.16S.S87", "M.T2.R1.S1.16S.S53", 
"M.T2.R1.S2.16S.S65", "M.T2.R1.S3.16S.S77", "M.T2.R2.S1.16S.S89", 
"M.T2.R2.S2.16S.S6", "M.T2.R2.S3.16S.S18", "M.T2.R3.S1.16S.S30", 
"M.T2.R3.S2.16S.S42", "M.T2.R3.S3.16S.S54", "M.T2.R4.S1.16S.S66", 
"M.T2.R4.S2.16S.S78", "M.T2.R4.S3.16S.S90", "M.T3.R1.S1.16S.S56", 
"M.T3.R1.S2.16S.S68", "M.T3.R1.S3.16S.S80", "M.T3.R2.S1.16S.S92", 
"M.T3.R2.S2.16S.S9", "M.T3.R2.S3.16S.S21", "M.T3.R3.S1.16S.S33", 
"M.T3.R3.S2.16S.S45", "M.T3.R3.S3.16S.S57", "M.T3.R4.S1.16S.S69", 
"M.T3.R4.S2.16S.S81", "M.T3.R4.S3.16S.S93", "M.T4.R1.S1.16S.S59", 
"M.T4.R1.S2.16S.S71", "M.T4.R1.S3.16S.S83", "M.T4.R2.S1.16S.S95", 
"M.T4.R2.S2.16S.S12", "M.T4.R2.S3.16S.S24", "M.T4.R3.S1.16S.S36", 
"M.T4.R3.S2.16S.S48", "M.T4.R3.S3.16S.S60", "M.T4.R4.S1.16S.S72", 
"M.T4.R4.S2.16S.S193", "M.T4.R4.S3.16S.S203"), class = "data.frame")

我将其绘制为

p<-ggplot() 
p<-p+geom_point(data=df_sites,aes(x,y,colour=Sites), shape = "diamond", size = 5)
df_arrows <- structure(list(x = c(-0.0506556191949347, -0.248732307259684, 
0.75), y = c(-0.669658874134264, -0.45802558549515, -0.110871926510315
)), class = "data.frame", row.names = c("`POX-C`", "Protein", 
"yield"))
p+geom_segment(data=df_arrows, aes(x = 0, y = 0, xend = x, yend = y),
arrow = arrow(length = unit(0.2, "cm")))

我想为这些箭头添加注释。我该怎么做?

我们可以使用geom_text和包含在df_arrows中的数据:

library(dplyr) # get %>% and mutate
p <- p+geom_segment(data=df_arrows, aes(x = 0, y = 0, xend = x, yend = y),
arrow = arrow(length = unit(0.2, "cm")))
p + geom_text(data = df_arrows %>% mutate(labs = row.names(.)), 
aes(x = x, y = y, label = labs))

如果你想让剧情在眼睛上更容易一点,避免在事情上绘图,你可以尝试ggrepel包中的geom_text_repel功能。

最新更新