Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I include the row names of a dataframe instead of only the numbers using ggplot2?

I have a dataset that I am working with, and have performed a k-means clustering analysis. I am trying to generate the cluster plot graph, but cannot seem to re-label the graph to include the row names (and not the numbers). How can I do this? I do not want the row numbers in the image.

Here is the data and code (data has already been normalized for ease of analysis):

# Open libraries
library(tidyverse)
library(ggplot2)
library(cluster)
library(factoextra)

# Data
data <- data.frame(label = c('K1',
                          'K2',
                          'K3',
                          'K4',
                          'K5',
                          'K6',
                          'K7',
                          'K8',
                          'K9',
                          'K10',
                          'K11',
                          'K12',
                          'K13',
                          'K14',
                          'K15',
                          'K16',
                          'K17',
                          'K18',
                          'K19',
                          'K20'),
                price = c(-0.624434769750764,
                          -0.536599938797358,
                          -0.644183377954367,
                          1.89585408542367,
                          -0.982659691833129,
                          -0.835733232884629,
                          -1.10200036294203,
                          1.0702916454921,
                          -0.845431392672383,
                          -0.60717549444702,
                          -0.772305180090069,
                          0.489522844465606,
                          0.49759411259524,
                          0.567213760967889,
                          0.41156375566992,
                          1.76934999015057,
                          -0.548828699133812,
                          -0.580000479793385,
                          -0.517891659889718,
                          1.89585408542367),
                control = c(0.707555348515056,
                            0.338078487882797,
                            0.0631189636913435,
                            0.939552447051593,
                            -0.8648694304548,
                            -1.09400236728101,
                            -0.478207599560573,
                            -0.452430144167626,
                            0.887997536265696,
                            -3.03017568346247,
                            -0.607094876525316,
                            0.887997536265696,
                            1.25461023518763,
                            0.137587168159862,
                            1.0541189154647,
                            0.456225158433809,
                            -0.0342625344597922,
                            -0.549811642318762,
                            -0.400875233381728,
                            0.784887714693904))

# Assign row names
rownames(data) <- data$label
data <- data[,-1]

# Computing k-means clustering
k3 <- kmeans(data, centers = 3, nstart = 25)

# Produce cluster plot
fviz_cluster(k2, data = soy)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Are you trying to obtain something like this?

enter image description here

Heres my code:

k3 <- kmeans(data, centers = 3, nstart = 25)


fviz_cluster(k3, 
             data = data, 
             geom = "point", 
             ggtheme = theme_minimal()) +
  geom_text(aes(label = rownames(data)),
            vjust = 1.5, 
            hjust = 0.5, 
            size = 3) +
  labs(title = "K-Means Clustering")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading