RoyBatty
RoyBatty

Reputation: 326

Ggplot not displaying labels with scale_discrete

I have a binary matrix between 2 variables. I would like to know if there is a way to cluster the binary matrix in R. If so, which algorithm should I be using?

The matrix looks like this

        hobby1  hobby2  hobby3  hobby4
person1   1       0       0       1
person2   0       1       0       1
person3   1       1       1       0
person4   0       1       1       1

So for clustering the patients by the most common hobbies they have, I found the following code in a stackoverflow question.

m.h<-(matrix(sample(0:1,200,T),nrow=20))

# CREATE CROSS_PRODUCT
m.cross<-matrix(unlist(lapply(1:nrow(m.h),function(x)crossprod(m.h[x,],t(m.h)))),nrow=nrow(m.h),byrow=T)

# USE reshape2 to melt/flatten the data
require(reshape2)
m.long<-melt(m.cross)
m.long[order(m.long$value,factor(m.long$Var2),factor(m.long$Var1)),]

require(ggplot2)
ggplot(m.long)+
  geom_tile(aes(Var1,Var2,fill=value))+
  geom_text(aes(Var1,Var2,label=value))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  scale_fill_gradient(low="yellow",high="red") +
  scale_x_discrete(breaks = 1:nrow(m.h), labels=unlist(lapply(1:nrow(m.h),function(x)paste0("Person ",x)))) + 
  scale_y_discrete(breaks = 1:nrow(m.h), labels=unlist(lapply(1:nrow(m.h),function(x)paste0("Person ",x)))) +
  coord_cartesian(xlim=c(0,nrow(m.h)+1),ylim=c(0,nrow(m.h)+1)) 

However, I would like the plot to display the person in the X and Y axis. For that you have the scale_y_discrete and scale_x_discrete function, but it's not working and I don't know why, as my result is displayed as:

enter image description here

Upvotes: 0

Views: 617

Answers (1)

Quinten
Quinten

Reputation: 41235

As said in the comments, your Var are numeric which means that they are continuous. So you should use scale_x_continuous and scale_y_continuous for your data. You can use this code:

m.h<-(matrix(sample(0:1,200,T),nrow=20))
  
  # CREATE CROSS_PRODUCT
  m.cross<-matrix(unlist(lapply(1:nrow(m.h),function(x)crossprod(m.h[x,],t(m.h)))),nrow=nrow(m.h),byrow=T)
  
  # USE reshape2 to melt/flatten the data
  require(reshape2)
  m.long<-melt(m.cross)
  m.long[order(m.long$value,factor(m.long$Var2),factor(m.long$Var1)),]
  
  require(ggplot2)
  ggplot(m.long)+
    geom_tile(aes(Var1,Var2,fill=value))+
    geom_text(aes(Var1,Var2,label=value))+
    theme(axis.text.x = element_text(angle = 90, hjust = 1))+
    scale_fill_gradient(low="yellow",high="red") +
    scale_x_continuous(breaks = 1:nrow(m.h), labels=unlist(lapply(1:nrow(m.h),function(x)paste0("Person ",x)))) + 
    scale_y_continuous(breaks = 1:nrow(m.h), labels=unlist(lapply(1:nrow(m.h),function(x)paste0("Person ",x)))) +
    coord_cartesian(xlim=c(0,nrow(m.h)+1),ylim=c(0,nrow(m.h)+1)) 

Output:

enter image description here

Upvotes: 1

Related Questions