Discussion:
Second y-axis on a log scale?
n***@gmail.com
2018-03-21 15:02:38 UTC
Permalink
Hello,

I was curious whether there is any way to make a second y-axis that is
scaled differently from the first. I am doing PK/PD modeling and it's usual
to plot two dependent variables vs time, but only one should be on a log
scale. Appreciate the time!
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sam Albers
2018-03-21 16:10:22 UTC
Permalink
HI Neil,

It might help if you were able to provide an example that illustrates the
plot you are trying to create.

A word of caution though - two y-axes (and especially one that is scaled
differently) are typically frowned (even scowled?) upon in the ggplot2
world for very good reason. These types of plots can be very misleading. A
common accepted usage is plotting data with two different units on each
axis (think feet and metres; Celsius and Fahrenheit). From what you are
describing, you might be trying to do something that falls into the former
category.

ggplot2 offers extremely flexible methods for faceting that might be
appropriate here.

HTH,

Sam
Post by n***@gmail.com
Hello,
I was curious whether there is any way to make a second y-axis that is
scaled differently from the first. I am doing PK/PD modeling and it's usual
to plot two dependent variables vs time, but only one should be on a log
scale. Appreciate the time!
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/
devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
n***@gmail.com
2018-03-21 16:58:58 UTC
Permalink
This is the type of graph I'm trying to replicate. I'd like to do this in R
with ggplot2. I am aware that there are decisions made in the grammar of
ggplot based on moral and aesthetic reasons. However, this is a common and
central type of graph that is used in PK/PD modeling, one that's built into
all already made programs that do it, so I would be surprised if this is
not allowed.

Best,
Chris
Post by Sam Albers
HI Neil,
It might help if you were able to provide an example that illustrates the
plot you are trying to create.
A word of caution though - two y-axes (and especially one that is scaled
differently) are typically frowned (even scowled?) upon in the ggplot2
world for very good reason. These types of plots can be very misleading. A
common accepted usage is plotting data with two different units on each
axis (think feet and metres; Celsius and Fahrenheit). From what you are
describing, you might be trying to do something that falls into the former
category.
ggplot2 offers extremely flexible methods for faceting that might be
appropriate here.
HTH,
Sam
Post by n***@gmail.com
Hello,
I was curious whether there is any way to make a second y-axis that is
scaled differently from the first. I am doing PK/PD modeling and it's usual
to plot two dependent variables vs time, but only one should be on a log
scale. Appreciate the time!
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups
"ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sam Albers
2018-03-21 18:41:01 UTC
Permalink
Hi Neil,

Here is a much more succinct distillation of why working with plots with
separate y scales can be problematic:
https://stackoverflow.com/a/3101876/5596534

I have noticed that the defense of two y-axis plots is usually of the
"other people do it" variety which I think dodges the point that they are
misleading. Whatever the case, sec_axis is implemented in ggplot2 and
Alexander's
approach should get you there.

Good luck!

Sam
Post by n***@gmail.com
This is the type of graph I'm trying to replicate. I'd like to do this in
R with ggplot2. I am aware that there are decisions made in the grammar of
ggplot based on moral and aesthetic reasons. However, this is a common and
central type of graph that is used in PK/PD modeling, one that's built into
all already made programs that do it, so I would be surprised if this is
not allowed.
Best,
Chris
Post by Sam Albers
HI Neil,
It might help if you were able to provide an example that illustrates the
plot you are trying to create.
A word of caution though - two y-axes (and especially one that is scaled
differently) are typically frowned (even scowled?) upon in the ggplot2
world for very good reason. These types of plots can be very misleading. A
common accepted usage is plotting data with two different units on each
axis (think feet and metres; Celsius and Fahrenheit). From what you are
describing, you might be trying to do something that falls into the former
category.
ggplot2 offers extremely flexible methods for faceting that might be
appropriate here.
HTH,
Sam
Post by n***@gmail.com
Hello,
I was curious whether there is any way to make a second y-axis that is
scaled differently from the first. I am doing PK/PD modeling and it's usual
to plot two dependent variables vs time, but only one should be on a log
scale. Appreciate the time!
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devt
ools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google
Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/
devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Stevens
2018-03-21 16:46:08 UTC
Permalink
There is a command for a second axis - I haven’t used it in a awhile but it looks something like this:

scale_x_continuous(sec.axis = sec_axis(trans = ~.*0.01361+0.51380, name = "Event Time",
breaks = c(0.00, 0.25,0.50,0.75,1.0)), name = "Postnatal day”,
breaks = c(0,5,10,15,20,30,45,160))


Good luck.

Alex

On Mar 21, 2018, at 8:02 AM, ***@gmail.com<mailto:***@gmail.com> wrote:

Hello,

I was curious whether there is any way to make a second y-axis that is scaled differently from the first. I am doing PK/PD modeling and it's usual to plot two dependent variables vs time, but only one should be on a log scale. Appreciate the time!

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com<mailto:***@googlegroups.com>
To unsubscribe: email ggplot2+***@googlegroups.com<mailto:ggplot2+***@googlegroups.com>
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com<mailto:ggplot2+***@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
n***@gmail.com
2018-03-21 20:19:24 UTC
Permalink
Hi,

This is my current code:


ggplot(data, aes(x=Time, y = Indirect_Effect)) +
geom_point(aes(y= Concentration/10, stat = "summary", fun.y = mean,
na.rm = TRUE) +
geom_point(aes(y=Indirect_Effect), stat = "summary",
fun.y = mean, na.rm = TRUE) +
scale_y_continuous(sec.axis = sec_axis(~.*10, name = "Concentration
(µM)")) +
facet_wrap(~Study) +
labs(y="Indirect_Effect",
x="Time (hrs)",
colour = "Parameter") +
xlim(0,25) +
geom_hline(aes(yintercept = 1), color = "cyan",
linetype = "dashed") +
geom_line(aes(y= Concentration/10), stat = "summary",
fun.y = mean, na.rm = TRUE) +
geom_line(aes(y=Indirect_Effect), stat = "summary",
fun.y = mean, na.rm = TRUE) +
stat_summary(aes(y = Indirect_Effect),
fun.data = mean_se, geom = "errorbar") +
stat_summary(aes(y = Concentration/10),
fun.data = mean_se, geom = "errorbar")


This issue with sec_axis is that it does not accept non-monotonous
transformations. If I try ~log10(.) as part of the transformation argument,
it returns an error:
Error in f(..., self = self) :
transformation for secondary axes must be monotonous

Thanks,
Chris
Post by Alexander Stevens
There is a command for a second axis - I haven’t used it in a awhile but
scale_x_continuous(sec.axis = sec_axis(trans = ~.*0.01361+0.51380, name = "Event Time",
breaks = c(0.00,
0.25,0.50,0.75,1.0)), name = "Postnatal day”,
breaks = c(0,5,10,15,20,30,45,160))
Good luck.
Alex
Hello,
I was curious whether there is any way to make a second y-axis that is
scaled differently from the first. I am doing PK/PD modeling and it's usual
to plot two dependent variables vs time, but only one should be on a log
scale. Appreciate the time!
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
https://github.com/hadley/devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brandon Hurr
2018-03-22 00:44:07 UTC
Permalink
The code is somewhat helpful... could you also give us some sample data?

I did find this:
https://stackoverflow.com/questions/44589490/using-ggplots-sec-axis-with-a-non-monotonic-transformation

And this:
https://github.com/tidyverse/ggplot2/issues/2008

They might help with getting it to work.

HTH
B
Post by n***@gmail.com
Hi,
ggplot(data, aes(x=Time, y = Indirect_Effect)) +
geom_point(aes(y= Concentration/10, stat = "summary", fun.y = mean,
na.rm = TRUE) +
geom_point(aes(y=Indirect_Effect), stat = "summary",
fun.y = mean, na.rm = TRUE) +
scale_y_continuous(sec.axis = sec_axis(~.*10, name = "Concentration
(µM)")) +
facet_wrap(~Study) +
labs(y="Indirect_Effect",
x="Time (hrs)",
colour = "Parameter") +
xlim(0,25) +
geom_hline(aes(yintercept = 1), color = "cyan",
linetype = "dashed") +
geom_line(aes(y= Concentration/10), stat = "summary",
fun.y = mean, na.rm = TRUE) +
geom_line(aes(y=Indirect_Effect), stat = "summary",
fun.y = mean, na.rm = TRUE) +
stat_summary(aes(y = Indirect_Effect),
fun.data = mean_se, geom = "errorbar") +
stat_summary(aes(y = Concentration/10),
fun.data = mean_se, geom = "errorbar")
This issue with sec_axis is that it does not accept non-monotonous
transformations. If I try ~log10(.) as part of the transformation argument,
transformation for secondary axes must be monotonous
Thanks,
Chris
Post by Alexander Stevens
There is a command for a second axis - I haven’t used it in a awhile but
scale_x_continuous(sec.axis = sec_axis(trans = ~.*0.01361+0.51380, name = "Event Time",
breaks = c(0.00,
0.25,0.50,0.75,1.0)), name = "Postnatal day”,
breaks = c(0,5,10,15,20,30,45,160))
Good luck.
Alex
Hello,
I was curious whether there is any way to make a second y-axis that is
scaled differently from the first. I am doing PK/PD modeling and it's usual
to plot two dependent variables vs time, but only one should be on a log
scale. Appreciate the time!
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devt
ools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups
"ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/
devtools/wiki/Reproducibility
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...