2017-08-31 150 views
1

我想知道是否可以用ggplot餅圖修改x軸的刻度。ggplot餅圖選擇軸刻度

在這裏我能做些什麼:

# Some colors 
couleurs <- data.frame(
id=seq(1,17,1), 
mix=c(c(rep(1,6),rep(2,7),rep(3,4))), 
html=c("#A00020","#109618","#388EE4","#C484D1","#FFAA33","#CCCDD0","#004AC5","#F80094","#CB5023","#638995","#33CFCF","#95DC4E","#F7D633","#5C403C","#F72020","#00D96C","#FDE4C5") 
) 
couleurs$html <- factor(couleurs$html, levels = couleurs$html[order(couleurs$id, decreasing = FALSE)]) 

# Data 
activite <- data.frame(label=c("B to B","B to C","B to B/B to C", "B to B"), cible=c(rep("Externe",3), "Interne"), nb=c(12,9,3,12)) 
activite$label <- factor(activite$label, levels = activite$label[order(activite$nb[activite$cible=="Externe"], decreasing = TRUE)]) 
library(plyr) 
activite<-ddply(activite,.(cible),transform,pc=(nb/sum(nb))*100) 
activite 

# Pie chart 
library(ggplot2) 
ggplot(data = activite, aes(x = "", y = nb, fill = label)) + 
geom_bar(stat = "identity", position = position_fill(), width = 1) + 
coord_polar(theta= "y", start = 0, direction = -1) + 
labs(fill="") + 
scale_fill_manual(values=as.character(couleurs$html[1:nrow(activite)]), labels=paste(activite$label,"\t\t\t",sep="")) + 
geom_text(aes(label = paste(pc,"%", sep=" ")), size=4, colour = "white", fontface = "bold", position = position_fill(vjust = 0.5)) + 
theme(strip.text = element_text(size=20, face = "bold",), strip.background = element_rect(fill="grey75")) + 
theme(panel.background = element_rect(fill = "white")) + 
theme(plot.background = element_rect(fill = "grey92")) + 
theme(legend.position="bottom", legend.background = element_rect(fill="grey92")) + 
theme(legend.key = element_blank()) + 
theme(panel.grid.minor = element_blank(), panel.grid.major = element_line(colour = "grey75")) + 
theme(axis.text.y = element_blank()) + 
theme(axis.ticks.length = unit(0, "mm")) + 
theme(axis.title.x = element_blank(),axis.title.y = element_blank()) + 
theme(legend.box.spacing = unit(1, "mm")) + 
facet_wrap(~ cible) 

這裏我的結果:

My pie chart

幾個小時的檢索算法後,我沒有找到一個解決方案,以創造我想要的東西。完全相同的餅圖,但個性化的蜱這樣的:

The result I would like to reach

有了這些特殊條件: - 不改變餅圖中的數據的方向,我希望它像正是這種 - 如果可能的(但如果不行的話,沒關係),我希望刻度標籤不要與軸重疊。

如果有人能幫助我,我會非常感激。

回答

2

這裏有一個解決方案:

ggplot(data = activite %>% 
     group_by(cible) %>% 
     arrange(desc(nb)) %>% 
     mutate(axis.label = cumsum(nb), 
       axis.position = cumsum(pc)/100) %>% 
     mutate(axis.label = ifelse(pc == min(pc), 
            paste(axis.label, "0", sep = "-"), 
            axis.label)), 
     aes(x = 1, y = nb, fill = label)) + 
    geom_segment(aes(x = 1, xend = 1.6, y = axis.position, yend = axis.position), 
       colour = "grey75") + 
    geom_vline(xintercept = 1.6, colour = "grey75") + 
    geom_bar(stat = "identity", position = position_fill(reverse = T), width = 1) + 
    coord_polar(theta= "y", start = 0, direction = 1) + 
    labs(fill="") + 
    scale_fill_manual(values=as.character(couleurs$html[1:nrow(activite)]), labels=paste(activite$label,"\t\t\t",sep="")) + 
    geom_text(aes(label = paste(pc,"%", sep=" ")), size=4, colour = "white", 
      fontface = "bold", position = position_fill(vjust = 0.5, reverse = T)) + 
    geom_text(aes(x = 1.7, label = axis.label), size = 3, 
      position = position_fill(reverse = T)) + 
    theme(strip.text = element_text(size=20, face = "bold",), strip.background = element_rect(fill="grey75")) + 
    theme(panel.background = element_rect(fill = "white")) + 
    theme(plot.background = element_rect(fill = "grey92")) + 
    theme(legend.position="bottom", legend.background = element_rect(fill="grey92")) + 
    theme(legend.key = element_blank()) + 
    theme(panel.grid = element_blank()) + 
    theme(axis.text = element_blank()) + 
    theme(axis.ticks = element_blank()) + 
    theme(axis.title = element_blank()) + 
    theme(legend.box.spacing = unit(1, "mm")) + 
    facet_wrap(~ cible) 

plot

說明

  1. 在標籤上的序列就順時針,而極座標的方向去逆時針。這使得標籤相當麻煩。我切換了極座標的方向,&增加了reverse = T裏面的幾何位置調整調用。

  2. 很難將不同的軸斷點分配給同一圖的不同面,所以我沒有。相反,我修改了數據以包含計算軸標籤/邊距位置,通過geom_segment/geom_vline,&添加的邊距將軸文本/刻度線隱藏在theme()中。

+0

太棒了!非常感謝你 !我永遠無法自己做到這一點。 –