我试图用R绘制双y轴图。它应该有一组数据的条形图和另一组的框图。这就是我到目前为止所拥有的:
set.seed(1)
boxData <- data.frame(group=rep(1:4, 10),measurement=matrix(rnorm(40), nrow=40))
barData <- data.frame(group=1:4, measurement=matrix(runif(4), nrow=4)*5)
p1 <- ggplot(boxData, aes(factor(group), measurement))
p1 <- p1 + geom_boxplot(width=0.2, position=position_dodge(width=0.2))
p1
p2 <-p1 + geom_bar(data=barData, aes(factor(group), measurement),stat="identity",
width=0.2, position=position_fill(width=0.2), colour="black")
p2
我需要解决两件事:
答案 0 :(得分:2)
除非你非常好网格,否则双y轴将不会发生。你可以&#34;假&#34;通过做这样的事情来躲避:
p1 <- ggplot(boxData, aes(x = group - 0.1, measurement,group = group))
p1 <- p1 + geom_boxplot(width=0.2, position=position_dodge(width=0.2))
p1
p2 <-p1 + geom_bar(data=barData, aes(x = group + 0.1, measurement,group = group),
stat="identity",width=0.2, colour="black")
p2