1

How do you create a back-to-back histogram from these two bar plots? I want to merge the bars by row in one plot, i.e. p1 on the right side and p2 on the left side of the zero axis. Here is an example:

library(ggplot2)
x1 <- seq(0,100,10)
x2 <- seq(-100,0,10)
rows <- c(1:11)
data <- data.frame(rows,x1,x2)
z <- c(-100,100)
p1 <- qplot(rows, data=data, weight=x1, geom="bar", binwidth=1, color="black",
            xlab="", ylab="", position = "identity", ylim=z)
p2 <- qplot(rows, data=data, weight=x2, geom="bar", binwidth=1, color="black",
            xlab="", ylab="", position = "identity", ylim=z)
p1 + coord_flip() + theme(legend.position="none")
p2 + coord_flip() + theme(legend.position="none")

3 Answers 3

2

The geom='bar' is really meant for "binning" your data. If you don't already have the values for each of your rows, ggplot can calculate the value for you. But in your case, you already know the length of the bar, so you are much better off using geom_rect, which can plot rectangles. ggplot is intended to work in long form, so use melt in the reshape package to convert your data:

library(reshape)
# Put the data into long form - preferred for ggplot
melted.data <- melt(data, id.vars='rows')
# Use geom_rect to plot data that was already been "binned".
ggplot(melted.data, aes(xmax=rows+0.5, xmin=rows-0.5, ymax=value)) + 
  geom_rect(ymin=0, color="black") + coord_flip()

enter image description here

You also avoid having to use z because ggplot will take care of the minimum and maximum y values.

You can make the bar length along the x-axis scale, and then you won't have to use coord_flip, but I find it less confusing to always code the bar length as y. But this would get you the same thing:

ggplot(melted.data, aes(ymax=rows+0.5, ymin=rows-0.5, xmax=value)) + 
  geom_rect(xmin=0, color="black")

You can force geom_bar to do what you want by fixing the binning statistic to the identity function (fancy way of saying don't bin), and the position to identity as well, so it doesn't try to stack the bars.

ggplot(melted.data, aes(x=rows, y=value)) + 
  geom_bar(position='identity', stat='identity', color="black") + coord_flip()

Which will get you the same thing as well.

0
1

You want to have all your data in one data.frame and create a factor type group level ($group in the code below). Then, color by group to force the weights to evaluate separately for each group. I'm not overly familiar with qplot, but I assume you could, if you really wanted to, add in some code later on to force both groups to have the same color.

library(ggplot2)
x1 <- seq(0,100,10)
x2 <- seq(-100,0,10)
rows <- c(1:11)
data <- rbind(data.frame(rows,x=x1,group=1),data.frame(rows,x=x2,group=2))
data$group = as.factor(data$group)
z <- c(-100,100)
p1 <- qplot(rows, data=data, weight=x, geom="bar", binwidth=1, color=group,
            xlab="", ylab="", position = "identity", ylim=z)
p1 + coord_flip() + theme(legend.position="none")
1

Sorry I don't know ggplot2 very well and this might not be what you are after, but you could do this using

    barplot() 

in base as follows.

    barplot(x1, 1, horiz = T, xlim = range(c(x1, x2)))
    barplot(x2, 1, horiz = T, add = T)
1
  • 1
    seems like Mark has the answer that you need.
    – roman
    Feb 27, 2015 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.