2

I am trying to change the bar width of my plots, my codes are here:

ggplot(df_densityWindow, aes(x = idx, y = density, color = factor(location))) + 
geom_bar(stat = 'identity', fill = 'white', width = 1) +
  theme_bw() +
  facet_grid(marker ~ case, scales = 'free') +
  theme(strip.background = element_rect(colour="black", fill="white"),
  strip.text = element_text(margin = margin(10, 10, 10, 10), size = 40)) +
  scale_color_manual(name = 'Regions',values = c("#F26969", "#02f527",'#F2F2C6')) +
  background_grid(major = 'y', minor = "none") + # add thin horizontal lines 
  xlab('Index') +
  ylab(expression(paste('Density/', mm^2, )))+
  theme(axis.text = element_text(size = 38)) +
  theme(axis.title = element_text(size = 40)) +
  theme(legend.text = element_text(size = 38)) +
  theme(legend.title = element_text(size = 40)) +
  theme(axis.text.x=element_text(angle=90, hjust=1)) +
  panel_border()

Since I changed the bar width to 1, I assume there should not be any overlapping between each bar, however, the results are: enter image description here For a closer look: enter image description here They are still overlapping with each other, how to solve this?

EDIT:

data link: https://livejohnshopkins-my.sharepoint.com/:u:/g/personal/hmi1_jh_edu/Ee5tkBFh-3VOrYahCRy9SC8BMx8DOdIHZKiMHUY6g1mVFQ?e=9i4TCr

Haoyang Mi
  • 55
  • 5
  • 1
    There's no need to call `theme` so many times. It makes the code less readable. Please also provide data as a `dput` not link. Use `dput(head(data_set,n))`. – NelsonGon Sep 06 '19 at 16:27
  • Thank you for your suggestion! The link is for downloading the data file in rds format. – Haoyang Mi Sep 06 '19 at 17:22
  • Please edit [as shown here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Sep 06 '19 at 17:23
  • I think it would be better not to subset the data, because I think my issue occurs due to the large volume of my dataset, so that subsetting may not helpful to reproduce the problem here. – Haoyang Mi Sep 06 '19 at 17:30

2 Answers2

1

I believe the width argument is scaled to the resolution of the data, so I'd recommend experimenting with the values of both width (set it to a value less than 1) and binwidth.

Hope it works out.

Jwolf
  • 90
  • 7
  • Seems like geom_bar does not have binwidth argument any more, and I changed the width to even 0.05 and it still not working... – Haoyang Mi Sep 06 '19 at 16:14
0

try adding the following position setting to your geom_bar call:

geom_bar(stat = 'identity', fill = 'white', width = 1, position = "dodge")

this should dodge the bars so that they aren't on top of each other. you can also use position_dodge to set the spacing that you'd like manually.

  • Thank you for the help! I tried this method and it's still not working, could you please download my data and have a try? – Haoyang Mi Sep 08 '19 at 14:41