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

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Marna Wagley
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Expected min height of view

R help mailing list-2
I upgraded from R 4.0.2 to R 4.0.3 for Apple Mac at Duke University. Now, the only output I get from R 4.0.3 is an error message. Greg Coats
2021-01-07 22:58:42.997 R[8311:37566] Warning: Expected min height of view: (<NSPopoverTouchBarItemButton: 0x7fcb6c592570>) to be less than or equal to 30 but got a height of 32.000000. This error will be logged once per view in violation.
> version
               _                          
platform       x86_64-apple-darwin17.0    
arch           x86_64                      
os             darwin17.0                  
system         x86_64, darwin17.0          
status                                    
major          4                          
minor          0.3                        
year           2020                        
month          10                          
day            10                          
svn rev        79318                      
language       R                          
version.string R version 4.0.3 (2020-10-10)
nickname       Bunny-Wunnies Freak Out    
>
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Secondary y axis in ggplot2: did not respond when change its y-axis value

Rui Barradas
In reply to this post by Marna Wagley
Hello,

What about the following?
First get the min and max of value by variable == "y1".
Then use that range to scale up "y2".

rng <- tapply(daT1$value, daT1$variable, range)$y1

ggplot(data = daT1, aes(x = x, group = variable, color = variable)) +
   geom_line(data = subset(daT1, variable == "y1"),
             aes(y = value)) +
   geom_line(data = subset(daT1, variable == "y2"),
             aes(y = value*diff(rng) + rng[1])) +
   facet_wrap(~group) +
   scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))


Hope this helps,

Rui Barradas

Às 01:01 de 08/01/21, Marna Wagley escreveu:

> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Secondary y axis in ggplot2: did not respond when change its y-axis value

R help mailing list-2
In reply to this post by Marna Wagley
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
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
[hidden email]
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

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

<https://www.inbo.be>


Op vr 8 jan. 2021 om 02:02 schreef Marna Wagley <[hidden email]>:

> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Secondary y axis in ggplot2: did not respond when change its y-axis value

Marna Wagley
In reply to this post by Rui Barradas
Thank you Rui and Thierry for the suggestion, it helped me.
thanks


On Fri, Jan 8, 2021 at 6:58 AM Rui Barradas <[hidden email]> wrote:

> Hello,
>
> What about the following?
> First get the min and max of value by variable == "y1".
> Then use that range to scale up "y2".
>
> rng <- tapply(daT1$value, daT1$variable, range)$y1
>
> ggplot(data = daT1, aes(x = x, group = variable, color = variable)) +
>    geom_line(data = subset(daT1, variable == "y1"),
>              aes(y = value)) +
>    geom_line(data = subset(daT1, variable == "y2"),
>              aes(y = value*diff(rng) + rng[1])) +
>    facet_wrap(~group) +
>    scale_y_continuous(sec.axis = sec_axis(~ .*0.0001))
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 01:01 de 08/01/21, Marna Wagley escreveu:
> > 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]]
> >
> > ______________________________________________
> > [hidden email] 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]]

______________________________________________
[hidden email] 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.