[R] Secondary y axis in ggplot2: did not respond when change its y-axis value

Thierry Onkelinx th|erry@onke||nx @end|ng |rom |nbo@be
Fri Jan 8 16:20:25 CET 2021

The second y-axis in ggplot2 is only intended to relabel an axis with a
fixed transformation. E.g. one axis in degree Celcius and one in Kelvin,
km and miles, ...
It does not rescale the variables.

It looks like you want to display two variables with unrelated units on the
same y-axis. That is not a good idea. https://blog.datawrapper.de/dualaxis/

ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry.onkelinx using inbo.be
Havenlaan 88 bus 73, 1000 Brussel

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey


Op vr 8 jan. 2021 om 02:02 schreef Marna Wagley <marna.wagley using gmail.com>:

> Hi R users,
> I was trying to plot a graph with a secondary axis, and used the following
> code for the data but the secondary line and secondary y -axis value did
> not match. I would like to show both lines in one graph.
> Any suggestions?
> library(ggplot2)
> library(reshape2)
> daT<-structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
> 3L, 4L, 5L, 6L, 7L, 8L), y1 = c(9754L, 1051L, 5833L, 5769L, 2479L,
> 470L, 5828L, 174L, 2045L, 6099L, 8780L, 8732L, 4053L, 9419L,
> 4728L, 3587L), y2 = c(0.51, 0.61, 0.3, 0.81, 0.89, 0, 1.9, 0.76,
> 0.87, 0.29, 0, 0.42, 0.73, 0.96, 0.62, 0.06), group = c("A",
> "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B",
> "B", "B")), class = "data.frame", row.names = c(NA, -16L))
> print(daT)
> daT1<-melt(daT, id.vars=c("x", "group"))
> daT1%>%
>   ggplot() +
>   geom_line(aes(x = x, y = value, group = variable, color = variable)) +
>   facet_wrap(~group) +
>   scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))
>         [[alternative HTML version deleted]]
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]

More information about the R-help mailing list