在ggplot2中,如何为双面板图分别设置y-labs?

时间:2014-09-07 15:21:01

标签: r ggplot2

如何分别为这两个面板添加两个y-labs:即“first panel”,“second panel”?**

这是我的数据:

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

然后,我用两个面板生成了一个图,这是我的代码:

d1$panel <- "a"
d2$panel <- "b" 
d <- rbind (d1, d2)

#Upper panel
p<-ggplot(data=d,aes(x=order))+facet_grid(panel~.,scale="free")+
    geom_rect(data=d1, aes(xmin=order-0.1, xmax=order+0.1, ymin = min, ymax=max), size=1,alpha=0, color="black")+
    geom_point(data=d1, aes(y=min, shape="min"), size=5, fill="white")+ 
    geom_point(data=d1, aes(y=mean, shape="mean"), size=5)+
    geom_point(data=d1, aes(y=max, shape="max"), size=5)+
    #labels on x-axis
    scale_x_continuous(breaks=c(1:13), labels=c("Insincere","Joking","Suggesting","Irony",
                   "Neutral","Serious","Commanding","Praising",
                   "Sincere","Friendly","Polite","Hostile", "Rude"))+
    #delete the ylab (or there will be "min")
    xlab("")+ylab("")+theme_bw()+
    theme(axis.text.x=element_text(size=25,angle=45, vjust=0.5, color="black"))+
    theme(legend.text = element_text(size = 20))+
    theme(legend.title = element_text(size = 20))+
    theme(axis.text.y = element_text(size=20))+
    scale_shape_manual(values=c("min"=15,"mean"=18,"max"=16))+
# Lower panel 
geom_bar(data=d2,aes(y=SpRate, fill="SpRate"),stat="identity",width=0.9)+
    theme(strip.text = element_text(size=20, face="bold"))+
    scale_fill_manual(values=c("SpRate"="grey"))+
    labs(fill = "SpRate:")+
    labs(shape = "f0:")+
    guides(shape = guide_legend(order = 1), 
              fill = guide_legend(order = 2))+
#hide the strip
    theme(strip.text.y = element_blank() , 
          strip.background = element_blank())
#There is no ylabs on both panel.
p

那么,如何分别为这两个面板添加两个y-labs:即“第一个面板”,“第二个面板”?

2 个答案:

答案 0 :(得分:1)

试试这个,

g = ggplotGrob(p)
library(gtable)
g = gtable_add_grob(g, list(textGrob("top panel", rot=90), 
                             textGrob("bottom panel", rot=90)), 
                     t = c(3,5), l = 1)
g$widths[[1]] = unit(1, "line")
grid.newpage()
grid.draw(g)

答案 1 :(得分:0)

AFAIK无法为构面(?)设置不同的轴标签。所以,而不是这个

ggplot()+
  facet_grid(panel~., scale = "free") + 
  geom_point(data=d1, aes(x, y))+ 
  geom_bar(data=d2, aes(x, z), stat= "identity" )

您可能想尝试这种方法

library(gridExtra)
grid.arrange(ggplot() + geom_point(data=d1, aes(x, y)) + ylab("1"), 
             ggplot() + geom_bar(data=d2, aes(x, z), stat="identity") + ylab("2"), 
             ncol = 1)