簡體   English   中英

在ggplot2直方圖中的每個方面選擇不同的中斷(不是比例問題)

[英]Choose different breaks per facet in ggplot2 histogram (not a scale issue)

我想在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)

在此處輸入圖片說明

第二,我得到了正確圖表的預期結果。

而且我無法同時獲得每個圖表的預期結果,如下所示:

在此處輸入圖片說明

我們可以使用scales包中的pretty_breaks 以下代碼與您的第一次嘗試相同,除了我添加了一行: 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))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM