在yaxis而不是xaxis中绘制ggplot2中的平滑线条

时间:2016-11-01 08:43:59

标签: r ggplot2 axis smooth

我创建了以下情节:

数据:

from datetime import datetime
import glob
import csv
import os

dirpath, dirnames, filenames = next(os.walk('/strm1/serino/DATA'))

for dirname in dirnames:
    if len(dirname) >= 8:
        try:
            dt = datetime.strptime(dirname[:8], '%m%d%Y')
            print(dt, dirname)
            csv_folder = os.path.join(dirpath, dirname)

            for csv_file in glob.glob(os.path.join(csv_folder, 'figs', '*.csv')):
                with open(csv_file, newline='') as f_input:
                    csv_input = csv.reader(f_input)

                    for row in csv_input:
                        print(row)

        except ValueError as e:
            pass

图表:

    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)

但是,这不是我想要的最终情节。我想保持黄土回归的输出,如上图所示,但是在x轴上显示prop,在y轴上显示lat。我知道对于统计来说,正确的是将因变量(即prop)保持在y轴上,但是我的自变量(即lat)表示纬度,并且在垂直位置显示纬度会很有趣(y轴)。

如果我只是改变以下代码行,说明x = prop和y = lat我的黄土回归将尊重这个陈述,并且不会产生理想的拟合(即,prop作为lat的函数)。

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

关于如何准确再现上图(即保持黄土回归)的任何想法,但在x轴和纬度为y轴的道具?

1 个答案:

答案 0 :(得分:0)

虽然stat_smooth无法直接执行此操作,但您只需使用coord_flip即可获得所需内容。你基本上构建了你想要的图,并翻转x和y轴。在以下示例中,gg是表示您提供的ggplot代码的ggplot2对象:

gg + coord_flip()

enter image description here