oğuz bozkurt
oğuz bozkurt

Reputation: 41

How can i set column names on near the boxes in ggplot2?

I am trying to create a heat map like this MAP

My data set is here:DATASET

ggplot(data=mxdf)+
  geom_tile(aes(x=mxdf$Var1,y=mxdf$Var2,fill=mxdf$value))+
  scale_fill_viridis(option = "B",discrete = T,end=1)+
  labs(title = "CORRELATIONS",y="",x="")+
  theme(legend.position = "none",axis.text.x = element_text(angle = 45, vjust = 1, 
                                                            size = 9, hjust = 1))

I use this code and I get this map enter image description here

that's okay but X axis column names is too far to graph. How can I set x axis column names like first graph ?

Upvotes: 1

Views: 342

Answers (1)

Rhino8
Rhino8

Reputation: 156

I think you need to add annotate. However to fix the annotation position you also need to use expansion in the x-axis.

 ggplot(data= mxdf)+
  geom_tile(aes(x= mxdf$Var2,y= mxdf$Var1,fill= mxdf$value))+
  scale_fill_viridis(option = "B",discrete = T,end=1)+
  labs(title = "CORRELATIONS",y="",x="")+
  theme(legend.position = "none",axis.text.x = element_text(angle = 45, vjust = 1, 
                                                            size = 9, hjust = 1)) +
  scale_x_discrete(expand = c(0.5, 0.5)) +
  annotate("text", x = (1:length(levels(mxdf$Var2)))-1, y = 1:length(levels(mxdf$Var2)), label = levels(mxdf$Var2)) +
  theme(
    axis.text.y = element_blank()
  )

Upvotes: 1

Related Questions