r - 如何在 R 中的饼图旁边制作图例?

标签 r charts legend pie-chart

我用下一个代码在 R 中制作了一个饼图:

#make slices
slices <- c(19, 26, 55)

# Define some colors 
colors <- c("yellow2","olivedrab3","orangered3")

# Calculate the percentage for each day, rounded to one decimal place
slices_labels <- round(slices/sum(slices) * 100, 1)

# Concatenate a '%' char after each value
slices_labels <- paste(slices_labels, "%", sep="")

# Create a pie chart with defined heading and custom colors and labels
pie(slices, main="Sum", col=colors, labels=slices_labels, cex=0.8)

# Create a legend at the right   
legend("topright", c("DH","UT","AM"), cex=0.7, fill=colors)

但我想要饼图旁边的图例。我也试过以下代码:legend("centreright", c("DH","UT","AM"), cex=0.7, fill=colors) .
但这并没有给我饼图旁边的图例。

我必须使用哪个代码在中间的饼图旁边制作图例?

最佳答案

你可以玩xy来自 legend 的争论(参见 ?legend):

legend(.9, .1, c("DH","UT","AM"), cex = 0.7, fill = colors)

但是,饼图可能不是表示数据的最佳方式,因为我们的眼睛不太擅长评估角度。饼图对我来说似乎合理的唯一用例是比较 2 个类别,因为由于 watch ,我们可以很容易地评估这些比例。

enter image description here

关于r - 如何在 R 中的饼图旁边制作图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206472/

相关文章:

r - 如何向 ggpairs() 添加外部图例?

r - nlminb 的 PORT 例程中的算法名称?

r - 更改 qplot (R) 中的图例大小

matlab - 添加与图表无关的自定义图例

R Shiny - 隔离使用 req() 检查先决条件的响应式(Reactive)表达式

r - dplyr掩盖GGally并破坏ggparcoord

charts - 页面加载时谷歌图表上始终可见的工具提示?

python - matplotlib savefig 修剪图形

javascript - ZingChart 的 Scale-Y 刻度对于散点图绘制错误

r - 在 R 中稍微向上推图例