I want to plot a heatmap using ggplot2, and I want to customize the x-axis ticks - location, text, and angle.
Here are my example data:
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
I'd like to have the x-axis tick have only a single tick value, located in the middle of the axis range - 10, with the label: "X-Label", at a 90 degrees angle. In addition, I do not want an x-axis title.
Here's what I'm trying:
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var2,y=Var1,fill=value))+
geom_tile()+theme_minimal()+scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred")+
scale_x_discrete(breaks=10,labels="X-Label")+theme(axis.title.x=element_blank(),axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
So, obviously it's not working.
I thought that I can define the angle as part of the scale_x_discrete arguments but couldn't find that option.
Also, note that I do not want an x-axis title but only the tick text. So I'm not sure why the breaks and labels arguments in scale_x_discrete are ignored.
Here's the desired outcome using plotly:
library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))
Thanks



