R円グラフ


円グラフ

円グラフは、データの円形のグラフィカルビューです。

関数を使用してpie()円グラフを描画します。

# Create a vector of pies
x <- c(10,20,30,40)

# Display the pie chart
pie(x)

結果:

例の説明

ご覧のとおり、円グラフは、ベクトルの値(この場合は10、20、30、40)ごとに1つの円を描画します。

デフォルトでは、最初の円のプロットはx軸から始まり、反時計回りに移動します。

注:各パイのサイズは、次の式を使用して、値を他のすべての値と比較することによって決定されます。

値をすべての値の合計で割った値:x/sum(x)


開始角度

パラメータを使用して、円グラフの開始角度を変更できinit.angleます。

の値はinit.angle度単位の角度で定義されます。デフォルトの角度は0です。

最初のパイを90度で開始します。

# Create a vector of pies
x <- c(10,20,30,40)

# Display the pie chart and start the first pie at 90 degrees
pie(x, init.angle = 90)

結果:


ラベルとヘッダー

パラメータを使用labelして円グラフにラベルを追加し、main パラメータを使用してヘッダーを追加します。

# Create a vector of pies
x <- c(10,20,30,40)

# Create a vector of labels
mylabel <- c("Apples", "Bananas", "Cherries", "Dates")

# Display the pie chart with labels
pie(x, label = mylabel, main = "Fruits")

結果:


col次のパラメータを使用して、各円に色を追加できます。

# Create a vector of colors
colors <- c("blue", "yellow", "green", "black")

# Display the pie chart with colors
pie(x, label = mylabel, main = "Fruits", col = colors)

結果:


伝説

各パイの説明のリストを追加するには、次のlegend()関数を使用します。

# Create a vector of labels
mylabel <- c("Apples", "Bananas", "Cherries", "Dates")

# Create a vector of colors
colors <- c("blue", "yellow", "green", "black")

# Display the pie chart with colors
pie(x, label = mylabel, main = "Pie Chart", col = colors)

# Display the explanation box
legend("bottomright", mylabel, fill = colors)

結果:

凡例は次のいずれかに配置できます。

bottomright、、、、、、、、、、 bottom_ bottomleft_ left_ topleft_ top_ topright_ right_ center