2017-10-25 18 views
6

मैं ggplot2 पैकेज के geom_bar में भरने के क्षेत्र के विषय में एक प्रश्न है geom_bar।आदेश और 2 अलग चर के साथ भरने के ggplot2 आर

मैं एक चर (अगले उदाहरण चर var_fill कहा जाता है) के साथ मेरी geom_bar को भरने के लिए लेकिन एक और चर के साथ geom_plot (बुलाया clarity उदाहरण में) आदेश चाहते हैं।

मैं यह कैसे कर सकता हूं?

बहुत बहुत धन्यवाद!

उदाहरण:

rm(list=ls()) 

set.seed(1) 

library(dplyr) 
data_ex <- diamonds %>% 
    group_by(cut, clarity) %>% 
    summarise(count = n()) %>% 
    ungroup() %>% 
    mutate(var_fill= LETTERS[sample.int(3, 40, replace = TRUE)]) 

head(data_ex) 

# A tibble: 6 x 4 
    cut clarity count var_fill 
    <ord> <ord> <int> <chr> 
1 Fair  I1 210  A 
2 Fair  SI2 466  B 
3 Fair  SI1 408  B 
4 Fair  VS2 261  C 
5 Fair  VS1 170  A 
6 Fair VVS2 69  C 

मैं बक्से के इस आदेश [स्पष्टता] चाहते हैं: बक्से के इस भरने (रंग) [var_fill] के साथ

library(ggplot2) 
ggplot(data_ex) + 
    geom_bar(aes(x = cut, y = count, fill=clarity),stat = "identity", position = "fill", color="black") 

enter image description here

:

ggplot(data_ex) + 
    geom_bar(aes(x = cut, y = count, fill=var_fill),stat = "identity", position = "fill", color="black") 

enter image description here

EDIT1: जवाब missuse द्वारा पाया:

p1 <- ggplot(data_ex) + geom_bar(aes(x = cut, y = count, group = clarity, fill = var_fill), stat = "identity", position = "fill", color="black")+ ggtitle("var fill") 

p2 <- ggplot(data_ex) + geom_bar(aes(x = cut, y = count, fill = clarity), stat = "identity", position = "fill", color = "black")+ ggtitle("clarity") 

library(cowplot) 
cowplot::plot_grid(p1, p2) 

enter image description here

EDIT2: अब मैं missuse की मदद से ggmosaic विस्तार के साथ ऐसा करने की कोशिश की

rm(list=ls()) 
set.seed(1) 
library(ggplot2) 
library(dplyr) 
library(ggmosaic) 

data_ex <- diamonds %>% 
    group_by(cut, clarity) %>% 
    summarise(count = n()) %>% 
    ungroup() %>% 
    mutate(residu= runif(nrow(.), min=-4.5, max=5)) %>% 
    mutate(residu_classe = case_when(residu < -4~"< -4 (p<0.001)",(residu >= -4 & residu < -2)~"[-4;-2[ (p<0.05)",(residu >= -2 & residu < 2)~"[-2;2[ non significatif",(residu >= 2 & residu < 4)~"[2;4[ (p<0.05)",residu >= 4~">= 4 (p<0.001)")) %>% 
    mutate(residu_color = case_when(residu < -4~"#D04864",(residu >= -4 & residu < -2)~"#E495A5",(residu >= -2 & residu < 2)~"#CCCCCC",(residu >= 2 & residu < 4)~"#9DA8E2",residu >= 4~"#4A6FE3")) 


ggplot(data_ex) + 
    geom_mosaic(aes(weight= count, x=product(clarity, cut)), fill = data_ex$residu_color, na.rm=T)+ 
    scale_y_productlist() + 
    theme_classic() + 
    theme(axis.ticks=element_blank(), axis.line=element_blank())+ 
    labs(x = "cut",y="clarity") 

enter image description here

लेकिन मैं साजिश के दाईं ओर (नीचे) इस कथा को जोड़ना चाहते हैं, लेकिन मैं नहीं जानता कि कैसे मैं यह कर सकता है, क्योंकि भरण क्षेत्र के बाहर एईएस तो scale_fill_manual काम नहीं करता है ...

enter image description here

उत्तर

5

का उपयोग समूह सौंदर्य:

p1 <- ggplot(data_ex) + 
    geom_bar(aes(x = cut, y = count, group = clarity, fill = var_fill), 
      stat = "identity", position = "fill", color="black") + ggtitle("var fill") 

p2 <- ggplot(data_ex) + 
    geom_bar(aes(x = cut, y = count, fill = clarity), stat = "identity", position = "fill", color = "black")+ 
    ggtitle("clarity") 

library(cowplot) 
cowplot::plot_grid(p1, p2) 

enter image description here

संपादित करें: ggmosaic

library(ggmosaic) 

p3 <- ggplot(data_ex) + 
    geom_mosaic(aes(weight= count, x=product(clarity, cut), fill=var_fill), na.rm=T)+ 
    scale_x_productlist() 

p4 <- ggplot(data_ex) + 
    geom_mosaic(aes(weight= count, x=product(clarity, cut), fill=clarity,), na.rm=T)+ 
    scale_x_productlist() 

cowplot::plot_grid(p3, p4) 

enter image description here

साथ, के लिए समूह बिल्कुल जरूरत नहीं है ggmosaic मुझे लगता है दोनों भूखंडोंउलट कर रहे हैं geom_bar केसंस्करण।

EDIT3:
को परिभाषित करने जैसे समस्याओं aes फिक्स बाहर भरने:
1) एक्स अक्ष पठनीयता
2) प्रत्येक आयत

data_ex %>% 
mutate(color = ifelse(var_fill == "A", "#0073C2FF", ifelse(var_fill == "B", "#EFC000FF", "#868686FF"))) -> try2 

ggplot(try2) + 
    geom_mosaic(aes(weight= count, x=product(clarity, cut)), fill = try2$color, na.rm=T)+ 
    scale_x_productlist() 

enter image description here की सीमाओं में बहुत छोटे रंग की लाइनों को हटा

वाई अक्ष लेबल जोड़ने के लिए किसी को थोड़ा झगड़ा चाहिए। यहाँ एक दृष्टिकोण है:

ggplot(try2) + 
    geom_mosaic(aes(weight= count, x=product(clarity, cut)), fill = try2$color, na.rm=T)+ 
    scale_x_productlist()+ 
    scale_y_continuous(sec.axis = dup_axis(labels = unique(try2$clarity), 
             breaks = try2 %>% 
              filter(cut == "Ideal") %>% 
              mutate(count2 = cumsum(count/sum(count)), 
                lag = lag(count2)) %>% 
              replace(is.na(.), 0) %>% 
              rowwise() %>% 
              mutate(post = sum(count2, lag)/2)%>% 
              select(post) %>% 
              unlist())) 

enter image description here

EDIT4: कथा जोड़ने दो तरह से पूरा किया जा सकता।

1 - कथा उत्पन्न करने के लिए एक नकली परत जोड़कर - लेकिन यह एक समस्या एक्स अक्ष लेबल के साथ (वे कटौती का एक संयोजन कर रहे हैं और भरने) इसलिए मैं मैनुअल ब्रेक परिभाषित पैदा करता है और लेबल से

data_ex ओपी

ggplot(data_ex) + 
    geom_mosaic(aes(weight= count, x=product(clarity, cut), fill = residu_classe), alpha=0, na.rm=T)+ 
    geom_mosaic(aes(weight= count, x=product(clarity, cut)), fill = data_ex$residu_color, na.rm=T)+ 
    scale_y_productlist()+ 
    theme_classic() + 
    theme(axis.ticks=element_blank(), axis.line=element_blank())+ 
    labs(x = "cut",y="clarity")+ 
    scale_fill_manual(values = unique(data_ex$residu_color), breaks = unique(data_ex$residu_classe))+ 
    guides(fill = guide_legend(override.aes = list(alpha = 1)))+ 
    scale_x_productlist(breaks = data_ex %>% 
         group_by(cut) %>% 
         summarise(sumer = sum(count)) %>% 
         mutate(sumer = cumsum(sumer/sum(sumer)), 
           lag = lag(sumer)) %>% 
         replace(is.na(.), 0) %>% 
         rowwise() %>% 
         mutate(post = sum(sumer, lag)/2)%>% 
         select(post) %>% 
         unlist(), labels = unique(data_ex$cut)) 

enter image description here

2 EDIT2 - एक भूखंड से कथा निकालने और अन्य

में जोड़कर

कथा के लिए नकली साजिश बनाने:

gg_pl <- ggplot(data_ex) + 
    geom_mosaic(aes(weight= count, x=product(clarity, cut), fill = residu_classe), alpha=1, na.rm=T)+ 
    scale_fill_manual(values = unique(data_ex$residu_color), breaks = unique(data_ex$residu_classe)) 

बनाने के सही साजिश

z = ggplot(data_ex) + 
    geom_mosaic(aes(weight= count, x=product(clarity, cut)), fill = data_ex$residu_color, na.rm=T)+ 
    scale_y_productlist()+ 
    theme_classic() + 
    theme(axis.ticks=element_blank(), axis.line=element_blank())+ 
    labs(x = "cut",y="clarity") 


a.gplot <- ggplotGrob(gg_pl) 
tab <- gtable::gtable_filter(a.gplot, 'guide-box', fixed=TRUE) 
gridExtra::grid.arrange(z, tab, nrow = 1, widths = c(4,1)) 

enter image description here

1

आप लगभग वहां हैं! आप बस एईएस में ऑर्डर निर्दिष्ट करते हैं, इसलिए यह कुछ ऐसा होगा:

ggplot(data_ex) + 
    geom_bar(aes(x = cut, y = count, fill=var_fill, order=clarity),stat = "identity", position = "fill", color="black") 

और आप जाने के लिए अच्छे हैं।

+0

धन्यवाद, लेकिन मुझे लगता है कि आदेश या मूल्यह्रास हुआ हो जाएगा है? यह github – antuki

+0

पर ggplot2 के वास्तविक संस्करण के साथ मेरे साथ काम नहीं करता है, dev_tools संस्करण में यह हो सकता है लेकिन अगर आपको इसकी आवश्यकता हो तो आप वापस संस्करण भी जा सकते हैं (Google आपके संस्करण को वापस संस्करणों पर जाने पर है) – ike

संबंधित मुद्दे