简体   繁体   中英

4 Panel Double Line Plot with Two Y-Axes

I am trying to create a graph that has 4 panels, 1 for each 'Male'. Each panel will have two lines, one for each y axis. The first y axis has 'A' on the left and the second y-axis would have 'B' on the right y-axis. The x-axis doesn't represent anything other than the fact that there are 15 measurements per male. I want to set limits for each y-axis as well. The limits are as follows:

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

I don't need a legend and can figure out different line styles, colors, and axis labels I think. The only quirk might be if I want one overall axis label on the left side of the plots labelled 'A' and one axis label on the right side of the plots labelled 'B', so the axis labels aren't overly redundant.

I really have no idea where to start because there are two issues: the 4 panels and each panel having two y-axes. Any help is greatly appreciated!

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")

I'm going to give you a Lattice/LatticeExtra solution because there's no easy way to get ggplot2 to make a second y-axis. I'm assuming your original data set is named dd . Then I would do

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)

And that produces

双Y面板图

Here all the ranges are the same so you can more directly compare the samples.

If you really want separate y-axis for each plot then try

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轴图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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