0

I've got a table with different Plot Types and the Cluster number they are sorted by. The table looks like this:

'data.frame':   80 obs. of  16 variables:
 $ GS_CT_HC_NO: Factor w/ 16 levels "GS_CT_HC_NO",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ CT_HC_NO   : Factor w/ 8 levels "CT_HC_NO","CT_HC_SW",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ GS_CT_HC   : Factor w/ 8 levels "GS_CT_HC","GS_CT_LC",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ GS_HC_NO   : Factor w/ 8 levels "GS_HC_NO","GS_HC_SW",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ GS_CT_NO   : Factor w/ 8 levels "GS_CT_NO","GS_CT_SW",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ CT_HC      : Factor w/ 4 levels "CT_HC","CT_LC",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ CT_NO      : Factor w/ 4 levels "CT_NO","CT_SW",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ GS_CT      : Factor w/ 4 levels "GS_CT","GS_FT",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ GS_HC      : Factor w/ 4 levels "GS_HC","GS_LC",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ GS_NO      : Factor w/ 4 levels "GS_NO","GS_SW",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ HC_NO      : Factor w/ 4 levels "HC_NO","HC_SW",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ NO         : Factor w/ 2 levels "NO","SW": 1 1 1 1 1 2 2 2 2 2 ...
 $ HC         : Factor w/ 2 levels "HC","LC": 1 1 1 1 1 1 1 1 1 1 ...
 $ GS         : Factor w/ 2 levels "GS","SS": 1 1 1 1 1 1 1 1 1 1 ...
 $ CT         : Factor w/ 2 levels "CT","FT": 1 1 1 1 1 1 1 1 1 1 ...
 $ ClusterNr  : int  4 5 2 4 1 4 3 3 4 4 ...

Now, I created a Mosaicplot with the vcd package in R of GS_CT_HC_NO (first column) and the Cluster Nr with the following code:

mosaic(ClusterNr ~ GS_CT_HC_NO, data = groups, shade = TRUE, highlighting_fill = 
terrain.colors(5), labeling_args = list(tl_labels = FALSE, clip = FALSE, tl_varnames = TRUE,
pos_labels = "center", set_varnames = c(GS_CT_HC_NO = "Plot Class", ClusterNr = "Cluster"),
rot_labels = c(0, 0, 0, 90), gp_labels = (gpar(fontsize = 8)), just_labels = "left"))

I want to create such a plot for all other Plot Type combinations by using an for loop but when I tried with

for(j in 1:dim(groups[,-16])[2]) {

mosaic(ClusterNr ~ j, data = groups, shade = TRUE, highlighting_fill = 
terrain.colors(5), labeling_args = list(tl_labels = FALSE, clip = FALSE, tl_varnames =TRUE,
 pos_labels = "center", set_varnames = c(j = "Plot Class", ClusterNr = "Cluster"),
 rot_labels = c(0, 0, 0, 90), gp_labels = (gpar(fontsize = 8)), just_labels = "left"))

}

I received the Error:

Error in model.frame.default(formula = ~j + ClusterNr, data = groups) : 
 Variablenlängen sind unterschiedlich (gefunden für 'ClusterNr')

Last part of the error code means, that the length of variables is different (especially for the variable 'ClusterNr')

Is anybody please able to help me finding a correct working loop with the possibility to save every plot as a jpeg?

Cheers, Sven!

Sven H.
  • 1
  • 1
  • 2
    Please show the actual code you used that gave the error, and provide a [mcve]. See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. –  Apr 12 '18 at 13:44
  • Please show your implementation of *j* in `for` loop. And to be clear: do you want to subset your dataframe by *ClusterNr* and then run mosaic plots on each subset? *plot for all other Plot Type combinations* is not too clear. – Parfait Apr 12 '18 at 14:23
  • The aim was to show the distribution of Plottypes in the Cluster groups. Therefore, i just want to plot the whole column of ClusterNr with each other column, which containing the Plottype combinations. – Sven H. Apr 13 '18 at 09:14

0 Answers0