Help with simple bar plot

Hey all,

I'm having trouble creating a seemingly simple bar plot. I'm aiming for a plot where each individual row of data in this table is a bar with y=the spot_counts_mean, but data within the same Group_name, as my aesthetics dictate, are getting grouped together. I'd like to separate them all out. The dodge position isn't working either. Bar plot shown in my comment. Any suggestions?!

circle_plot <- ggplot(Spot_counts_stats_circle, aes(x=Group_name, y = Spot_counts_mean)) +
geom_bar(stat = "identity", fill=alpha("blue", 0.3))

ggplot will only do what you tell it to do. In your code you've just told it to use Group name as the element of the x axis. If you want it to separate out by another variable, eg Pool, then you need to mention that variable, as it won't guess what's in your mind.

The syntax and options of ggplot2 can be pretty complex imho, however, and it's worth googling or searching Stack Exchange for tips on how others have made it work. Or read Kieran Healy's Data Visualisation book.

Here's a couple of ways to do what I think you're trying to do:


ggplot2::midwest %>% 
  filter(category == "ALR") %>% 
  ggplot(aes(x = state, y = popdensity, fill = county)) +
  geom_col(position = position_dodge2(preserve = "single"))

ggplot2::midwest %>% 
  filter(category == "ALR") %>% 
  ggplot(aes(x = county, y = popdensity)) +
  geom_col(position = "dodge", fill = "slategrey") +
  facet_grid(~state, scales = "free_x", space = "free_x")

Created on 2020-09-12 by the reprex package (v0.3.0)

This StackOverflow thread helped me with this answer.

Also, please provide actual sample data in your questions, rather than screenshots. You can use dput or datapasta to help you paste a sample of your data into your question.

Thank you for the suggestions Francis, and I tried making a reprex for the post but was having issues with the wrong code chunks being used.

I realized a simple solution to avoid faceting would be to create a column 1:nrow, and use this column for my x-axis aesthetic. With each number different, ggplot can't group them and every row of data gets its own bar. Then, i could use geo_text to add my group name labels

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.