Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

r - Pie plot getting its text on top of each other

Just trying to fix this overlapped labeling:

My code:

  values=c(164241,179670)
  labels=c("Private", "Public")
  colors=c("#cccccc", "#aaaaaa")
  categoriesName="Access"
  percent_str <- paste(round(graph$values / sum(graph$values) * 100,1), "%", sep="")

  values <- data.frame(val = graph$values, Type = graph$labels, percent=percent_str )

  pie <- ggplot(values, aes(x = "", y = val, fill = Type)) + geom_bar(width = 1) + 
          geom_text(aes(y = **val + 1**, **hjust=0.5**, **vjust=-0.5**, label = percent), colour="#333333", face="bold", size=10) +
          coord_polar(theta = "y") + ylab(NULL) + xlab(NULL) +
          scale_fill_manual(values = graph$colors) + labs(fill = graph$categoriesName) +
          opts( title = graph$title, 
                axis.text.x = NULL,
                plot.margin = unit(c(0,0,0,0), "lines"), 
                plot.title = theme_text(face="bold", size=14), 
                panel.background = theme_rect(fill = "white", colour = NA) )
  print(pie)

Tried messing with the values marked with asterisks (** **) but haven't got anywhere. Any help appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

here is an example:

pie <- ggplot(values, aes(x = "", y = val, fill = Type)) + 
  geom_bar(width = 1) + 
  geom_text(aes(y = val/2 + c(0, cumsum(val)[-length(val)]), label = percent), size=10)
pie + coord_polar(theta = "y")

enter image description here

Perhaps this will help you understand how it work:

pie + coord_polar(theta = "y") + 
  geom_text(aes(y = seq(1, sum(values$val), length = 10), label = letters[1:10]))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...