0

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.

  • Usually discrete variables could be interpreted as categorical ones, i.e., `factor`. You can encode the order of the levels - there are plotted in ggplot accordingly. – Drey Dec 09 '16 at 08:48
  • [Related](http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph?rq=1). – Axeman Dec 09 '16 at 08:52
  • well i found the answer. I used the argument limits in stat_x_discrete() element and setting this argument to be same as the argument labels in the stat_x_discrete() element. – Vamsi Krishna Dec 09 '16 at 12:51

0 Answers0