[英]How to place the labels further from pie chart
How to place the labels further away from pie chart in R? 如何将标签远离R中的饼图?
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices,labels = lbls, col=rainbow(length(lbls)), radius=.2)
You can modify the pie
-function line 50-54
and save it as a new function pie2
您可以修改pie
功能线50-54
并将其另存为新功能pie2
Type 类型
pie
Change line 50-54
to 将第50-54
行更改为
if (!is.na(lab) && nzchar(lab)) {
lines(c(1, 1.35) * P$x, c(1, 1.35) * P$y)
text(1.5 * P$x, 1.5 * P$y, labels[i], xpd = TRUE,
adj = ifelse(P$x < 0, 1, 0), ...)
}
Change line length (default = 1.05
) 更改行长度(默认值= 1.05
)
lines(c(1, 1.35) * P$x, c(1, 1.35) * P$y)
Change the factor (default = 1.1
) 更改因子(默认值= 1.1
)
text(1.5 * P$x, 1.5 * P$y, labels[i], xpd = TRUE,
adj = ifelse(P$x < 0, 1, 0), ...)
Now define pie2
and run the new function 现在定义pie2
并运行新函数
pie2(slices,labels = lbls, col=rainbow(length(lbls)), radius=.2)
You could manually place text with text() and create no labels by rep("",times). 您可以手动放置带有text()的文本,并且不按rep(“”,times)创建标签。
But I agree, pie-charts are a bad way to visualize data. 但我同意,饼图是一种可视化数据的坏方法。
To provide some code, 要提供一些代码,
pie(slices,labels = rep("",5), col=rainbow(length(lbls)), radius=.8,lty=4)
text(0.9,0.6,"UK")
lines(c(0.6,0.85),c(0.45,0.55))
and align everything where you want it. 并根据您的需要调整所有内容。
If you just want to create one single pie chart this is an option, but getting all those coordinates right can be very frustrating.. 如果你只是想创建一个单个饼图,这是一个选项,但正确地获得所有这些坐标可能会非常令人沮丧。
Sorry I would have commented rather then answered if I had the option. 对不起,如果我有选择,我会评论而不是回答。
You can use ggplot
to form the pie-chart. 您可以使用ggplot
来形成饼图。 This will get the labels separate from the pie-chart as a legend. 这将使标签与饼图分开作为图例。
# R version 3.2.2
install.packages("ggplot2")
library("ggplot2")
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
df = data.frame(slices = slices,labels = lbls)
ggplot(df,aes(x = factor(1),fill = labels))+
geom_bar(width = 1)+
coord_polar(theta = "y")+
theme(axis.title = element_blank())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.