R中定制的背对背直方图

时间:2015-06-18 10:49:57

标签: r plot 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

是否可以更改条形的形状,使它们只是简单的线条,如:

enter image description here

1 个答案:

答案 0 :(得分:4)

你的代码对我来说是模糊的,你的最终结果应该是什么。也许这个:

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