r - 如何在ggpubr中调整图例的大小
问题描述
我有以下代码:
T<-as.data.frame(matrix(0,11,2))
T$V2<-T$V2+1
colors <- c("V1" = "blue", "V2" = "red")
A<-ggplot(T,aes(x=seq(-5,5)))+
#geom_smooth(aes(ymin=Treat-Treatse,ymax=Treat+Treatse),colour='blue')+
geom_line(aes(y=V1,color="V1"))+
geom_point(size=2,aes(y=V1,color="V1"))+
geom_line(aes(y=V2,color="V2"))+
geom_point(size=2,aes(y=V2,color="V2"))+
labs(x='Yadayada',y='Dumdedum',color="Legend")+
scale_color_manual(values = colors)+geom_vline(xintercept=0,linetype='dashed',col='blue')+
scale_x_continuous(breaks=seq(-5,5),labels=c('-5','-4','-3','-2','-1','0','1','2',
'3','4','5'))+theme_classic()+
ggtitle('Example')
B<-ggplot(T,aes(x=seq(-5,5)))+
#geom_smooth(aes(ymin=Treat-Treatse,ymax=Treat+Treatse),colour='blue')+
geom_line(aes(y=V1,color="V1"))+
geom_point(size=2,aes(y=V1,color="V1"))+
geom_line(aes(y=V2,color="V2"))+
geom_point(size=2,aes(y=V2,color="V2"))+
labs(x='Yadayada',y='Dumdedum',color="Legend")+
scale_color_manual(values = colors)+geom_vline(xintercept=0,linetype='dashed',col='blue')+
scale_x_continuous(breaks=seq(-5,5),labels=c('-5','-4','-3','-2','-1','0','1','2',
'3','4','5'))+theme_classic()+
ggtitle('Example')
get_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
legend<-get_legend(B)
ggarrange(arrangeGrob(A+theme(legend.position="none"),
B+theme(legend.position="none")
,nrow=1),legend,
nrow=2,heights=c(10,2))
它生成以下图:
我想知道是否有办法在上面生成的公共图中(相对于公共图中的两个数字)调整图例的大小?“高度”功能似乎并没有调整图例本身的大小,只是调整它占用的空间量。
解决方案
我通过后门解决了这个问题。我只是通过编写将图例分成不同的输出
ggarrange(legend)
这使我可以将图例输出到单独的图像中,该图像可以在 rstudio 之外调整大小。
推荐阅读
- xcode8 - IB Designables 无法渲染和更新自动布局状态:dlopen(Cosmos.framework,1):找不到合适的图像
- extjs - ExtJS:类中的范围问题
- git - 将未签名的提交与 master 合并
- ibm-midrange - AS/400 上的 GeneXus 网格负载问题
- javascript - 正则表达式仅匹配 URL 中的字符串“img”(例如,不是标签名称)
- git - git fetch 非快进更新
- python - 哈希值的多次迭代对其自身
- amazon-web-services - AWS API 网关。从 github 更新现有 API。添加新路线
- android - 以编程方式设置 ScrollView 内 LinearLayout 的高度不起作用
- c# - 是否可以从 C# 中的下拉数据源中找到特定值?