问题
I create a plot with the following dataset and I would like to add a label only for points flagged with a T in the "DisplayName" column.
Probe Name DisplayName X Y
bob1 A 0 53.989643 7935.185
bob2 B T 55.11423 7930.626
bob3 C 0 49.537724 6901.7715
bob4 D 0 57.280113 6687.0156
bob5 E T 7.5517325 840.3756
bob6 F 0 62.68943 6666.6665
bob7 G T 32.553364 3036.508
bob8 H 0 34.120102 2553.5354
bob9 I 0 127.54777 7818.89
My idea would be to use text() and which() to add the value of "Name" but I am stuck with something like this:
plot(data$X, data$Y)
text(data$X, data$Y, d$Name[which(d$DisplayName =="T", arr.ind=TRUE)])
Any help will be appreciated,
Cheers ;)
回答1:
You can combine subset()
and with()
to use only part of data frame for function text()
.
plot(df$X, df$Y)
with(subset(df,DisplayName=="T"),text(X,Y,Name))
回答2:
You can do this most simply (but perhaps not as elegantly as Didzis' answer) as follows:
plot(data$X, data$Y)
text(data$X[d$DisplayName =="T"], data$Y[d$DisplayName =="T"], d$Name[d$DisplayName =="T"])
来源:https://stackoverflow.com/questions/17174090/add-labels-to-plot-for-specific-values-in-r