PGreen
PGreen

Reputation: 3307

Customized back-to-back histogram in R

Given this example of back to back histogram:

set.seed (123)
xvar <- round (rnorm (100, 54, 10), 0)
xyvar <- round (rnorm (100, 54, 10), 0)
myd <- data.frame (xvar, xyvar)
valut <- as.numeric (cut(c(myd$xvar,myd$xyvar), 12))
myd$xwt <- valut[1:100]
myd$xywt <- valut[101:200]
xy.pop <- data.frame (table (myd$xywt))
xx.pop <- data.frame (table (myd$xwt))


 library(plotrix)
 par(mar=pyramid.plot(xy.pop$Freq,xx.pop$Freq,
    main="Population Pyramid",lxcol="blue",rxcol= "pink",
  gap=0,show.values=F))

enter image description here

Is it possible to change the shape of the bars so that they are simply lines, something like:

enter image description here

Upvotes: 1

Views: 1318

Answers (1)

Roland
Roland

Reputation: 132874

Your code is obfuscating to me what your final result is supposed to be exactly. Maybe this:

library(ggplot2)
DF <- merge(xy.pop, xx.pop, by = "Var1")
ggplot(DF, aes(y = Var1, xmin = -Freq.x, xmax = Freq.y, x = 0)) +
  geom_errorbarh() +
  geom_vline(xintercept = 0, size = 1.5) +
  theme_minimal() +
  xlab("") + ylab("") +
  theme(panel.grid = element_blank()) 

resulting plot

Upvotes: 4

Related Questions