r - Align text right justified but center of each box in geom_text -


i have created graph ggplot using geom_tile , geom_text. want able read numbers down 1 column , have them lineup. set font courier , used hjust justify numbers (otherwise negative sign throws off alignment). cant find way keep right justification , center text in middle each box. ideas on how numbers line can scan column quickly?

reproducible example below.

require(reshape2) require(ggplot2) require(scales)  set.seed(504) df_graph <- data.frame("category"=c("green","blue","red","yellow","black","random1","random2","random3","random4","random5","random6","random7",                                     "random8","random9","random10"),"test1"=rnorm(15),"test1"=rnorm(15),"test2"=rnorm(15),"test3"=rnorm(15),                        "test4"=rnorm(15),"test5"=rnorm(15))  df_graph <- melt(df_graph)                        df_graph$text <- round(df_graph$value, 2)  colors_used <- c("#b2182b","#d6604d","#f4a582","#fddbc7","#f7f7f7","#d1e5f0","#92c5de","#4393c3","#2166ac") values <- seq(-4, 4, length = 9)  g <- ggplot(df_graph, aes(variable, category)) g <- g + geom_tile(aes(fill = value), colour = "white", size=0.75) g <- g + geom_text(aes(label=text),size=3, family="courier", hjust = 1) g <- g + scale_fill_gradientn(colours = colors_used, na.value = "#ffffff", values = values,rescaler = function(x, ...) x, oob = identity) g <- g + theme_bw() g <- g + theme(   axis.text.x= element_text(color="black", size=10, angle = 45, hjust = 0.5),   axis.text.y= element_text(color="black", size=8),   axis.title.y = element_text(color="black",size=10),   plot.title = element_text(color="black",size=14,face="bold"),   legend.key = element_rect(fill="white"), legend.background = element_rect(fill=na),   legend.position="none",   legend.title = element_blank(),   panel.grid  = element_blank(),   panel.border = element_blank() ) 

use hjust=0.5 in geom_text() , pad labels positive numbers appropriately, i.e. prepend space characters obtain labels of equal length:

df_graph$text <- format(round(df_graph$value, 2)) 

plot


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -