我想在ggplot2直方图中为每个方面选择不同的中断,但是经过数小时的搜索后却找不到解决方案。我确切地说这不是规模问题。
我的工作样本:
library(plyr)
library(ggplot2)
# 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
fct_itinerants<-structure(list(order = c(1L, 2L, 3L, 4L, 6L, 1L, 2L, 5L), label = structure(c(3L,
6L, 2L, 1L, 5L, 3L, 6L, 4L), .Label = c("Chargés d'affaires",
"Chauffeurs - Livreurs", "Commerciaux", "Encadrement", "Non précisé",
"Techniciens"), class = "factor"), cible = structure(c(1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Externe", "Interne"), class = "factor"),
nb = c(15L, 14L, 6L, 2L, 2L, 12L, 12L, 1L)), .Names = c("order",
"label", "cible", "nb"), class = "data.frame", row.names = c(NA,
-8L))
# Data complements
fct_itinerants$label <- factor(fct_itinerants$label, levels = unique(fct_itinerants$label[order(fct_itinerants$order, decreasing = FALSE)]))
fct_itinerants <- ddply(fct_itinerants, .(cible), transform,pc=(nb/sum(nb))*100)
foo_col = c()
for (i in 1:nrow(fct_itinerants)) {
fct_itinerants$color[i]<-as.character(couleurs$html[as.numeric(fct_itinerants$label)[i]])
}
fct_itinerants$y_min <- 0
fct_itinerants$y_max[fct_itinerants$cible=="Externe"] <- 25
fct_itinerants$y_max[fct_itinerants$cible=="Interne"] <- 12
我的两个最好的图表尝试,但我仍然不满意:
# First try
ggplot(data = fct_itinerants , aes(x=label, y=nb, fill=label)) +
geom_bar(stat="identity", position = position_stack()) +
coord_flip() +
theme(legend.position="none") +
theme(strip.text = element_text(size=10, face = "bold", ), strip.background = element_rect(fill="grey75")) +
theme(axis.title = element_blank()) +
scale_x_discrete(limits = rev(levels(fct_itinerants$label))) +
facet_wrap(~cible, scales = "free_x") +
geom_blank(aes(y = y_min)) +
geom_blank(aes(y = y_max))
ggsave("try_1.png", width = 15, height = 5, units = "cm", dpi = 300, limitsize = TRUE)
有了这个,我得到了左图的预期结果。
# Second try
ggplot(data = fct_itinerants , aes(x=label, y=nb, fill=label)) +
geom_bar(stat="identity", position = position_stack()) +
coord_flip() +
theme(legend.position="none") +
theme(strip.text = element_text(size=10, face = "bold", ), strip.background = element_rect(fill="grey75")) +
theme(axis.title = element_blank()) +
scale_x_discrete(limits = rev(levels(fct_itinerants$label))) +
facet_wrap(~cible, scales = "free_x") +
geom_blank(aes(y = y_min)) +
geom_blank(aes(y = y_max))+
scale_y_continuous(breaks = c(seq(0, 25, by = 2)))
ggsave("try_2.png", width = 15, height = 5, units = "cm", dpi = 300, limitsize = TRUE)
对于第二个,我得到了正确图表的预期结果。
我无法在同一时间获得每张图表的预期结果,如下所示:
答案 0 :(得分:3)
我们可以使用pretty_breaks
包中的scales
。以下代码与您的第一次尝试相同,只是我添加了一行:scale_y_continuous(breaks = pretty_breaks(5))
以指定中断号为5。
library(scales)
ggplot(data = fct_itinerants , aes(x=label, y=nb, fill=label)) +
geom_bar(stat="identity", position = position_stack()) +
coord_flip() +
theme(legend.position="none") +
theme(strip.text = element_text(size=10, face = "bold", ), strip.background = element_rect(fill="grey75")) +
theme(axis.title = element_blank()) +
scale_x_discrete(limits = rev(levels(fct_itinerants$label))) +
# Specify the number of breaks using pretty_breaks
scale_y_continuous(breaks = pretty_breaks(5)) +
facet_wrap(~cible, scales = "free_x") +
geom_blank(aes(y = y_min)) +
geom_blank(aes(y = y_max))