在ggplot2中,我试图将两张具有相同x轴的图形放入Hadley网站的一个分隔面图中:https://github.com/hadley/ggplot2/wiki/Align-two-plots-on-a-page
这是建议的代码:
library(ggplot2)
x <- seq(1992, 2002, by=2)
d1 <- data.frame(x=x, y=rnorm(length(x)))
xy <- expand.grid(x=x, y=x)
d2 <- data.frame(x=xy$x, y=xy$y, z= jitter(xy$x + xy$y))
d1$panel <- "a"
d2$panel <- "b"
d1$z <- d1$x
d <- rbind(d1, d2)
p <- ggplot(data = d, mapping = aes(x = x, y = y))
p <- p + facet_grid(panel~., scale="free")
p <- p + layer(data= d1, geom = c( "line"), stat = "identity")
p <- p + layer(data=d2, mapping=aes(colour=z, fill=z), geom =
c("tile"), stat = "identity")
p
但是当我想在上面板中添加具有不同y值的更多绘图时,我出了点问题。
这是我的数据:
attitude <- c("Hostile", "Rude", "Praising", "Commanding", "Insincere", "Polite", "Joking", "Suggesting", "Irony", "Serious", "Friendly", "Sincere", "Neutral")
order<-c(12,13,8,7,1,11,2,3,4,6,10,9,5)
min<-c(0.249746688,0.105828885,0.170151929,0.20565908,-0.09135461,0.192402573,0.023005096,0.011312206,-0.044620705,0.072541529,0.089307133,0.165717303,0.110689225)
max<- c(2.2885,2.4161,1.8467,1.7535,1.6409,2.0631,1.6517,1.7195,1.5322,1.8345,2.2395,2.1871,1.5551)
mean<-c(1.100819511,1.128603777,0.873735105,0.843770095,0.659525513,0.972857404,0.681777825,0.693606814,0.696879247,0.82999014,0.955312553,0.94512688,0.730545923)
SpRate<-c(0,0,0,0,0,0,0,0,0,0,0,0,0)
d1 <- data.frame(attitude, order, min, max, mean, SpRate)
attitude <- c("Hostile", "Rude", "Praising", "Commanding", "Insincere", "Polite", "Joking", "Suggesting", "Irony", "Serious", "Friendly", "Sincere", "Neutral")
order<-c(12,13,8,7,1,11,2,3,4,6,10,9,5)
min<-c(0,0,0,0,0,0,0,0,0,0,0,0,0)
max<-c(0,0,0,0,0,0,0,0,0,0,0,0,0)
mean<-c(0,0,0,0,0,0,0,0,0,0,0,0,0)
SpRate<-c(0.1505,0.154,0.1615,0.1615,0.172,0.1725,0.1765,0.177,0.1845,0.1905,0.1905,0.1945,0.1955)
d2<-data.frame(attitude, order, min, max, mean, SpRate)
d1
d2
事实上,我想在网站上绘制一个情节,上面的面板显示三个不同y值(最小值,平均值,最大值)的点条,下面的面板显示条形图,其中y = SpRate,但它似乎不起作用,为什么?
这是我的代码:
library (ggplot2)
d1$panel <- "a"
d2$panel <- "b"
d <- rbind (d1, d2)
p <- ggplot (data=d, mapping = aes(x=order))
p <- p+facet_grid (panel~., scale="free")
#upper panel
p <-p+ layer (data=d1, aes(y=min), geom = c("point"), stat="identity")
p <-p+ layer (data=d1, aes(y=max), geom = c("point"), stat="identity")
p <-p+ layer (data=d1, aes(y=mean), geom = c("point"), stat="identity")
#lower panel
p<-p+ layer (data=d2, aes(y=SpRate), geom = c("bar"), stat="identity")
# not work, why?
p
答案 0 :(得分:1)
您也可以使用geom_point()
和geom_bar()
执行此操作,然后再进行操作
ggplot(data=d,aes(x=order))+facet_grid(panel~.,scale="free")+
geom_point(data=d1,aes(y=min))+
geom_point(data=d1,aes(y=max))+
geom_point(data=d1,aes(y=mean))+
geom_bar(data=d2,aes(y=SpRate),stat="identity")