2017-08-31 106 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

太棒了!非常感谢你 !我永远无法自己做到这一点。 –