Thank you everyone! Really appreciate your help.

Regards,
Sumeet Singla

Sent from my iPhone

On Mar 18, 2019, at 5:21 PM, Ruiz, Ana (Clinical Pharmacology) 
<ana.r...@pfizer.com<mailto:ana.r...@pfizer.com>> wrote:

Dear Sumeet,

The code below is not mine but I have used it in the past:
(https://gist.github.com/jslefche/e4c0e9f57f0af49fca87)

#Function for double 
axis###########################################################

ggplot_dual_axis = function(plot1, plot2, which.axis = "x") {

  # Update plot with transparent panel
  plot2 = plot2 + theme(panel.background = element_rect(fill = NA))

  grid.newpage()

  # Increase right margin if which.axis == "y"
  if(which.axis == "y") plot1 = plot1 + theme(plot.margin = unit(c(0.7, 1.5, 
0.4, 0.4), "cm"))

  # Extract gtable
  g1 = ggplot_gtable(ggplot_build(plot1))

  g2 = ggplot_gtable(ggplot_build(plot2))

  # Overlap the panel of the second plot on that of the first
  pp = c(subset(g1$layout, name == "panel", se = t:r))

  g = gtable_add_grob(g1, g2$grobs[[which(g2$layout$name=="panel")]], pp$t, 
pp$l, pp$b, pp$l)

  # Steal axis from second plot and modify
  axis.lab = ifelse(which.axis == "x", "axis-b", "axis-l")

  ia = which(g2$layout$name == axis.lab)

  ga = g2$grobs[[ia]]

  ax = ga$children[[2]]

  # Switch position of ticks and labels
  if(which.axis == "x") ax$heights = rev(ax$heights) else ax$widths = 
rev(ax$widths)

  ax$grobs = rev(ax$grobs)

  if(which.axis == "x")

    ax$grobs[[2]]$y = ax$grobs[[2]]$y - unit(1, "npc") + unit(0.15, "cm") else

      ax$grobs[[1]]$x = ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")

  # Modify existing row to be tall enough for axis
  if(which.axis == "x") g$heights[[2]] = g$heights[g2$layout[ia,]$t]

  # Add new row or column for axis label
  if(which.axis == "x") {

    g = gtable_add_grob(g, ax, 2, 4, 2, 4)

    g = gtable_add_rows(g, g2$heights[1], 1)

    g = gtable_add_grob(g, g2$grob[[6]], 2, 4, 2, 4)

  } else {

    g = gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)

    g = gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

    g = gtable_add_grob(g, g2$grob[[7]], pp$t, length(g$widths), pp$b - 1)

  }

  # Draw it
  grid.draw(g)

}

################################################################

<image001.png>
Ana Ruiz, Pharm.D, Ph.D
Clinical Pharmacology, Sr. Director
10646 Science Center Dr., CB10, office 2439
San Diego, CA 92121
Office: 858 622 3143
Cell: 858 336 1791
Fax: 877 481 0722

From: owner-nmus...@globomaxnm.com<mailto:owner-nmus...@globomaxnm.com> 
<owner-nmus...@globomaxnm.com<mailto:owner-nmus...@globomaxnm.com>> On Behalf 
Of Singla, Sumeet K
Sent: Monday, March 18, 2019 2:54 PM
To: nmusers@globomaxnm.com<mailto:nmusers@globomaxnm.com>
Subject: [EXTERNAL] [NMusers] How to add secondary Y-axis in RStudio?

Everyone,

I know this is not R discussion group but since many people here use RStudio 
for data plotting and visualization in conjunction with NONMEM, I thought I 
might get help here. Anyone knows how to add secondary Y-axis with its own 
“independent” scaling? Reason I said independent is because right now I can add 
secondary Y-axis in ggplot but its scaling is dependent as a factor of primary 
Y-axis and I don’t want that. I am also using GraphPad which helps in 
independent scaling of secondary Y-axis but the process is too long. So, any 
options and help will be appreciated. Thank you!

Regards,
Sumeet Singla
Graduate Student
Dpt. of Pharmaceutics & Translational Therapeutics
College of Pharmacy- University of Iowa

Reply via email to