ggplot:带有线性标签的对数刻度

时间:2018-01-17 18:34:21

标签: r ggplot2

我希望记录变换我的数据但是有一个轴线,其线性值对应于日志刻度。例如,在Iversen和Soskice 2002的以下PDF的第3页中。数据已被转换,但标签在其相应的线性值中是为了便于阅读。

http://faculty.washington.edu/cadolph/vis/vishw1.pdf

这是一些可重现的数据和我对情节的开始:

@IBAction func resetTapped(_ sender: Any) {

    if audioEngine.isRunning {

        audioEngine.stop()
        recognitionRequest?.endAudio()

    }

    globalVariables.tempText = ""
    globalVariables.finalText = ""
    globalVariables.boolRecording = false

    self.inView.text = ""
    self.resultView.text = ""

    inView.backgroundColor = UIColor.red

}

private let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?
private let audioEngine = AVAudioEngine()

func startRecording() -> String {

    self.inView.text = ""

    if recognitionTask != nil {

        recognitionTask?.cancel()
        recognitionTask = nil

    }

    let audioSession = AVAudioSession.sharedInstance()

    do {

        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try audioSession.setMode(AVAudioSessionModeMeasurement)
        try audioSession.setActive(true, with: .notifyOthersOnDeactivation)

    } catch {

        print("audioSession properties weren't set because of an error.")

    }

    recognitionRequest = SFSpeechAudioBufferRecognitionRequest()

    guard let recognitionRequest = recognitionRequest else {

        fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")

    }

    recognitionRequest.shouldReportPartialResults = true

    recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in

        var isFinal = false

        if result != nil {

            if globalVariables.boolRecording == true {

                self.inView.text = result?.bestTranscription.formattedString
                globalVariables.tempText = self.spaceChanges(String: self.recordingChanges(String: self.inView.text))
                self.resultView.text = globalVariables.finalText + globalVariables.tempText

            }

            isFinal = (result?.isFinal)!

        }

        if error != nil || isFinal {

            self.audioEngine.stop()
            self.audioEngine.inputNode.removeTap(onBus: 0)

            self.recognitionRequest = nil
            self.recognitionTask = nil

        }

    })

    audioEngine.inputNode.removeTap(onBus: 0)

    let recordingFormat = audioEngine.inputNode.outputFormat(forBus: 0)

    audioEngine.inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in self.recognitionRequest?.append(buffer) }

    audioEngine.prepare()

    do {
        try audioEngine.start()
    } catch {
        print("audioEngine couldn't start because of an error.")
    }
    return self.inView.text
}

有什么想法吗?我见过其他类似的问题,但没有一个问题有效(例如Linear Ticks on a log plot in R's GGplot)。

4 个答案:

答案 0 :(得分:5)

trans内尝试scale_x_contineous,仅转换轴。

ggplot(data, aes(parties, poverty))+
    geom_point(size=2)+
    scale_x_continuous(
        trans = "log10",
        breaks = 1:10
    )

答案 1 :(得分:3)

这与其他两个答案类似,但给你一些不同的东西:

ggplot(data, aes(parties, poverty))+
  geom_point(size=2)+
  scale_x_log10(breaks = scales::log_breaks(n = 10)) +
  annotation_logticks(sides = "b")

如果你想在x轴和y轴上都需要它,那么你需要添加:

 scale_y_log10(breaks = scales::log_breaks(n = 10))

并在sides = "b"函数中将sides = "bl"更改为annotation_logticks()

答案 2 :(得分:3)

coord_trans正是如此:

ggplot(data, aes(parties, poverty))+
  geom_point(size=2)+
  coord_trans(x = 'log10')

enter image description here

答案 3 :(得分:1)

breaks参数涵盖的内容(结帐?scale_x_log10)。

对于您当前的问题,您可以将最后一行修改为:

scale_x_log10(breaks = 1:999)

scale_x_log10(breaks = 2*(1:999))  # just even numbers