1

With a data frame df and a column col_name I use the below function to generate histograms like in the figure below.

myHistogramDensity <- function(df, col_name) {
  p1 <- ggplot(df, aes_string(x=col_name)) + 
    geom_histogram(aes(y=..density..), binwidth=1, colour="black", fill="white")
  p1 <- p1 + scale_y_continuous(labels=percent) 
  p1 <- p1 + annotation_custom(tableGrob(myMinMaxMed(df, col_name), rows = NULL),
                               xmin=10, xmax=13, ymin=0.5, ymax=0.6)
  return (p1)
}

This gives me a histogram like below.

How do I get the x-axis to display labels for all discrete values present in col_name ?

histogram

user3206440
  • 4,749
  • 15
  • 75
  • 132
  • You should provide the data.frame you are using. Please, check this link http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – shiny Jan 29 '17 at 05:21

1 Answers1

6

Add the following to your function (this only works if col_name is made up of integer values...since you said "discrete" in your request, I assume this should work)

x_axis_labels <- min(df[,col_name]):max(df[,col_name])

p1 <- p1 + scale_x_continuous(labels = x_axis_labels, breaks = x_axis_labels)