根据行/列在ggplot中的第n个元素上加上标签 [英] Label every nth element in ggplot depending on row/column

查看:81
本文介绍了根据行/列在ggplot中的第n个元素上加上标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用ggplot( t9 )绘制我的PCA数据(= pca.df ),并尝试标记我的数据点.彼此相邻的数据点超过500个,如果我对每个数据点进行标记,则标记会变得混乱,因此我只希望对第10或15个数据点进行标记.

I am plotting my PCA data (= pca.df) with ggplot (t9) and try to label my data points. With over 500 data points right next to each other, the labeling gets messy if I label every single one of them, so I want to label just every 10th or 15th data point.

我尝试使用gghighlight,但是我对ifelse语句的基本了解对我没有解决办法.

I tried using gghighlight, but my basic knowledge of ifelse statements leads to no solution for me.

我的(t9)代码在下面打印.结果应该是一个Score/Score-Plot,在轴上和每个轴上分别具有PC1PC2的值,可以说第5个元素根据行号进行了标记.

My code for (t9) is printed down below. The result should be a Score/Score-Plot with the values of PC1 and PC2 on the axes and every, lets say 5th element being labelled according to rownumber.

> dput(head(df))
structure(c(0.720861821444252, 0.741748310125995, 0.708402634437037, 
0.709801133484984, 0.715066449559326, 0.736082130721757, 0.653704393279032, 
0.698276222706427, 0.72211406630421, 0.715258352183308, 0.687541455566144, 
0.70691880577236, 0.648677410108962, 0.652162185222687, 0.673335514807695, 
0.686310528373139, 0.672288782506351, 0.685387769196099, 0.606520717118424, 
0.591747992573417, 0.637540141146717, 0.643172972625407, 0.631438730296424, 
0.684507675451748, 0.555771668718988, 0.593784695895615, 0.587382247577035, 
0.576747109369824, 0.609475457932671, 0.59866246596185, 0.519063669593429, 
0.530651696453032, 0.532787309576192, 0.541282412745153, 0.548262870872043, 
0.538801663579734, 0.4839370855738, 0.483476075398052, 0.495569187972609, 
0.508351252780073, 0.516728822566894, 0.536204665645588, 0.444866548123319, 
0.451359569708855, 0.488498854129039, 0.458566775758124, 0.466211854024398, 
0.491560347455407, 0.391256576032243, 0.407839446833575, 0.415186340652814, 
0.424286965099726, 0.411873304932725, 0.429761415133547, 0.339836624254536, 
0.368786621339532, 0.355816757014086, 0.378789270638685, 0.39335185857301, 
0.391171949534279, 0.301518457021116, 0.323263419961053, 0.319326889442142, 
0.329577970161398, 0.332317131236052, 0.328401547614829, 0.278194980292123, 
0.271241021424707, 0.278009324614118, 0.289181065286873, 0.282708689390152, 
0.287325696956387, 0.217765754577593, 0.229671138051344, 0.214270216785571, 
0.248548146541018, 0.242536426938002, 0.247615674841088, 0.161232560624038, 
0.167964791643414, 0.184493790352952, 0.169637631152996, 0.207171184044563, 
0.18671607328531, 0.136854807157759, 0.110836224164859, 0.141700425964129, 
0.170466088007464, 0.147693931044626, 0.161727182052285, 0.0820803384886784, 
0.094874042231684, 0.0838404020068718, 0.106453346170999, 0.0893222925431942, 
0.105425228691039, 0.0320549073043817, 0.031817899518242, 0.0492796841876071, 
0.0534417407043242, 0.0492509775017762, 0.0576741314907473, -0.0003305571982993, 
0.00517263223466776, 0.00898939160288738, 0.0137528774437919, 
0.0152605422021288, 0.0215229583963186, -0.0532648337441121, 
-0.0283369016701845, -0.0236614679612245, -0.0250738126949397, 
-0.0339585310595162, -0.00502894085262935, -0.0837238471177474, 
-0.0406195676958014, -0.0552223579150677, -0.0469508535844212, 
-0.0427121136759291, -0.0552519953457953, -0.099633841516817, 
-0.0838034423810929, -0.0891078733208937, -0.0847178896069714, 
-0.0416689904316896, -0.0845884534908026, -0.112879391070739, 
-0.116357070717273, -0.105954173536172, -0.0809224012272034, 
-0.0803030066250538, -0.0804524938179006, -0.141076501626193, 
-0.141494024691483, -0.133849678959928, -0.118385990843758, -0.113423371384572, 
-0.123928162937715, -0.145978888454582, -0.145163933507897, -0.155263241214673, 
-0.139704305017438, -0.119946496925743, -0.143872145220887, -0.157460920557041, 
-0.172462482989155, -0.161340062935618, -0.160945553437212, -0.142106857091405, 
-0.146243108149656), .Dim = c(6L, 25L), .Dimnames = list(NULL, 
    c("1360", "1361", "1362", "1363", "1364", "1365", "1366", 
    "1367", "1368", "1369", "1370", "1371", "1372", "1373", "1374", 
    "1375", "1376", "1377", "1378", "1379", "1380", "1381", "1382", 
    "1383", "1384")))

pca.df = prcomp(na.omit(df), center = FALSE)

t9 = ggplot(get_pca_var(pca.df)$coord, aes(x = get_pca_var(pca.df)$coord[,1], y = get_pca_var(pca.df)$coord[,2])) +
  xlab("PC1") + ylab("PC2") + 
  geom_point() +
  geom_text(aes(label=seq(1:nrow(get_pca_var(pca.df)$coord)), hjust=0, vjust=5))

t9

这给出了以下图解,但每个元素都标有标签:

This gives the following plot, but with every element labelled:

作为结果,我想要这样的代码部分,但是要使用ifelsefor语句(每第n行/列,例如下面的"try"中的第5个元素)突出显示标签:

I want some code part like this as a result, but with the ifelse or for statement (every nth row/column, for example 5th element in my "try" below) to highlight the labels:

b = as.numeric(rownames(as.data.frame(iris.pca$x)))
ggplot(iris.pca) +
  geom_line(aes(x, y, colour = colour)) +
  gghighlight
 ""(for (b in PC1) { 
   if (x%%5 == 0) {
     print(label = as.numeric(rowname))
   } else (no label)
 }""

感谢您的帮助!

推荐答案

我之前应该添加几行.这与您的想法类似.

I would have added few lines before. It is similar to your idea.

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))

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

Please let us know if this is what you wanted.

这篇关于根据行/列在ggplot中的第n个元素上加上标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆