Bland–Altman Plot in ggplot2

Peter Dalgaard 著 “Introductory Statistics with R” の Ch. 5.6 “Paired t test” を読んでいたところ「対応のある2群の差を可視化して確認するには、対応同士の平均と差の分散図を描くとヨイ」といったようなことが書かれていた。このプロットを Bland–Altman Plot と呼ぶらしい。 せっかくなので ggplot2 で描いてみた。 bland_altman_plot <- function(dat){   dat$diff <- dat$pre – dat$post   ggplot(dat, aes(pre, diff)) +   geom_hline(yintercept = mean(dat$diff) + c(-2, 0, 2) * sd(dat$diff), linetype=2, color=’brown’) +   geom_point() +  theme_bw() +   xlab(‘mean ofContinue reading “Bland–Altman Plot in ggplot2”

Mandelbrot Set in R

r blogger の ”A 3D Version of R’s curve() Function” という投稿のコメント欄にある lattice ライブラリの例にインスパイアされて、マンデルブロー集合(Mandelbrot Set)を R & ggplot2 /lattice で 2D/3D グラフを書いてみた。 Definition Mandelbort 集合はz_{n+1} = z_n^2 + c, c in C(複素数), z_0 = 0 で定義される漸化式で、n -> ∞ の時に収束するような c 全体の集合。 Program library(ggplot2) library(lattice) g <- function(x0, y0) { x <- 0 y <- 0 forContinue reading “Mandelbrot Set in R”

[R]Play with ggplot2 Part 03 “Positive/Negative BarPlot”

“CHART OF THE DAY” をみていたら、 Positive/Negative Value をとり、正負によって色を分ける棒グラフが利用されていた。 CHART OF THE DAY: Here’s Where Stocks Soared And Got Slammed In 2010 DailyFinance :Best and Worst Global Stock Markets of 2010 同じようなグラフを ggplot2 で実現してみた。 Program library(ggplot2) df <- data.frame( country = c(‘Philippines’, ‘Thailand’, ‘Peru’, ‘Chile’, ‘Malaysia’, ‘Hungary’, ‘Portugal’, ‘Italy’, ‘Spain’, ‘Greece’), total_return = c(58.11, 55.79, 54.65,Continue reading “[R]Play with ggplot2 Part 03 “Positive/Negative BarPlot””

[R]Draw Mt. Fuji in R

はてなをみてたら、過去に静岡大学で出題されたという数学の問題がたくさんブックマークされていた。 正しくグラフを描くと富士山が浮かびあがる。 現実逃避に R で書いてみた。 # normal R version f <- function(x) { ifelse (abs(x) <= 1, (x ^ 4 – x ^ 2 + 6), 12 / (abs(x) + 1)) } g <- function(x) { ifelse (abs(x) <= 2, (0.5 * cos( 2 * x * pi) + 7/2), NA) } plot(c(-8, 8), c(-4,Continue reading “[R]Draw Mt. Fuji in R”

[R]Play with ggplot2 Part 02 “error bar”

ggplot2 になれるために、いろいろいじってみる。今回のテーマは error bar。mean は geom_bar/geom_point それぞれでグラフ化。 国立がん研究センターのがん情報サービスで公開されている 5年相対生存率 のデータをもとに、以下をグラフ化。 部位別がん患者5年相対生存率(主要部位) 部位別がん患者5年相対生存率(詳細部位) 出典 厚生労働省がん研究助成金「地域がん登録精度向上と活用に関する研究」平成16年度報告書 Tsukuma H, Ajiki W, Ioka A, Oshima A, and Research Group of Population-Based Cancer Registry of Japan, Survival of cancer patients diagnosed in 1993-96: collaborative study of population-based cancer registries in Japan, Japanese Journal of Clinical Oncology, 36: 602-607, 2006 部位別がん患者5年相対生存率(主要部位)Continue reading “[R]Play with ggplot2 Part 02 “error bar””

Tutorial of ggplot2 by Hadley Wickham at ISM

大学共同利用機関法人 情報・システム研究機構 統計数理研究所(The Institute of Statistical Mathematics)で開催された R のグラフィックパッケージggplot2 の作者本人によるチュートリアルに参加。 Speaker Prof. Hadley Wickham (Rice University, USA) Prof. Uwe Ligges (Technische Universitat Dortmund, Germany) Program 25 November (Thu) 10:30 – 17:00 10:30-12:00 Tutorial of ggplot2 (1) by Hadley Wickham 12:00-13:00 Lunch 13:00-14:00 Tutorial of ggplot2 (2) by Hadley Wickham 14:10-15:10 Prospects and Challenges for CRAN –Continue reading “Tutorial of ggplot2 by Hadley Wickham at ISM”

[R]Play with ggplot2 Part 01 “barplot”

ggplot2 になれるために、いろいろいじってみる。 National Cancer Institute で公開されている “Epidemiology and End Results (SEER) “ のデータをもとに、以下をグラフ化。 5-Year Relative Survival (Percent) by Year of Diagnosis(5年相対生存率) Stage Distribution (%) 1999-2006c, Case Counts and Percentages(発見時のステージ分布) 5-Year Relative Survival (Percent) 1999-2006c by Stage at Diagnosis (ステージ別5年相対生存率) 選んだ癌は次の3つ。(深い意味なし) Lung and Bronchus Pancreas Liver and Intrahepatic Bile Duct 5-Year Relative Survival (Percent) by YearContinue reading “[R]Play with ggplot2 Part 01 “barplot””

[R]2 axes plotting in R

R でY軸に左右ことなるスケールのグラフをプロットする方法 標準グラフライブラリ版 データフレームで A, B, C の3つの変数があったとき、 AxB, AxC をプロットしたいとする。 手順としては、次のようにする。 AxB をプロット C の定義式を B に合わせるように変換 変換した C の定義域を利用して AxC をプロット Yの右軸にCの定義式でスケールをふる 手順(3) では B in [a, b] の場合 y = a + (b-a) * x, x in [0,1] として y in [a, b] となるようにパラメータ変換する。 datasets.longley を利用すると、次のような感じになる。 plot(longley$Year, longley$Employed, type=’l’) temp1 <- range(longley$Employed)Continue reading “[R]2 axes plotting in R”

[R]viewports’ grid in ggplot2

R の ggplot2 でレイアウトをグリッド分割し、各グリッドに対してプロットする方法。 次のように、まずグリッド分割し、セルを指定してプロットしていけばよい。 grid.newpage() pushViewport(viewport(layout = grid.layout(2, 3))) print(qplot(speed, dist, data=cars), vp=viewport(layout.pos.row = 1:2, layout.pos.col = 1)) print(qplot(log(speed), log(dist), data=cars), vp=viewport(layout.pos.row = 1, layout.pos.col = 2:3)) print(qplot(speed, dist, data=cars), vp=viewport(layout.pos.row = 2, layout.pos.col = 2)) print(qplot(log(speed), log(dist), data=cars), vp=viewport(layout.pos.row = 2, layout.pos.col = 3)) Hadley Wickham の “Ggplot2: Elegant Graphics for Data AnalysisContinue reading “[R]viewports’ grid in ggplot2”