首页 文章

4面板双线图,带两个Y轴

提问于
浏览
0

我正在尝试创建一个包含4个面板的图形,每个“男性”1个面板 . 每个面板将有两条线,每条轴对应一个y轴 . 第一个y轴在左边有'A',第二个y轴在右边y轴上有'B' . x轴不代表每个男性有15个测量值的事实 . 我也想为每个y轴设置限制 . 限制如下:

男性548(40,58)和(2600,3100)男性594(36,54)和(3400,3900)男性331(42,60)和(3500,3700)男性126(35,49)和(3000, 4200)

我不需要传奇,我想可以找出不同的线条样式,颜色和轴标签 . 唯一的怪癖可能是如果我想在标记为“A”的图的左侧有一个整体轴标签,而在标有“B”的图的右侧有一个轴标签,那么轴标签不会过度冗余 .

我真的不知道从哪里开始,因为有两个问题:4个面板和每个面板有两个y轴 . 任何帮助是极大的赞赏!

structure(list(Male = c(594L, 594L, 594L, 594L, 594L, 594L, 594L, 
594L, 594L, 594L, 594L, 594L, 594L, 594L, 594L, 548L, 548L, 548L, 
548L, 548L, 548L, 548L, 548L, 548L, 548L, 548L, 548L, 548L, 548L, 
548L, 331L, 331L, 331L, 331L, 331L, 331L, 331L, 331L, 331L, 331L, 
331L, 331L, 331L, 331L, 331L, 126L, 126L, 126L, 126L, 126L, 126L, 
126L, 126L, 126L, 126L, 126L, 126L, 126L, 126L, 126L), A = c(53.07, 
46.91, 48.02, 43.36, 41.32, 39.69, 41.25, 43.9, 43.24, 41.48, 
37.22, 38.04, 37.5, 38.88, 37.51, 57.23, 51.97, 46.85, 51.02, 
41.56, 51.23, 44.79, 50.87, 46.6, 56.22, 46.98, 49.04, 50.07, 
46.32, 48.75, 53.62, 58.19, 51.29, 51.85, 55.28, 55.66, 54.14, 
49.4, 49.87, 44.81, 44.23, 47.99, 45.46, 44.9, 42.09, 43.36, 
44.52, 44.77, 49.08, 47.88, 39.24, 41.75, 48.63, 49.95, 43.57, 
41.94, 37.74, 40.97, 45.56, 45.65), B = c(3833.4, 3692.7, 3625, 
3630, 3589.7, 3506.9, 3501.6, 3606.2, 3580.6, 3530, 3479.7, 3512, 
3510.9, 3484.5, 3535.5, 2973.9, 2948.8, 2715.2, 2980.4, 2693.6, 
2888.4, 2718.5, 2971, 2752.2, 3008.5, 2718.4, 2860.2, 2848, 2893.3, 
2940.2, 3526.7, 3592.9, 3588.2, 3614.1, 3619.2, 3625.8, 3574.8, 
3650, 3678.2, 3655.6, 3675.3, 3681.3, 3680.7, 3647.5, 3670, 3640, 
3360.8, 3309.4, 3101.1, 3263.3, 3070, 3153.3, 3594, 4220, 3670, 
3367.9, 3156.7, 3431, 3440.5, 3276.7)), .Names = c("Male", "A", 
"B"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 25L, 26L, 27L, 28L, 29L, 30L, 46L, 47L, 48L, 49L, 50L, 51L, 
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L), class = "data.frame")

1 回答

  • 2

    我没有简单的方法让ggplot2制作第二个y轴 . 我假设您的原始数据集名为 dd . 然后我会这样做

    require(lattice)
    require(latticeExtra)
    pd<-dd
    pd$Male<-factor(pd$Male)
    pd$idx <- ave(rep(1, nrow(pd)), pd$Male, FUN=seq_along)
    mA<-xyplot(A~idx|Male, data=pd, type="l")
    mB<-xyplot(B~idx|Male, data=pd, type="l")
    doubleYScale(mA, mB)
    

    这就产生了

    panel plot with double Y

    这里所有范围都相同,因此您可以更直接地比较样本 .

    如果你真的想要每个情节单独的y轴,那么试试吧

    free.y <- list(y=list(relation="free"))
    mA <- xyplot(A~idx|Male, data=pd, type="l", scales=free.y)
    mB <- xyplot(B~idx|Male, data=pd, type="l", scales=free.y)
    comb <- doubleYScale(mA, mB)
    comb$x.between <- 5 #(add space otherwise panels touch)
    comb
    

    panel plot with double free y axis

相关问题