0

I have created the following plot:

The data:

    lat<-c(37.30,37.30,37.30,37.30,69.25,69.25,37.30,0.00,0.00,37.30,37.30,37.30,37.30,-75.00,-75.00,70.00,25.30,25.30,37.30,45.00,46.75,-49.00,-49.00,-49.00,58.50,-37.00,37.30,37.30,37.30,37.30,69.25,69.25,37.30,0.00,0.00,37.30,37.30,37.30,37.30,-75.00,-75.00,70.00,25.30,25.30,37.30,-49.00,-49.00,-49.00,16.10,-9.12,50.00,30.00)
    prop<-c(64, 62, 38, 37, 50, 30, 27, 10, 25, 39, 25,  6,  5, 25, 47, 24, 20,  2, 62, 40, 48, 60, 20, 40, 66, 57, 14, 25, 11,  0,  3,  5,  0,  0,  0, 14, 10, 11, 9,  1,  1, 34, 20, 90,  0,  0,  0,  0,  4,  5, 85,  6)
    group<-c("gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","gnc","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr","mr")
    mydata<-data.frame(lat,prop,group)

The graph:

    library(ggplot2)
    library(scales)

    ggplot(data=mydata,aes(x=lat,y=prop,colour=group))+
    geom_point(size=2.3) +
    stat_smooth(span=0.75,n=26,method="loess",aes(fill=group)) +
    theme_bw()+
    theme(
      panel.grid.minor=element_blank(),
      panel.grid.major=element_blank(),
      legend.position=c(0.15,0.85),
      legend.title=element_blank(),
      axis.text=element_text(size=17),
      axis.title=element_text(size=19),
      legend.text=element_text(size=17)
    )+
    scale_y_continuous(limit=c(0,NA),oob=squish)+ 
    scale_x_continuous(limit=c(-70,70),oob=squish,breaks=c(-60,-30,0,30,60))

However, this is not the final plot that I want. I would like to keep the output from the loess regressions as shown in the plot above but present the graph with prop in the x-axis and lat in the y-axis. I know that for statistics the correct is to keep the dependent variable (i.e., prop) in the y-axis, but my independent variable (i.e., lat) represents Latitude, and it would be interesting to show latitude in the vertical position (y-axis).

If I simply change the following line of code stating that x=prop and y=lat my loess regressions will respect this statement and will not produce the desirable fit (i.e., prop as a function of lat).

    ggplot(data=mydata,aes(x=prop,y=lat,colour=group))+

Any ideas on how to reproduce exactly the plot above (i.e., maintaining the loess regressions as they are) but with prop in x-axis and lat in y-axis?

Suzana
  • 21
  • 2
  • 3
    try adding `coord_flip()` at the end of your `ggplot()` call – mtoto Nov 01 '16 at 08:50
  • Possible duplicate of [ggplot2: geom\_smooth select observations connections (equivalence to geom\_path())](http://stackoverflow.com/questions/39476914/ggplot2-geom-smooth-select-observations-connections-equivalence-to-geom-path) – Axeman Nov 01 '16 at 10:53

1 Answers1

0

Although stat_smooth has no means of doing this directly, you can simply use coord_flip to get what you want. You essentially build the plot you want, and flip the x and y axis around. In the following example gg is the ggplot2 object representing the ggplot code you provided:

gg + coord_flip()

enter image description here

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • If this answers your question, you can accept it by clicking on the green tick mark just below the up/downvote counter next to my answer. – Paul Hiemstra Nov 02 '16 at 09:51
  • I did it and it says that my vote is recorded but does not appear here because I do not have enough reputation, sorry. – Suzana Nov 03 '16 at 10:02
  • It is true you cannot upvote because of your limited reputation, but as a question asker you can always determine which of the answers you got is the correct one. Just below my upvote/downvote arrows there should be a tick mark that has no fill color. Clicking on it will make it green and signal that you accept my answer. – Paul Hiemstra Nov 03 '16 at 14:53