0

I use k-means analysis and bind the cluster number (km$cluster) to my original data frame. I would like to present histogram per cluster number in one plot (each km$cluster contains at least 2000 observations). Is it possible?How can i do it?Many thanks,Ron

dd <- "UserID MoneyAmount DaysReg km$cluster \n 1 5 6 1.4 1 \n 2 4 9 1.4 1 \n 3 6 7 1.3 1 \n 4 7 30 9.4 2" 
Data <- read.table(text=dd, header = TRUE) 
sessionInfo() 
R version 3.0.2 (2013-09-25) Platform: i386-w64-mingw32/i386 (32-bit) 
[1] ggplot2_0.9.3.1 flexclust_1.3-4 modeltools_0.2-21 lattice_0.20-23 RODBC_1.3-10 
ziggystar
  • 28,410
  • 9
  • 72
  • 124
mql4beginner
  • 2,193
  • 5
  • 34
  • 73

1 Answers1

0

I assume that MoneyAmount is your value of interest. The following code plots a histogram of MoneyAmount for each cluster.

require(ggplot2)
qplot(data=Data,x=MoneyAmount,facets=km.cluster ~ .)

enter image description here

ziggystar
  • 28,410
  • 9
  • 72
  • 124
  • Thanks ziggystar, I tried your solution here: qplot(data=NewRusSep13,x=km$cluster,facets=cluster ~ .) But i got this error massage so i guess I'm doing somthing wrong.. Error in layout_base(data, rows, drop = drop) : At least one layer must contain all variables used for facetting --Thanks,Ron – mql4beginner Jan 23 '14 at 12:08
  • @user1024441 If you provide a reproducible example in your question, I can give you the exact code: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – ziggystar Jan 23 '14 at 12:22
  • UserID km$number DaysFromReg MoneyAmount 391068166810 2 320 70 391068166811 1 12 40 391068166812 1 10 38 – mql4beginner Jan 23 '14 at 12:31
  • @user1024441 You should read the link in my comment. In particular if you are going to ask more R questions. From your last comment I can tell you didn't read it. – ziggystar Jan 23 '14 at 12:34
  • I'm a noob to Stackoverfolw.com so i thought the link is internal issue - I'll look it right now. – mql4beginner Jan 23 '14 at 12:36
  • For this question, some sample data (in the form of runnable R-code, e.g. produced with `dput`) might be enough. For further questions you should also give code you have tried to achieve what you want and say why it didn't work. – ziggystar Jan 23 '14 at 12:39
  • dd <- "UserID MoneyAmount DaysReg km$cluster + 1 5 6 1.4 1 + 2 4 9 1.4 1 + 3 6 7 1.3 1 + 4 7 30 9.4 2" Data <- read.table(text=dd, header = TRUE) sessionInfo() R version 3.0.2 (2013-09-25) Platform: i386-w64-mingw32/i386 (32-bit) [1] ggplot2_0.9.3.1 flexclust_1.3-4 modeltools_0.2-21 lattice_0.20-23 RODBC_1.3-10 – mql4beginner Jan 23 '14 at 13:09
  • @user1024441 Your pasted code didn't work. I fixed it and added it to your question. – ziggystar Jan 23 '14 at 13:23