anup
anup

Reputation: 475

plotting two axis chart in R

I am working on a project whose ultimate result is total production, lets represent this by W. The nature of W is such that it varies with t and h. With a lot of tweaks, I managed to obtain the following 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")

This is how the original df looks like. In the above reproducible data, I've included only 30 rows.

> 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 ...

I need to plot W vs tt and hh in the same chart with two Y axis. I tried to plot with ggplot2 but I later learnt, ggplot doesnot support 2 y axes. Is there an another way to plot the data ? I would love something like these:: enter image description here enter image description here

Upvotes: 0

Views: 334

Answers (1)

Matias Andina
Matias Andina

Reputation: 4230

Here's my attempt with your data. Mind the scaling for the ternary axis

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

Just to add a powerful representation (as suggested in the comments).

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

I would also suggest surface plots.

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

Upvotes: 1

Related Questions