R 円グラフ
円グラフ
円グラフは、データを円形にグラフィカルに表示したものです。
pie()
関数を使用して円グラフを描画します。:
例の説明
ご覧のとおり、円グラフはベクトルの各値 (この場合は 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
プログラミング学習を加速させる
プログラミングをプロの講師に教えてもらいませんか。