labeling pie chart - strange behaviour in ggplot 2.2.1.

瀏覽次數:614 次
跳到第一則未讀訊息

igreg

未讀,
2017年1月13日 清晨7:07:262017/1/13
收件者:ggplot2

hi all...


I found this example to label a pie chart...


Issue:


### data

year
<- c(1,2,1,2,1,2)
prod
<- c(1,1,2,2,3,3)
quantity
<- c(33,50,33,25,34,25)

df
<- data.frame(year, prod, quantity)
rm
(year, prod, quantity)



### plot

ibrary(ggplot2)

# center's calculated by hand
centr2
<- c(16, 25, 49, 62.5, 81, 87.5)

ggplot
(data=df, aes(x=factor(1), y=quantity, fill=factor(prod))) +
    geom_bar
(stat="identity") +
    geom_text
(aes(x= factor(1), y=centr2, label = df$quantity), size=10) +
    facet_grid
(facets = .~year, labeller = label_value) +
    coord_polar
(theta = "y")



The solution from stackoverflow:

#### Solution

library(dplyr)
library(ggplot2)

df <- df %>% group_by(year) %>% mutate(pos = cumsum(quantity)- quantity/2)

ggplot(data=df, aes(x=factor(1), y=quantity, fill=factor(prod))) +
  geom_bar(stat="identity") +
  geom_text(aes(x= factor(1), y=pos, label = quantity), size=10) +  # note y = pos
  facet_grid(facets = .~year, labeller = label_value) +
  coord_polar(theta = "y")



with the latest version of ggplot2 (2.2.1) this solutions doesn't work anymore.
the text labels are in the wrong place... shifted by 180 degrees.

My configuration:

64 bit / windows 7
ggplot2 2.2.1
R version 3.3.2 - 64 bit


any ideas to solve this problem?

thank you!

greg


ping...@gmail.com

未讀,
2017年1月13日 上午8:38:062017/1/13
收件者:ggplot2
You have to make it like this,
df <- df %>% group_by(year) %>% mutate(pos = 100-(cumsum(quantity)- quantity/2))

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

igreg

未讀,
2017年1月13日 上午9:04:142017/1/13
收件者:ggplot2

thanks for this workaround, jie ping. 
for me, it's still a strange behavior... why did the order of the label position change... it doesn't make sense!

maybe it's the same problem like here


greg

san mohan

未讀,
2017年1月13日 上午9:18:502017/1/13
收件者:ggplot2

When the positions are calculated in dplyr, the data should be ordered in the same order as in the plot. Here is the solution that works: 

year <- c(1,2,1,2,1,2)
prod <- c(1,1,2,2,3,3)
quantity <- c(33,50,33,25,34,25)

df <- data.frame(year, prod, quantity)
rm(year, prod, quantity)



df <- df %>% group_by(year) %>% arrange(year,desc(prod)) %>% mutate(pos = cumsum(quantity)- quantity/2) 

igreg

未讀,
2017年1月13日 上午10:10:192017/1/13
收件者:ggplot2
@ san mohan... 

thanks a lot for your solution... this seems to solve the problem in a general way!
have a nice weekend!

回覆所有人
回覆作者
轉寄
0 則新訊息