removing white space between bars on a bar graph

8,482 views
Skip to first unread message

Benjamin Ditkowsky, Ph.D.

unread,
Apr 5, 2016, 11:34:06 PM4/5/16
to ggp...@googlegroups.com
I want to be able to remove the white space between bars in a bar plot (geom_bar) in which the width of the bars are determined by the Number of participants. I have tried position = 'dodge' but it did not work the way I want it to work.

This is what my current graph looks like:


Here is my reproducible example

library(ggplot2)
MyFile<- structure(list(ethnic = structure(c(5L, 3L, 1L, 4L, 2L),
                    .Label = c("Asian", "Black", "Hispanic", "Other", "White"), class = "factor"),
                    district = c(85L, 8L, 3L, 3L, 1L),
                    ap.course = c(86L, 4L, 6L, 3L, 1L)),
                   .Names = c("ethnic",  "district", "ap.course"), class = "data.frame",
                   row.names = c(NA,-5L))

attach(MyFile)
MyFile$prop<-ap.course/district
MyFile$w<- (.05+log(MyFile$ap.course))*.3
# add .5 because otherwise black does not show up
#multiply by 30% because otherwise the default spacing is odd

ggplot(MyFile, aes(x=ethnic,y=prop,, width=w), binwidth=0)+
  geom_bar(aes(fill=ethnic), stat="identity", position="dodge", width = 10 )+
  labs(x="Ethnicity ", y="Risk Ratio ")+
  theme_classic()


This is the way I want the graph to look


Any ideas would be appreciated.
-- 
Sincerely,

Ben Ditkowsky, Ph. D.
MeasuredEffects.com

Wouter van der Bijl

unread,
Apr 6, 2016, 4:12:53 AM4/6/16
to ggplot2
Here is one solution using facet_grid():

ggplot(MyFile, aes(x=1,y=prop,, width=w), binwidth=0)+
  geom_bar
(aes(fill=ethnic), stat="identity")+
  facet_grid
(~ethnic, scales = 'free_x', space = 'free')+
  scale_x_continuous
(expand = c(0, 0)) +
  theme_classic
()+
  theme
(axis.text.x=element_blank(),
        axis
.ticks.x=element_blank(),
        panel
.margin=unit(0, "lines"),
        strip
.background = element_blank(),
        strip
.text.x = element_blank())+

  labs
(x="Ethnicity ", y="Risk Ratio ")

Benjamin Ditkowsky, Ph.D.

unread,
Apr 6, 2016, 7:58:33 AM4/6/16
to ggp...@googlegroups.com
Perfect! Thanks. That is exactly what I was looking for.
Sincerely,

Ben Ditkowsky, Ph. D.
MeasuredEffects.com
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vivek Patil

unread,
Apr 6, 2016, 8:37:00 AM4/6/16
to Benjamin Ditkowsky, Ph.D., ggplot2
Using the approach suggested by https://learnr.wordpress.com/2009/03/29/ggplot2-variable-width-column-chart/, you could also achieve the goal using the following.

library(ggplot2)
MyFile<- structure(list(ethnic = structure(c(5L, 3L, 1L, 4L, 2L),
                                           .Label = c("Asian", "Black", "Hispanic", "Other", "White"), class = "factor"),
                        district = c(85L, 8L, 3L, 3L, 1L),
                        ap.course = c(86L, 4L, 6L, 3L, 1L)),
                   .Names = c("ethnic",  "district", "ap.course"), class = "data.frame",
                   row.names = c(NA,-5L))

attach(MyFile)
MyFile$prop<-ap.course/district

MyFile=MyFile[order(-MyFile$prop),]

MyFile$cumsum <- cumsum(MyFile$ap.course)
MyFile$wm <- MyFile$cumsum - MyFile$ap.course
MyFile$wt <- with(MyFile, wm + (cumsum - wm)/2)

p <- ggplot(MyFile, aes(ymin = 0))
p1 <- p + geom_rect(aes(xmin = wm, xmax = cumsum,
                          ymax = prop, fill = ethnic),color="white")
p1

Benjamin Ditkowsky, Ph.D.

unread,
Apr 6, 2016, 8:21:25 PM4/6/16
to Vivek Patil, ggplot2
Excellent As well. This is a very clever way to solve my question.

Thanks

Sincerely,

Ben Ditkowsky, Ph. D.
MeasuredEffects.com
Reply all
Reply to author
Forward
0 new messages