Reputation: 145
problem: I want to keep the column with the width I define (0.20); however, I would like to put the columns closer. I am trying to use position_dodge to do it but it doesn't seem to work (I change the width of the position_dodge but I see no change in the output).
data:
SITE N CHL sd se ci
SITE1 20 0.01037453 0.004217003 0.0009429505 0.001973618
SITE2 20 0.01704401 0.006188691 0.0013838333 0.002896396
SITE3 19 0.01191036 0.004188927 0.0009610057 0.002018998
script:
dodge <- position_dodge(width = 0.25)#this is the width I try to change to get the columns closer, but it doesn't do it
ggplot() + geom_bar(data=s_site2chl, aes(x=SITE, y=CHL, fill=SITE),
stat="identity", colour="black", width = .20, position = dodge)
Upvotes: 1
Views: 658
Reputation: 15907
To use position_dodge()
does not help, because there is nothing in your plot. The position only matters, if you have several bars with the same x
-value. Then, you need to choose, whether you want to stack them on top of each other ("stack"), next to each other ("dodge"), or let them overplot each other ("identity"). But you have only one bar per x
-value and thus position is irrelevant.
You can bring the bars closer together by making them wider, as others have suggested. If you set width = 1
, the bars will touch, if you choose a smaller value, there will still be a gap:
ggplot() + geom_bar(data=s_site2chl, aes(x=SITE, y=CHL, fill=SITE),
stat="identity", colour="black", width = .8)
You say in one of your comments, that you only want to bring the bars closer together, but not make them wider. I can see at least two ways to achieve this. One would be to just save the plot with a different resolution. The figure above is 590 pixels wide and 380 pixels high. If I store it with half the width, I get this:
The second possibility is that you expand the x-axis more on both sides. You can use scale_x_discrete()
as follows:
ggplot() + geom_bar(data=s_site2chl, aes(x=SITE, y=CHL, fill=SITE),
stat="identity", colour="black", width = .8) +
scale_x_discrete(expand = c(0, 2))
expand
takes a vector of two with the following meaning (from ?discrete_scale
):
a numeric vector of length two, giving a multiplicative and additive constant used to expand the range of the scales so that there is a small gap between the data and the axes. The defaults are (0,0.6) for discrete scales and (0.05,0) for continuous scales.
I set the additive value to 2
(instead of the default 0.6
), which adds more space on both sides of the bars and as a result makes them thinner.
Upvotes: 1