ggplot2で円グラフを描画しようとしています。私のコードを以下に示します。
df <- data.frame(
variable = c("australia","hungary","germany","france","canada"),
value = c(632,20,491,991,20)
)
ggplot(df, aes(x = "", y = value, fill = variable)) +
geom_bar(width = 1, stat = "identity") +
scale_fill_manual(values = c("red", "yellow","blue", "green", "cyan")) +
coord_polar(theta = "y") +
labs(title = "pie chart")
パーセンテージ値を表示したいのですが。どうやってやるの?
試してみてください
df <- data.frame(
variable = c("australia","hungary","germany","france","canada"),
value = c(632,20,491,991,20)
)
library(ggplot2)
ggplot(transform(transform(df, value=value/sum(value)), labPos=cumsum(value)-value/2),
aes(x="", y = value, fill = variable)) +
geom_bar(width = 1, stat = "identity") +
scale_fill_manual(values = c("red", "yellow","blue", "green", "cyan")) +
coord_polar(theta = "y") +
labs(title = "pie chart") +
geom_text(aes(y=labPos, label=scales::percent(value)))
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加