Search code examples
rcluster-analysis

R clustering PAM add country labels to clusplot


I am trying to add country labels to a clusplot and cant seem to get the plot to display anything other than the numbers, whereas I need the respective countries to show.

Here is the line of code I am trying to work with

fvi.use = exampledata[,-c(1)]
medians = apply(fvi.use,2,median)
mads = apply(fvi.use,2,mad)
cars.use = scale(fvi.use,center=medians,scale=mads)
cars.dist = dist(fvi.use)
cars.hclust = hclust(cars.dist)
plot(cars.hclust,labels=exampledata$Country,main='Heirarchial clustering')

cars.pam = pam(cars.dist,5)
names(cars.pam)
clusplot(cars.pam, attr(exampledata$Country,"Labels"), labels=5)
plot(cars.pam, labels=exampledata$Country)
mosaicplot(cars.use)
clusplot(cars.pam, attr(exampledata$Country,"Labels"), labels=5)

example data is a matrix with a country name in the first column, and then three more columns of data I would like to do the cluster analysis on

Any help would be appreciated


Solution

  • From your description

    example data is a matrix with a country name in the first column, and then and then three more columns of data I would like to do the cluster analysis on

    I assume that you want to label the points with country names. the documentation for clusplot.default says

    The labels of the points are the rownames of x if x is matrix like.

    So to get that, you need to set the row names to the countries that you want displayed. Here is an example with junk data.

    library(cluster)
    
    Countries = c('Afghanistan', 'Albania', 'Algeria',
        'Andorra', 'Angola','Antigua', 'Argentina',
        'Armenia',   'Aruba', 'Australia', 'Austria', 'Azerbaijan')
    
    set.seed(2017)
    Data = data.frame(Countries, iris[sample(150,12), 2:4])
    rownames(Data) = Countries
    PAM3 =pam(Data[2:4], 3)
    clusplot(PAM3, labels=3, lines=0)
    

    Clusplot with labeled points