Haoyang Mi
Haoyang Mi

Reputation: 55

Change the bar width does not work in geom_bar function

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

Upvotes: 2

Views: 935

Answers (2)

R. Soriano Smith
R. Soriano Smith

Reputation: 3

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.

Upvotes: 0

Jwolf
Jwolf

Reputation: 90

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.

Upvotes: 1

Related Questions