I have categorical variables being passed in the function, and i want the x-lables in multiple lines thus i use a function which i got on the internet called addline_format in scale_x_discrete refer the function below. the problem arises when i have blank values also as a category i know i can replace it with NA or anything but i want to represent as blank. the ggplot plots the blank values first and this scale_x_discrete gives the blank values in the end.
The function i created.
plthiststr <- function(df,d,e,y,b='') {
#df is dataframe/datatable,
#d is the name of the coulmn for the labels in x-axis with '',
#d SHOULDN'T contain Blank i.e '' type values Values, if it does this plot will not be correct.
#b is the label of y-axis with '',
#e is the title of the plot and y is df$d where d is without ''
#y is df$d where d is without ''
addline_format <- function(x){
gsub('\\s','\n',x)
}
require(ggplot2)
library(ggplot2)
g <- ggplot(df,aes_string(d))
plt <- g+geom_bar()+ylab(b)+ggtitle(e)+xlab('')+scale_x_discrete(labels= addline_format(c(unique(y))))
plt+theme(axis.text.x = element_text(size=10,colour="Blue",hjust=0.5,vjust=1),axis.ticks.y=element_blank(),panel.background=element_rect(fill="White",colour="Black"),panel.grid=element_blank())
}
is there a way i can make the orders match all the time.
and what will happen when i have 'date' type variables. I believe the addline_format will only work for chr type variables.