2017-10-25 17 views
6

Tôi có câu hỏi liên quan đến trường điền trong geom_bar của gói ggplot2.đặt hàng và điền vào với 2 biến khác nhau geom_bar ggplot2 R

Tôi muốn điền vào geom_bar của tôi với một biến (trong ví dụ tiếp theo biến được gọi là var_fill) nhưng đặt hàng geom_plot với biến khác (được gọi là clarity trong ví dụ).

Tôi có thể làm như thế nào?

Cảm ơn bạn rất nhiều!

Ví dụ:

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 

Tôi muốn lệnh này trong những hộp [rõ ràng]:

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

enter image description here

với điền này (màu sắc) của hộp [var_fill] :

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

enter image description here

EDIT1: Câu trả lời được tìm thấy bởi 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: Bây giờ tôi đã cố gắng để làm điều này với phần mở rộng ggmosaic với sự giúp đỡ của missuse

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

Nhưng tôi muốn thêm huyền thoại này (dưới đây) ở phía bên phải của cốt truyện nhưng tôi không biết làm thế nào tôi có thể làm điều đó bởi vì lĩnh vực điền là aes bên ngoài để scale_fill_manual không hoạt động ...

enter image description here

Trả lời

5

Sử dụng nhóm thẩm mỹ:

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

EDIT: với 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

Dường như với tôi cho ggmosaic nhóm là không cần thiết ở tất cả, cả hai các ô được đảo ngược phiên bản của geom_bar.

EDIT3:
định điền ngoài aes sửa chữa các vấn đề như:
1) trục X dễ đọc
2) loại bỏ các dòng màu rất nhỏ trong lãnh thổ của mỗi hình chữ nhật

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

Để thêm nhãn trục y, bạn cần một chút tranh cãi. Dưới đây là một cách tiếp cận:

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: thêm các huyền thoại có thể được thực hiện theo hai cách.

1 - bằng cách thêm một lớp giả để tạo ra huyền thoại - tuy nhiên điều này tạo ra một vấn đề với các nhãn trục x (họ là một sự kết hợp của cắt và điền vào) do đó tôi xác định các vi phạm nhãn hiệu và nhãn

data_ex từ OP EDIT2

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 - bằng cách chiết xuất các huyền thoại từ một cốt truyện và thêm nó vào người kia

library(gtable)    
library(gridExtra) 

làm cho cốt truyện giả cho huyền thoại:

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

làm cho cốt truyện đúng

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

bạn sắp hoàn tất! bạn chỉ cần chỉ định thứ tự trong aes, do đó, nó sẽ là một cái gì đó như:

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

và bạn tốt để đi.

+0

Cảm ơn bạn nhưng tôi nghĩ để được hoặc sẽ được khấu hao? nó không làm việc với tôi với phiên bản thực tế của ggplot2 trên github – antuki

+0

chắc chắn, phiên bản dev_tools có thể có nhưng bạn cũng có thể quay trở lại phiên bản nếu bạn chỉ cần điều này (google là bạn của bạn trên phiên bản trở lại) – ike

Các vấn đề liên quan