Remove space between columns in ggplot2 geom_bar

I look to "dodge" the bar bars together. The following R code leaves a blank space between the columns. Other answers like this show how to do this for part of the bars of a group, but this does not seem to apply to individual columns by a factor along the x axis.

require(ggplot2)
dat <- data.frame(a=c("A", "B", "C"), b=c(0.71, 0.94, 0.85), d=c(32, 99, 18))

ggplot(dat, aes(x= a, y = b, fill=d, width = d/sum(d))) +
  geom_bar(position=position_dodge(width = 0.1), stat="identity")

A game with a variable width changes the appearance, but it is not possible to make the columns sit side by side, while maintaining a significant difference in width (the fill color is also displayed excessively in this column).

+4
source share
2 answers

x , , :

dat$width <-
  dat$d / sum(dat$d)

, , data.frame , , . , , - , , , , :

dat$loc <-
  cumsum(dat$width) - dat$width/2

ggplot, :

ggplot(dat, aes(x= loc, y = b, fill=d, width = width)) +
  geom_bar(stat="identity") +
  scale_x_continuous(breaks = dat$loc
                     , labels = dat$a)

enter image description here

, .

+5

.

ggplot(dat, aes(x=cumsum(d/sum(d))) - d/sum(d)/2, y = b, fill=d, width=d/sum(d))) +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_x_continuous(breaks=cumsum(dat$d/sum(dat$d)) - dat$d/sum(dat$d)/2, labels=dat$a)

,

+4

Source: https://habr.com/ru/post/1668390/


All Articles