Question

Je suis nouveau dans R et je voudrais savoir comment étiqueter les points de données sur un diagramme de dispersion. J'ai essayé le code suivant mais j'obtiens une erreur.

x = c(102856,17906,89697,74384,91081,52457,73749,29910,75604,28267,122136,
      54210,48925,58937,76281,67789,69138,18026,90806,44893)
y = c(2818, 234, 2728, 2393, 2893, 1015, 1403, 791, 2243, 596, 2468, 1495,
      1232, 1746, 2410, 1791, 1706, 259, 1982, 836)

plot(x, y, main="Scatterplot ", xlab="xaxis ", ylab="yaxis ", pch=19)

names = c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R",
          "T","W","V","Y")

library(calibrate)
textxy(x, y, labs=names, cx = 0.5, dcol = "black", m = c(0, 0))

Error in text.default(X[posXposY], Y[posXposY], labs[posXposY], adj = c(-0.3,  :
plot.new has not been called yet

Je ne comprends pas cette erreur. Aidez-moi, s'il vous plaît

Était-ce utile?

La solution

Tu pourrais le faire dans ggplot2:

require(ggplot2)
d <- data.frame(x = c(102856,17906,89697,74384,91081,52457,73749,29910,75604,28267,122136, 54210,48925,58937,76281,67789,69138,18026,90806,44893), y = c(2818, 234, 2728, 2393, 2893, 1015, 1403, 791, 2243, 596, 2468, 1495, 1232, 1746, 2410, 1791, 1706, 259, 1982, 836), names = c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R","T","W","V","Y"))
ggplot(d, aes(x,y)) + geom_point() + geom_text(aes(label=names))

Vous voudrez peut-être que les étiquettes de texte ne soient pas directement au-dessus des points, que vous pourriez accomplir en utilisant le hjust ou vjust arguments dans le geom_text partie.

Autres conseils

Vous pouvez facilement créer cela en utilisant le text() fonction.

text(x,y,labels=names)

Votre code fonctionne pour moi avec:

> sessionInfo()
R version 2.14.2 Patched (2012-02-29 r58525)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
 [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8    
 [5] LC_MONETARY=en_GB.utf8    LC_MESSAGES=en_GB.utf8   
 [7] LC_PAPER=C                LC_NAME=C                
 [9] LC_ADDRESS=C              LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C      

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] calibrate_1.7 mgcv_1.7-13  

loaded via a namespace (and not attached):
[1] grid_2.14.2    lattice_0.20-0 Matrix_1.0-4   nlme_3.1-103  
[5] tools_2.14.2

Vérifiez que vous avez un R à jour et une version de étalonner et s'il ne les met pas à jour et réessayez.

Il serait plus naturel d'utiliser l'ordre suivant de vos appels:

> library(calibrate)
> names = c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R",
+           "T","W","V","Y")
> plot(x, y, main="Scatterplot ", xlab="xaxis ", ylab="yaxis ", pch=19)
> textxy(x, y, labs=names, cx = 0.5, dcol = "black", m = c(0, 0))

Cela ne devrait pas faire de différence si la fenêtre de tracé produit par le plot() L'appel est toujours ouvert.

Vous n'avez pas besoin du package de calibre. Tu peux faire:

texte (x, y-50, noms)

Cela fonctionne pour moi.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top