使用長格式(與寬格式)文字檔案中的 pgfplots 進行繪圖?

使用長格式(與寬格式)文字檔案中的 pgfplots 進行繪圖?

在統計程式語言中R,有兩種不同的形式來表示數據。這寬幅形式看起來像下面這樣:

     subject time age weight height
1 John Smith    1  33     90   1.87
2 Mary Smith    1  NA     55   1.54

長表看起來像下面這樣:

     subject variable value
1 John Smith     time  1.00
2 Mary Smith     time  1.00
3 John Smith      age 33.00
4 Mary Smith      age    NA
5 John Smith   weight 90.00
6 Mary Smith   weight 55.00
7 John Smith   height  1.87
8 Mary Smith   height  1.54

範例來源:Hadley Wickham 的。但請注意,我將 Mary 的體重改為 55,而之前的體重為 NA。

是否可以讀取長格式的文字文件,並繪製例如身高與體重的關係圖?

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}
  % the following code doesn't work, but communicates the idea
  % of what I want to work
  \addplot [x variable=height,y variable=weight] {input.txt};
\end{axis}
\end{tikzpicture}
\end{document}

我問這個問題的原因是因為我想繪製我編寫的電腦模擬程式的輸出,並且以長形式輸出比以寬形式輸出更容易。目前,我使用R腳本convert.R讀取長格式的文字檔案並將其轉換為寬格式的in.txt文字文件,這out.txtpgf圖可以閱讀。如果 pgfplots 可以in.txt直接讀取和處理而不需要我手動運行,那就更方便了convert.R

答案1

[根據我的評論轉換]:

pgfplotstable不支援“長格式”。如果您想要一些獨立於 R 的轉換腳本,您可以考慮編寫 TeX 腳本(基於pgfplotstable基本 TeX 程式設計)或 LUA 腳本。或者您R透過 shell escape 或透過呼叫腳本\addplot shell

相關內容