Mark Manger
2014-Apr-17 03:01 UTC
[R] Labeling/identifying observations in plot after MclustDR from Mclust
Hi, I?m trying to figure out how to label points in a contour plot produced from the output of MclustDR, the dimension reduction function in the Mclust package. The original data frame has row names RRcoarse govtDeficit res_gdp GrossFixedCapForm_UN fuelExports AUS_76 2.000 -4.5125520 1.5197260 26.5169 12.7851900 AUS_81 3.500 -3.6041720 3.7029600 27.1058 20.8597300 AUS_91 4.000 -1.2131010 3.8150380 24.1444 20.5832700 AUS_01 4.000 0.0279132 4.4617860 26.9491 25.5349700 AUT_76 2.000 -2.7850080 7.0378630 25.7365 1.6797000 AUT_81 1.000 -3.4403770 6.4757080 23.2928 1.4688700 I then use these variables to imbalances.selections.def = subset(imbalances.all, select=c(5:8,10:12), cu_gdp < 0) clusters.defs = Mclust(imbalances.selections.def, G=1:10) I use MclustDR to reduce the dimensions to plot the results, which works beautifully and produces a nice contour plot. dr.defs = MclustDR(clusters.defs) plot(dr.defs, what="contour?) But the plot is not particularly informative unless I know which observation is which. How can I label the points displayed in the plot to identify the observations? I can probably extract this from the dr.defs object whose str() is below, but I don?t know how to do that, or somehow from the original data frame row names. Help would be greatly appreciated. I know this is trivial in a regular plot(x,y) situation. str(dr.defs) List of 22 $ call : language MclustDR(object = clusters.defs) $ type : chr "Mclust" $ x : num [1:49, 1:7] 2 3.5 4 4 2 1 1 2 2 2 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:49] "AUS_76" "AUS_81" "AUS_91" "AUS_01" ... .. ..$ : chr [1:7] "RRcoarse" "govtDeficit" "res_gdp" "GrossFixedCapForm_UN" ... $ Sigma : num [1:7, 1:7] 0.875 0.116 -0.781 -0.525 2.339 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:7] "RRcoarse" "govtDeficit" "res_gdp" "GrossFixedCapForm_UN" ? etc etc etc Many thanks in advance, Mark S. Manger Assistant Professor Munk School of Global Affairs and Department of Political Science University of Toronto Observatory Site | 315 Bloor Street West | Room 212 Toronto, ON M5S 0A7 mark.manger at utoronto.ca
Jim Lemon
2014-Apr-22 03:30 UTC
[R] Labeling/identifying observations in plot after MclustDR from Mclust
On 04/17/2014 01:01 PM, Mark Manger wrote:> Hi, > > I?m trying to figure out how to label points in a contour plot produced from the output of MclustDR, the dimension reduction function in the Mclust package. > > The original data frame has row names > > RRcoarse govtDeficit res_gdp GrossFixedCapForm_UN fuelExports > AUS_76 2.000 -4.5125520 1.5197260 26.5169 12.7851900 > AUS_81 3.500 -3.6041720 3.7029600 27.1058 20.8597300 > AUS_91 4.000 -1.2131010 3.8150380 24.1444 20.5832700 > AUS_01 4.000 0.0279132 4.4617860 26.9491 25.5349700 > AUT_76 2.000 -2.7850080 7.0378630 25.7365 1.6797000 > AUT_81 1.000 -3.4403770 6.4757080 23.2928 1.4688700 > > I then use these variables to > > imbalances.selections.def = subset(imbalances.all, select=c(5:8,10:12), cu_gdp< 0) > clusters.defs = Mclust(imbalances.selections.def, G=1:10) > > > I use MclustDR to reduce the dimensions to plot the results, which works beautifully and produces a nice contour plot. > > dr.defs = MclustDR(clusters.defs) > plot(dr.defs, what="contour?) > > But the plot is not particularly informative unless I know which observation is which. > > How can I label the points displayed in the plot to identify the observations? I can probably extract this from the dr.defs object whose str() is below, but I don?t know how to do that, or somehow from the original data frame row names. Help would be greatly appreciated. I know this is trivial in a regular plot(x,y) situation. > > str(dr.defs) > > List of 22 > $ call : language MclustDR(object = clusters.defs) > $ type : chr "Mclust" > $ x : num [1:49, 1:7] 2 3.5 4 4 2 1 1 2 2 2 ... > ..- attr(*, "dimnames")=List of 2 > .. ..$ : chr [1:49] "AUS_76" "AUS_81" "AUS_91" "AUS_01" ... > .. ..$ : chr [1:7] "RRcoarse" "govtDeficit" "res_gdp" "GrossFixedCapForm_UN" ... > $ Sigma : num [1:7, 1:7] 0.875 0.116 -0.781 -0.525 2.339 ... > ..- attr(*, "dimnames")=List of 2 > .. ..$ : chr [1:7] "RRcoarse" "govtDeficit" "res_gdp" "GrossFixedCapForm_UN" ? > etc etc etc >Hi Mark, I didn't see an answer to your question, but you might find the boxed.labels function (plotrix) helpful. I think your labels can be obtained using: mylabels<-rownames(dr.defs$x) and by calling: boxed.labels(x,y,mylabels) where x and y are the coordinates of the points you want to label, you will get the labels placed at the points. I suggest boxed.labels because the labels will have a little box around them and a background that prevents the underlying image from obscuring them. Jim