曲线图饼图选择轴勾号 [英] ggplot pie chart choose axes ticks

查看:0
本文介绍了曲线图饼图选择轴勾号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用柱状图饼图修改x轴的刻度。

以下是我能做的:

# 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,"			",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)

这是我的结果:

经过几个小时的搜索,我没有找到一个解决方案来创建我想要的东西。完全相同的饼图,但带有个性化的勾号:

具有以下特定条件: -不要改变饼图中数据的方向,我想要的正是这样 -如果可能(但如果不可能,也可以),我希望扁虱的标签不与轴重叠。

如果有人能帮我,我将不胜感激。

推荐答案

这里有一个解决方案:

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,"			",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)

说明

  1. 标签中的顺序是顺时针方向,而极坐标的方向是逆时针。这使得标签变得相当麻烦。我切换了极坐标的方向,&;在几何的位置调整调用中添加了reverse = T

  2. 很难将不同的轴断点分配给同一绘图的不同方面,因此我没有这样做。相反,我修改了数据以包括计算的轴标签/边距位置,通过geom_segment/geom_vline添加边距,并在theme()中隐藏轴文本/勾号。

这篇关于曲线图饼图选择轴勾号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆