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

How can I make legend next to my piechart in R?

我用 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)。 但这并没有给我的饼图旁边的图例。

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

您可以使用 legend 中的 xy 参数(参见 ?legend):

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

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