+ Reply to Thread
Results 1 to 5 of 5

Thread: how to plot a scatterplot with three different y axes?

  1. #1
    TS Contributor
    Points: 40,089, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Downloads
    gianmarco's Avatar
    Location
    Italy
    Posts
    1,367
    Thanks
    232
    Thanked 301 Times in 225 Posts

    how to plot a scatterplot with three different y axes?




    Hello,

    I have a dataset like the following:
    Code: 
    mydata <- structure(list(historical_sources = c(7.5, 6, 10, 2.5, 4.5, 2, 
    4.5, 2.5, 1, 7, 4.5, 9, 2.5, 2.5, 1.5, 3, 4, 3.5, 6.5, 7, 12.5, 
    15.5), May = c(5.1, 14.3, 16.9, 2.5, 30.8, 2.4, 7.7, 1.7, 1.7, 
    5.3, 2.5, 7.8, 4.9, 3.1, 2.9, 1.9, 1.9, 6.9, 4.7, 11.5, 8.5, 
    10.8), July = c(4.3, 22.6, 22.9, 5.6, 22.3, 4.4, 10.5, 1.9, 1.9, 
    4.8, 3.4, 8, 8, 3.9, 8.6, 1.4, 1.4, 3.2, 8.2, 14.6, 9.5, 16.1
    ), October = c(8.7, 20.4, 15.9, 4.6, 8.9, 6.4, 10, 4.5, 4.5, 
    9.1, 7.4, 14.8, 2.1, 6.5, 1.9, 1.8, 1.8, 4.1, 8.7, 12.3, 4.9, 
    4.6), May_diff = c(-2.4, 8.3, 6.9, 0, 26.3, 0.4, 3.2, -0.8, 0.7, 
    -1.7, -2, -1.2, 2.4, 0.6, 1.4, -1.1, -2.1, 3.4, -1.8, 4.5, -4, 
    -4.7), July_diff = c(-3.2, 16.6, 12.9, 3.1, 17.8, 2.4, 6, -0.6, 
    0.9, -2.2, -1.1, -1, 5.5, 1.4, 7.1, -1.6, -2.6, -0.3, 1.7, 7.6, 
    -3, 0.6), October_diff = c(1.2, 14.4, 5.9, 2.1, 4.4, 4.4, 5.5, 
    2, 3.5, 2.1, 2.9, 5.8, -0.4, 4, 0.4, -1.2, -2.2, 0.6, 2.2, 5.3, 
    -7.6, -10.9)), .Names = c("historical_sources", "May", "July", 
    "October", "May_diff", "July_diff", "October_diff"), class = "data.frame", row.names = c(NA, 
    -22L))
    I would like to plot a scatterplot in which x is the "historical sources" variable, while three variables (i.e., May_diff, July_diff, and October_diff) should be displayied on three different y axes.

    I cannot wrap my head around how to get that (either in R base or ggplot2).

    Any elucidation is (as usual) highly appreciated.

    Best
    Gm
    http://cainarchaeology.weebly.com/

  2. #2
    Omega Contributor
    Points: 38,284, Level: 100
    Level completed: 0%, Points required for next Level: 0
    hlsmith's Avatar
    Location
    Not Ames, IA
    Posts
    6,991
    Thanks
    397
    Thanked 1,185 Times in 1,146 Posts

    Re: how to plot a scatterplot with three different y axes?

    Can you have the two y-axes and gradient shade the plots based on the potential 3rd y-axis variable?
    Stop cowardice, ban guns!

  3. #3
    Super Moderator
    Points: 31,766, Level: 100
    Level completed: 0%, Points required for next Level: 0
    bugman's Avatar
    Posts
    2,255
    Thanks
    290
    Thanked 324 Times in 265 Posts

    Re: how to plot a scatterplot with three different y axes?

    GM, I ran into problems similar to this but ended up just using a combination of fill and color arguments and also faceting to de-clutter my output.

    However, you could try using the "sec_axis" argument in "scale_y_continuous"


    https://twitter.com/hadleywickham/st...932225?lang=en
    Last edited by bugman; 06-15-2017 at 07:26 PM.
    The earth is round: P<0.05

  4. #4
    Points: 7,821, Level: 59
    Level completed: 36%, Points required for next Level: 129

    Posts
    159
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Re: how to plot a scatterplot with three different y axes?

    Another option would be package rgl, that allows 3D plots.
    Prediction is very difficult, especially about the future. (Niels Bohr)

  5. #5
    Probably A Mammal
    Points: 31,087, Level: 100
    Level completed: 0%, Points required for next Level: 0
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    2,564
    Thanks
    398
    Thanked 618 Times in 551 Posts

    Re: how to plot a scatterplot with three different y axes?


    I don't think 3D plots are useful in 99% of cases, and this would be one of them. Here is what I would do

    Code: 
    library(ggplot2)
    x <- reshape2::melt(mydata[c("historical_sources", "May_diff", "July_diff", "October_diff")], id.vars = "historical_sources")
    ggplot(x) + aes(historical_sources, value) + geom_point() + facet_wrap(~variable)
    Basically, reshape your data from wide-to-long format where each row tuple is (historical_source, variable, value) pairs, and variable takes on the label of *_diff. Then we simply plot the (historical_source, value) pairs and facet into 3 separate plots according to the variable labels. You can keep them on the shared value-axis or let them scale freely (facet_wrap parameter scale = "free_y")

    Additionally, you can remove the faceting and color according to variable in aes (color = variable).

    It really depends on what relationship you're trying to capture in this visualization. The per historical source deviation? The distribution (maybe boxplots)? Are the historical sources supposed to be categorical? (There are a lot of single instances per source here).

    Visualizing isn't hard. It's building the right visual for the intended purpose/application that is hard. You should clarify that purpose a bit more!
    You should definitely use jQuery. It's really great and does all things.

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats