在 R 中绘制粮农组织渔区地图

问题描述

我想在 R 中制作一张地图,根据数据集(在我的例子中,鲨鱼物种的长度数据)为粮农组织渔区着色。

FAO Fishing Areas

我更喜欢在 ggplot 中制作等值线图,但其他类型的地图也可以。最坏的情况是我可以添加气泡的粮农组织区域底图。即使只是现有的粮农组织区域底图也会很棒。欢迎提出任何建议!

解决方法

我转到 this page 并点击找到 this link 以检索 GeoJSON 文件:

2021-07-26 17:50:50.679871-0400 newFitnessApp[510:22218] [Assert] View <(null):0x0> does not conform to UITextInput protocol
2021-07-26 17:50:50.679947-0400 newFitnessApp[510:22218] [Assert] View <(null):0x0> does not conform to UITextInput protocol
2021-07-26 17:50:50.679972-0400 newFitnessApp[510:22218] [Assert] View <(null):0x0> does not conform to UITextInput protocol
2021-07-26 17:50:50.679990-0400 newFitnessApp[510:22218] [Assert] View <(null):0x0> does not conform to UITextInput protocol
2021-07-26 17:50:50.680006-0400 newFitnessApp[510:22218] [Assert] View <(null):0x0> does not conform to UITextInput protocol
2021-07-26 17:50:50.680120-0400 newFitnessApp[510:22218] [Assert] View <(null):0x0> does not conform to UITextInput protocol
2021-07-26 17:50:50.680146-0400 newFitnessApp[510:22218] [Assert] View <(null):0x0> does not conform to UITextInput protocol
2021-07-26 17:50:50.680165-0400 newFitnessApp[510:22218] [Assert] View <(null):0x0> does not conform to UITextInput protocol
2021-07-26 17:50:50.680182-0400 newFitnessApp[510:22218] [Assert] View <(null):0x0> does not conform to UITextInput protocol
2021-07-26 17:50:50.766468-0400 newFitnessApp[510:22218] [Snapshotting] Snapshotting a view (0x15268e680,_UIReplicantView) that has not been rendered at least once requires afterScreenUpdates:YES.

struct EditNotesView: View {
     @Binding var notes: String
    //@State private var notes: String = "type notes here"
    @Binding var isShowingSheet: Bool
    @EnvironmentObject var dataStore: DataStore
     @Environment(\.colorScheme) var colorScheme
     
     @ObservedObject var workoutDetailViewModel: WorkoutDetailViewModel
    
    var body: some View {
        VStack {
            HStack {
                Button("Cancel") {
                    isShowingSheet = false
                }
                .padding([.top,.leading])
                .foregroundColor(Color.blue)
                Spacer()
                Button("Save") {
                    if let unwrappedHKWorkout = workoutDetailViewModel.fullyLoadedWorkout?.hkWorkout {
                        print("NOW WE WILL SAVE")
                        workoutDetailViewModel.updateMetadataAndSaveWorkout(workoutToBeUpdated: unwrappedHKWorkout,updateType: .notes(notes)) { (updatedWorkout) in
                            
                        }
                    }
                    
                    isShowingSheet = false
                }
                .padding([.top,.trailing])
                .foregroundColor(Color.red)
            }
            HStack {
                TextEditor(text: $notes)
                    .font(.body)
                    .if(notes == "type notes here") { $0.foregroundColor(Color.gray) } //This is a hack until TextEditor has a placeholder option
                    .padding()
                    //This is a hack until TextEditor has a placeholder option
                    .onTapGesture {
                    
                        if notes == "type notes here" {
                            self.notes = ""
                        }
                    }
            }
        }
           .onAppear {
                        if let unwrappedNotes =  workoutDetailViewModel.fullyLoadedWorkout?.trackerMetadata.notes {
                            notes = unwrappedNotes
                            
                        } else {
                            notes = "type notes here"
                        }
                    }
    }
}

//This function is used so that TextEditor can take an optional string.  If it gets updated in SwiftUI we can delete it
func ??<T>(lhs: Binding<Optional<T>>,rhs: T) -> Binding<T> {
    Binding(
        get: { lhs.wrappedValue ?? rhs },set: { lhs.wrappedValue = $0 }
    )
}

从现在开始,我一直在关注 this example from the R graph gallery,并得到了 this SO questionthese notes 的帮助:

download.file("http://www.fao.org/fishery/geoserver/fifao/ows?service=WFS&request=GetFeature&version=1.0.0&typeName=fifao:FAO_AREAS_CWP&outputFormat=json",dest="FAO.json")

此时,library(geojsonio) library(sp) library(broom) library(ggplot2) library(dplyr) ## for joining values to map spdf <- geojson_read("FAO.json",what = "sp") 将显示区域的普通(base-R)图。

plot(spdf)

Gall projection

注释

  • 有些步骤很慢,可能值得研究如何粗化/降低空间多边形对象的分辨率(如果您只想显示图片,分辨率级别可能会过大)
  • 老实说,默认的连续配色方案可能更好,但现在所有酷孩子似乎都喜欢“viridis”,所以......
  • 可能有更好的方法来完成这些工作(例如设置地图投影、为陆地填充背景颜色,......?)