繁体   English   中英

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

[英]4 Panel Double Line Plot with Two Y-Axes

我正在尝试创建一个具有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")

我将为您提供一个Lattice / LatticeExtra解决方案,因为没有简单的方法来使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)

那会产生

双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

y双自由y轴图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM