裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

「ナイチンゲールの鶏頭図よりは折れ線グラフ」を Julia で描く

2022年02月24日 | ブログラミング

ナイチンゲールの鶏頭図よりは折れ線グラフ...という R のプログラムも,あまり良くない気がして,Julia で書いて(描いて)みた。

R の example(Nightingale) の図

折れ線に重ねるデータポイントが,文字通り「間が抜けている」

Julia で描いた図。自画自賛。

using RDatasets, DataFrames
Nightingale = dataset("HistData", "Nightingale");

using Plots
function line(x, y, col, legend)
    plot!(x, y, color=col, label=legend)
    scatter!(x, y, markersize=3, color=col)
end

using Dates
function linechart(df)
    gr(grid=false, tick_direction=:out, titlefont=(11, "times"),
        guidefont=(10, "times"), tickfont=(8, "times"),
        markerstrokewidth=0, fontfamily=(8, "serif"),
        foreground_color_legend=nothing, label="")
    tmtick  = Date(1854, 4):Month(6):Date(1856, 3)
    tmticks = Dates.format.(tmtick, "u yyyy")
    plot(title="Causes of Mortality of the British Army in the East",
        xlabel="Month/Year", ylabel="Annual Death Rate",
        xticks=(tmtick, tmticks), xminorticks=6)
    line(df.Date, df.DiseaseRate, :blue, "Preventable disease")
    line(df.Date, df.WoundsRate, :red, "Wounds and injuries")
    line(df.Date, df.OtherRate, :black, "Other")
    plot!(df.Date[[1, 12, 12, 1]], [0, 0, 1100, 1100],
        seriestype=:shape, fillcolor="gray10", alpha=0.04, legend=:right)
    annotate!(df.Date[1], 1000, text("Before Sanitary Commission", "times", :left, 10))
    annotate!(df.Date[13], 1000, text("After Sanitary Commission", "times", :left, 10))
end

linechart(Nightingale)


コメント (1)    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« ナイチンゲールの鶏頭図--再び | トップ | Julia で統計解析 一応の完 »
最新の画像もっと見る

1 コメント

コメント日が  古い順  |   新しい順
matplot (中澤)
2022-02-25 19:08:28
example(Nightingale)では、なぜかplot(type="n")してからpoints()で描画していますが、普通はmatplot()ですよね
> matplot(Nightingale[,1], Nightingale[,8:10], type="b", pch=16, col=c(4,2,1), xlab="Date", ylab="Annual Death Rate")
> legend("right", pch=16, col=c(4,2,1), lty=1, legend=c("Disease", "Wound", "Other"), title="Causes")
の2行だけで(後でrect()は追加するにしても)描けます
返信する

コメントを投稿

ブログラミング」カテゴリの最新記事