根据行/列标记ggplot中的第n个元素

时间:2019-07-11 17:06:55

标签: r if-statement ggplot2

我正在用ggplot(地名= t9 )绘制我的PCA数据(= pca.183s ),并尝试标记我的数据点。紧挨着500个以上的数据点,如果我对它们中的每一个进行标记,则标记会变得混乱,因此我只希望对第10或15个数据点进行标记。

我尝试使用gghighlight,但是我对ifelse语句的基本了解对我来说没有解决方法。

我的dput(t9)打印在下面。请注意,我从标记为“ [...]”的数据中裁剪了一些尺寸(列)。复制并粘贴后删除此部分,以免出错。

>dput(t9)
structure(list(data = structure(list(Dim.1 = c(-2.64734354178624, 
-2.684453957401, -2.72103824389099, -2.7573690538765, -2.79350662742449
), Dim.2 = c(-0.00690584020321681, -0.0073201848208554, -0.00665742324572404, 
-0.00701883060121352, -0.00841150808774304), Dim.3 = c(-0.000412824722174013, 
-0.00187772191064055, -0.00153941308232753, -0.0017079820861918, 
-0.00185339773024843), Dim.4 = c(-0.00379974637879092, -0.00407751816939334, 
-0.00502928751035698, -0.0047605647231583, -0.00436471516808505
), Dim.5 = c(0.000319419245109297, 0.000818960273297045, 0.0011031727317379, 
0.000386117109515785, 0.000509290349614579), Dim.6 = c(-0.00114820939204092, 
-0.00236940468999681, -0.00188089323617931, -0.000723357169355533, 
-0.000403598450968381), Dim.7 = c(-0.00135837604359201, -0.00183962442768656, 
-0.00147248928991352, -0.00228101038375798, -0.0027097346141399
), Dim.8 = c(-0.0028398473163052, -0.00231692383491865, -0.00290805988706844, 
-0.00259298630543098, -0.00319397304905035), Dim.9 = c(-0.00118198348599521, 
-0.00107699810223108, -0.00143857323344683, -0.00117982058584542, 
-0.00211841077026476), Dim.10 = c(0.000411590953489695, -0.00116304388256147, 
-0.000275055364941577, -0.00102210770014903, -0.00090707036026422
), **[...]** Dim.280 = c(-0.000182139854340029, -5.01542241348593e-05, 
    4.90052942830289e-05, 7.22622620807921e-05, 9.64644303427084e-05
    ), Dim.281 = c(-6.69341869237857e-16, -6.69341869236451e-16, 
    -6.69341869236567e-16, -6.69341869237628e-16, -6.69341869236464e-16
    )), row.names = c("1636", "1637", "1638", "1639", "1640"), class = "data.frame"), 
    layers = list(<environment>, <environment>, <environment>, 
        <environment>), scales = <environment>, mapping = structure(list(
        x = ~get_pca_var(pca.183s)$coord[, 1], y = ~get_pca_var(pca.183s)$coord[, 
            2]), class = "uneval"), theme = list(), coordinates = <environment>, 
    facet = <environment>, plot_env = <environment>, labels = list(
        y = "PC2", x = "PC1", label = "seq(1:nrow(get_pca_var(pca.183s)$coord))", 
        hjust = "hjust", vjust = "vjust")), class = c("gg", "ggplot"
))

作为结果,我需要这样的代码部分,但是使用ifelsefor语句(每第n行/列)突出显示标签:

ggplot(df) +
  geom_line(aes(x, y, colour = colour)) +
  gghighlight("statement")

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我之前会添加几行。这与您的想法类似。

Every_Nth_label <- 5
Index <- 1
Label_full <- c(1:length(get_pca_var(pca.183s)$coord[,1]))
for(I in Label_full){
  if (Index %% Every_Nth_label != 0){
    Label_full[Index] <- ""
  }
  Index <- Index + 1
}
t9 = ggplot(get_pca_var(pca.183s)$coord, 
aes(
x = get_pca_var(pca.183s)$coord[,1], 
y = get_pca_var(pca.183s)$coord[,2])
) + 
xlab("PC1") + 
ylab("PC2") +
geom_point() +
geom_text(aes(label=Label_full, hjust=0, vjust=5))

请告诉我们您是否要这样做。