2017-09-03 6 views
2

ggplot2ヒストグラムでファセットごとに異なるブレークを選択したいのですが、数時間の検索で解決策を見つけられませんでした。私はそれが規模の問題ではないことを正確に言います。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 

私の2つの最高の試みが、私はまだ満足していない:これで

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

enter image description here

、私は」左のグラフに期待される結果が得られました。二付き

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

enter image description here

、私は右図の予想結果を持っています。

そして、私はこのように、同じ時間に各チャートの期待どおりの結果を持って管理することはできません。

enter image description here

答えて

3

我々はscalesパッケージからpretty_breaksを使用することができます。私はあなたの答えを見たときにそれはとてもシンプルに見えます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)) 

enter image description here

+0

するブレーク番号を指定するには、scale_y_continuous(breaks = pretty_breaks(5)):次のコードは、私は1つの行を追加したことを除いて、あなたの最初の試みと同じです。私は少し恥ずかしいです...ありがとう! –

+0

この良い質問をありがとう、良い再現可能な例があります。 – www

関連する問題