Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:41:51+00:00 2026-06-02T19:41:51+00:00

Using ggplot2 I’m creating a histogram with a factor on the horizontal axis and

  • 0

Using ggplot2 I’m creating a histogram with a factor on the horizontal axis and another factor for the fill color, using a dodged position. My problem is that the fill factor sometimes takes only one value for a value of the horizontal factor, and with nothing to dodge the bar takes up the full width. Is there a way to make it dodge nothing so that all bar widths are the same? Or equivalently to plot the 0’s?

For example

ggplot(data = mtcars, aes(x = factor(carb), fill = factor(gear))) +
geom_histogram(position = "dodge")

enter image description here

This answer has a couple ideas. It was also asked before the new version was released, so maybe something changed? Using facets (also shown here) I don’t like for my situation, though I suppose editing the data and using geom_bar could work, but it feels inelegant. Moreover, when I tried facetting anyway

ggplot(mtcars, aes(x = factor(carb), fill = factor(gear))) +
    geom_bar() + facet_grid(~factor(carb))

I get the error “Error in layout_base(data, cols, drop = drop):
At least one layer must contain all variables used for facetting”

I suppose I could generate a data frame of counts and then use geom_bar,

mtcounts <- ddply(subset(mtcars, select = c("carb", "gear")),
    .fun = count, .variables = c("carb", "gear"))

filling out the levels that aren’t present with 0’s. Does anyone know if that would work or if there’s a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:41:53+00:00Added an answer on June 2, 2026 at 7:41 pm

    Updated geom_bar needs stat = "identity"

    I’m not sure if this is too late for you, but see the answer to a recent post here
    That is, I’d take Joran’s advice to pre-calculate the counts outside the ggplot call and to use geom_bar. As with the answer to other post, the counts are obtained in two steps: first, a crosstabulation of counts is obtained using dcast; then second, melt the crosstabulation.

    library(ggplot2)
    library(reshape2)
    
    dat = dcast(mtcars, factor(carb) ~ factor(gear), fun.aggregate = length)
    dat.melt = melt(dat, id.vars = "factor(carb)", measure.vars = c("3", "4", "5"))
    dat.melt
    
    (p <- ggplot(dat.melt, aes(x = `factor(carb)`, y = value, fill = variable)) + 
      geom_bar(stat = "identity", position = "dodge"))
    

    The chart:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ggplot2 to plot a figure that contains nine facets. Each facet
I have the following graph that I generated using ggplot2 I had finalPlot as
I'm currently using scale_brewer() for fill and these look beautiful in color (on screen
I have R code (using ggplot2) that pumps out a bunch of charts to
I am trying to create a barplot using ggplot2, with the y axis starting
I have a following plot with logarithmic y-scale that I plotted using ggplot2 in
I'm using ggplot2 to make some bullseye charts in R. They look delightful, and
I am trying to produce some example graphics using ggplot2, and one of the
I'm making a straightforward barchart in R using the ggplot2 package. Rather than the
I'm plotting a group of curves, using facet in ggplot2. I'd like to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.