在R中绘制两个轴图

时间:2018-05-01 16:52:40

标签: r plot ggplot2

我正在开发一个项目,其最终结果是总生产,我们可以通过W来表示。 W的性质因th而异。通过大量调整,我设法获得了以下df

sens <- structure(list(W = c(5216400.4123, 5399804.7349, 5595563.3087, 
5792353.9932, 5993467.7466, 6189404.9279, 6380940.454, 6566630.3544, 
6747453.6816, 6917820.9796, 7086201.8275, 7248213.5225, 5402700.4252, 
5592654.9057, 5795404.8549, 5999223.7818, 6207520.1695, 6410455.1037, 
6608831.1825, 6801152.8706, 6988434.1695, 7164886.0132, 7339280.46, 
7507078.2886, 5589000.4397, 5785505.0748, 5995246.3993, 6206093.5662, 
6421572.589, 6631505.2817), t = structure(c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("t_m75_tc", 
"t_m5_tc", "t_m25_tc", "t_p0_tc", "t_p25_tc", "t_p5_tc", "t_p75_tc", 
"t_p10_tc", "t_p125_tc", "t_p15_tc", "t_p175_tc", "t_p20_tc"), class = "factor"), 
    p = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
    3L, 3L, 3L, 3L), .Label = c("h_30", "h_27.5", "h_25", "h_22.5", 
    "h_20", "h_17.5", "h_15", "h_12.5", "h_10", "h_7.5", "h_5", 
    "h_2.5", "h_1", "h0", "h1", "h2.5", "h5", "h7.5", "h10", 
    "h12.5", "h15", "h17.5", "h20", "h22.5", "h25", "h27.5", 
    "h30"), class = "factor"), tt = c(-7.5, -5, -2.5, 0, 2.5, 
    5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 
    7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5), 
    hh = c(-30, -30, -30, -30, -30, -30, -30, -30, -30, -30, 
    -30, -30, -27.5, -27.5, -27.5, -27.5, -27.5, -27.5, -27.5, 
    -27.5, -27.5, -27.5, -27.5, -27.5, -25, -25, -25, -25, -25, 
    -25)), .Names = c("W", "t", "p", "tt", "hh"), row.names = c(NA, 
30L), class = "data.frame")

这就是原始df的样子。在上面可重复的数据中,我只包含了30行。

> str(sens)
'data.frame':   324 obs. of  5 variables:
 $ W : num  5216400 5399805 5595563 5792354 5993468 ...
 $ t : Factor w/ 12 levels "t_m75_tc","t_m5_tc",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ p : Factor w/ 27 levels "h_30","h_27.5",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ tt: num  -7.5 -5 -2.5 0 2.5 5 7.5 10 12.5 15 ...
 $ hh: num  -30 -30 -30 -30 -30 -30 -30 -30 -30 -30 ...

我需要在同一个图表中用两个Y轴绘制W vs tt and hh。我试着用ggplot2绘图,但我后来才知道,ggplot不支持2 y轴。是否有另一种绘制数据的方法?我会喜欢这些:: enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

这是我对您的数据的尝试。注意三元轴的缩放

library(ggtern)
ggtern(sens, aes(x = W/max(W),z=tt/max(tt), y=hh/max(hh))) +
 geom_point(size=3, col='red') +
 theme_bw()

enter image description here

只需添加强大的表示(如评论中所示)。

ggplot(sens, aes(W, tt, group=hh, color=hh)) +
 geom_point() +
 geom_line(lwd=1) +
 theme_bw() +
 scale_color_continuous(low = "black", high="red")

enter image description here

我也建议表面图。

require(rgl)  
surface3d(x, y, z)