本文介绍了在ggplot2中,如何为双面板图分别设置y-labs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何为这两个面板分别添加两个y-labs作为:即第一个面板,第二个面板?** 下面是我的数据: 态度订单分钟时间b%-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)意指LT; -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 态度< - c (敌对的,粗鲁,Pr aic,Commanding,Insince,Polite,Joking,Suggesting,Irony,Serious,Friendly,Sincere,Neutral) order min 最大值 mean< -c(0,0,0,0,0,0,0,0,0,0,0,0,0) SpRate D2< -data.frame(姿态,秩序,最小值,最大值,平均值,SPRATE) d1 d2 然后,我用两个面板,这是我的代码: d1 $面板 d2 $面板< - bd< - rbind(d1,d2) #上面板 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)+ #x轴上的标签 scale_x_continuous(breaks = c(1:13),labels = c(Insincere,Joking,Suggesting,Irony ,中立,严肃,指挥,赞美,真诚,友善,礼貌,敌对,粗鲁 b #delete ylab(或者会有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))+ #下面板 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(value s = c(SpRate=gray))+ labs(fill =SpRate:)+ labs(shape =f0:)+ guides(shape = guide_legend(order = 1), fill = guide_legend(order = 2))+ #hide strip theme(strip.text.y = element_blank(), strip .background = element_blank())#在这两个面板上都没有ylabs。 p 所以,如何为这两个面板分别添加两个y-labs as:iefirst panel,second panel? 解决方案试试这个, 'pre> G = ggplotGrob(p)库(gtable)G = gtable_add_grob(克,列表(textGrob( 顶部面板 ,rot = 90), textGrob(bottom panel,rot = 90)),t = c(3,5),l = 1)g $ widths [[1]] = unit(1,line) grid.newpage() grid.draw(g) how to add two y-labs for these two panels separately as: i.e."first panel", "second panel"?**Here is my data: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)d1d2Then, I generated a plot with two panels, here is my code:d1$panel <- "a"d2$panel <- "b" d <- rbind (d1, d2)#Upper panelp<-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.pSo, how to add two y-labs for these two panels separately as: i.e."first panel", "second panel"? 解决方案 try this,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) 这篇关于在ggplot2中,如何为双面板图分别设置y-labs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 06:12