0

I have a data set that I cut into 7 equal bins using the cut() function. I then converted the data back to a data frame using data.frame(), then plotted this using ggplot, as seen below:

library(ggplot2)
set.seed(1)
data <- runif(n=53,200,1400)
    # converted the data to a value and broke it down from lowest data point to highest data point:
     a-tub.val <- cut(as.numeric(as.character(data)), breaks = seq(0, 1400, by = 200))
    #converted this to a data frame to read into ggplot
     a_tub.dat <- data.frame(a_tub.val)
    # then ran my ggplot code:
     ggplot(data = a_tub.dat) +
        geom_bar(mapping = aes(x=a_tub.val, fill = a_tub.val)) +
        theme(text = element_text(size=20), axis.text.x = element_text(angle=90, hjust=1), legend.title = element_text(size = 12)) +
        ggtitle("Alpha-Tubulin Signals") +
        labs(y="Number in Category", x="Fluoresence bins", fill ="Fluorescence (in thousands)")

packages used were XLConnect and ggplot2

This displays only 4 bars on the ggplot (those that had values), instead of the 7 that I would like to achieve. Would anyone know a way to display those empty bins that contain no data, but should still be viewed on the graph?

Plot displaying 4 bars, however 7 bins were generated

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. I don't think the fact that you are using Excel is relevant to your plotting problem. Just include sample data and ignore where it came from when creating the reproducible example. – MrFlick Apr 07 '20 at 22:50
  • Does this answer your question? [ggplot2 keep unused levels barplot](https://stackoverflow.com/questions/10834382/ggplot2-keep-unused-levels-barplot) – Daniel V Apr 07 '20 at 22:51
  • okay, I think ive created a reproducible example using set.seed, did this help? relatively new to all of this, trying to help everyone read what I've asked. – Jonah_huggins Apr 07 '20 at 23:09
  • I think you want to add `scale_x_discrete(drop=FALSE) + scale_fill_discrete(drop=FALSE)` which will add empty bin for (0,200] and update the legend as well – Ben Apr 08 '20 at 00:57

1 Answers1

0

As noted in the linked answer by @DanielV, you need to add both a scale_x_discrete and scale_fill_discrete call so you can add drop = FALSE arguments.

 ggplot(data = a_tub.dat) +
        geom_bar(mapping = aes(x=a_tub.val, fill = a_tub.val)) + 
        scale_x_discrete(drop=FALSE) + 
        scale_fill_discrete(drop=FALSE) +
        theme(text = element_text(size=20), axis.text.x = element_text(angle=90, hjust=1), legend.title = element_text(size = 12)) +
        ggtitle("Alpha-Tubulin Signals") +
        labs(y="Number in Category", x="Fluoresence bins", fill ="Fluorescence (in thousands)")

enter image description here

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57