在R中的多个情节中的传说 [英] Legend in multiple plot in R

查看:182
本文介绍了在R中的多个情节中的传说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据其他人的评论,这篇文章已被分成几个
),您可以简化工作:


  1. 无需创建自定义布局来重叠多个图

  2. 无需手动创建图例



这个想法是创建2个分离对象,然后使用 doubleYScale 合并它们。后者将创建第二个轴。我希望我能得到你的ploygon想法,因为它不是很清楚你为什么在你的OP中反转它。

  library(latticeExtra)
obj1 < - xyplot(Variable〜TimeVariable | Type,type ='l',
groups = time,scales = list(x = list(relation ='free'),
y = list(relation ='free')),
auto.key = list(columns = 3,lines = TRUE,points = FALSE),

data = subset(dat.l,time!= 1))
obj2 < - xyplot(Variable〜TimeVariable | Type,
data = subset(dat.l,time == 1),type ='l',
scales = list(x = list(alternating = 2),
auto.key = list列= 3,lines = TRUE,points = FALSE),
y = list(relation ='free')),
panel = function(x,y,...){
panel .xyplot(x,y,...)
panel.polygon(x,y,col ='violetred4',border = NA,alpha = 0.3)
})


doubleYScale(obj1,obj2,add.axis = TRUE,style1 = 0,style2 = 1)


According to the comments from others, this post has been separated into several smaller questions from the previous version of this OP.

In the graph below, will you help me to (Newbie to R)

  • Custom legends according to the data they represent like filled for variable 1, circle points for variable 2 and line for variable 3 and their colors.
  • same letter size for the legend and axis-names.

The graph below is produced with the data in pdf device with following layout.

m <- matrix(c(1,2,3,3,4,5),nrow = 3,ncol = 2,byrow = TRUE)
layout(mat = m,heights = c(0.47,0.06,0.47))
par(mar=c(4,4.2,3,4.2))

#Codes for Fig A and B
...

#Margin for legend
par(mar = c(0.2,0.2,0.1,0.1))
    # Code for legend
...

#Codes for Fig C and D
...

解决方案

Using doubleYScale from latticeExtra and the data in the long format (see my previous answer), you can simplify the work:

  1. No need to create a custom layout to superpose many plots
  2. No need to create the legend manually

The idea is to create 2 separates objects and then merge them using doubleYScale. The latter will create the second axes. I hope I get your ploygon idea since it is not very clear why do you invert it in your OP.

library(latticeExtra)
obj1 <- xyplot(Variable~TimeVariable|Type,type='l',
               groups=time,               scales=list(x=list(relation='free'),
                                                      y=list(relation='free')),
               auto.key=list(columns = 3,lines = TRUE,points=FALSE) ,

       data = subset(dat.l,time !=1))
obj2 <- xyplot(Variable~TimeVariable|Type,
               data = subset(dat.l,time ==1),type='l',
               scales=list(x=list(alternating=2),
                           auto.key=list(columns = 3,lines = TRUE,points=FALSE),
                           y=list(relation='free')),
               panel=function(x,y,...){
         panel.xyplot(x,y,...)
         panel.polygon(x,y,col='violetred4',border=NA,alpha=0.3)
               })


doubleYScale(obj1, obj2, add.axis = TRUE,style1 = 0, style2 = 1)

这篇关于在R中的多个情节中的传说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆