0

I have the following problem - I need to print the name for each data point in a plot. I know that for that purpose I can use this function text(x, y, labels=row.names(myiris), cex = 0.7), but it gives me only the number of the row, not the name of the class. My data set looks like this:

    sepall sepalw petall petalw           class
1      5.1    3.5    1.4    0.2     Iris-setosa
2      4.9    3.0    1.4    0.2     Iris-setosa
3      4.7    3.2    1.3    0.2     Iris-setosa
4      4.6    3.1    1.5    0.2     Iris-setosa
5      5.0    3.6    1.4    0.2     Iris-setosa
6      5.4    3.9    1.7    0.4     Iris-setosa
7      4.6    3.4    1.4    0.3     Iris-setosa
8      5.0    3.4    1.5    0.2     Iris-setosa
9      4.4    2.9    1.4    0.2     Iris-setosa
10     4.9    3.1    1.5    0.1     Iris-setosa
11     5.4    3.7    1.5    0.2     Iris-setosa
12     4.8    3.4    1.6    0.2     Iris-setosa
13     4.8    3.0    1.4    0.1     Iris-setosa

and there are three options for the class - Setosa, Versicolour, Virginica. I want to get these as the names. I wan it to have the following structure:

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2

Can anyone help?

Teodora Georgieva
  • 145
  • 1
  • 1
  • 7
  • 1
    Have you tried `rownames(my_df) <- my_df$some_column`? – Scarabee Apr 02 '17 at 13:41
  • 1
    Possible duplicate of [Convert the values in a column into row names in an existing data frame in R](http://stackoverflow.com/questions/5555408/convert-the-values-in-a-column-into-row-names-in-an-existing-data-frame-in-r) – Scarabee Apr 02 '17 at 13:44
  • Thanks, but it gives the following error: Error in `row.names<-.data.frame`(`*tmp*`, value = value) : duplicate 'row.names' are not allowed – Teodora Georgieva Apr 02 '17 at 13:49
  • Duplicated row names are indeed not allowed. But do you really need to change row names? You can probably use `text(x, y, labels=my_df$some_column, cex = 0.7)`. – Scarabee Apr 02 '17 at 14:06
  • 5
    Have you tried to plot the contents of the `class` column directly instead of creating rownames and plotting these? I.e., `text(x, y, labels = myiris$class, cex = 0.7)` – Uwe Apr 02 '17 at 14:10
  • @Uwe Block has it right – jwells Apr 02 '17 at 14:12
  • 1
    Unrelated, but keep in mind that some of the more popular packages in use today do not use row names.... – A5C1D2H2I1M1N2O1R2T1 Apr 02 '17 at 14:18
  • @jwells Yeah, and BTW, the `data.table` package has abandoned to use rownames in favor of keyed or indexed columns. See [here](http://stackoverflow.com/q/24199533/3817004). – Uwe Apr 02 '17 at 14:22

0 Answers0