本文介绍了使用多个调色板在条形图中创建自定义图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 通过 问题:如何创建一个自定义图例,将3个调色板打印为4x3矩阵,至少包含行名称(圆柱体数量),如下所示: purples%>%full_join(reds)%>%full_join(blues)%>%spread(manufacturer,colr) #A tibble:4 x 4 cyl audi ford< NA> *< int> < CHR> < CHR> < CHR> 1 4#FEE5D9#EFF3FF#F2F0F7 2 5#FCAE91#BDD7E7#CBC9E2 3 6#FB6A4A#6BAED6#9E9AC8 4 8#CB181D#2171B5#6A51A3 但随后颜色代码被漂亮的图例颜色框取代。有点相关的是 此问答 ,但随后以网格中的传说,而不是 解决方案完整的工作解决方案: library(dplyr) library(ggplot2) library(gridExtra) library(RColorBrewer) library(tidyr) cyl ncat dd >% group_by(manufacturer,cyl)%>%汇总(n = n())%>% ungroup() #创建调色板紫色< - tibble(cyl,colr = brewer.pal(ncat,Purples)) reds blues< - tibble(manufacturer =ford,cyl,colr = brewer.pal(ncat,Blues)) #将数据合并到数据 dd_p< - dd%>%过滤器(!(制造商%在%c(aud (制造商==audi)%>%left_join(红色)$ b $($)$%$ left_join(紫色) dd_r< - dd%> b dd_b< - dd%>%过滤器(制造商==ford)%>%left_join(蓝色) mm group_by (mcyl = weighted.mean(cyl,n))%>% arrange(mcyl)%>% ungroup() gg_dd % left_join(mm) main_plot< - gg_dd%>% ggplot (mapping = aes(x = reorder(manufacturer,mcyl),y = n,fill = colr))+ geom_bar(stat =identity,position =fill)+ coord_flip() + scale_fill_identity() combined_legend< - 紫色%>% full_join(红色)%>% full_join(蓝色)%>% mutate(制造商= ifelse(is.na(制造商),generic,制造商))%>%ggplot(mapping = aes(y = manufacturer,x = factor l),fill = colr))+ geom_tile()+ scale_fill_identity()+ coord_fixed() $ b grid.arrange(main_plot,combined_legend,heights = c(10,2),ncol = 1) From this other Q&A, I have managed to create a bar chart with multiple color palettesQuestion: how to create a custom legend that prints the 3 color palettes as 4x3 matrix, with at least the row names (number of cylinders), like this:library(tidyr) purples %>% full_join(reds) %>% full_join(blues) %>% spread(manufacturer, colr)# A tibble: 4 x 4 cyl audi ford <NA>* <int> <chr> <chr> <chr>1 4 #FEE5D9 #EFF3FF #F2F0F72 5 #FCAE91 #BDD7E7 #CBC9E23 6 #FB6A4A #6BAED6 #9E9AC84 8 #CB181D #2171B5 #6A51A3but then with the color codes replaced by nice legend color boxes. Somewhat related is this Q&A, but then with the legends in a grid, instead of above each other. 解决方案 Full working solution:library(dplyr)library(ggplot2)library(gridExtra)library(RColorBrewer)library(tidyr)cyl <- sort(unique(mpg$cyl))ncat <- length(cyl) # 4 types of cylindersdd <- mpg %>% group_by(manufacturer, cyl) %>% summarise(n = n()) %>% ungroup()# create palettespurples <- tibble(cyl, colr = brewer.pal(ncat, "Purples"))reds <- tibble(manufacturer = "audi", cyl, colr = brewer.pal(ncat, "Reds"))blues <- tibble(manufacturer = "ford", cyl, colr = brewer.pal(ncat, "Blues"))# merge them with the datadd_p <- dd %>% filter(!(manufacturer %in% c("audi", "ford"))) %>% left_join(purples)dd_r <- dd %>% filter(manufacturer == "audi") %>% left_join(reds)dd_b <- dd %>% filter(manufacturer == "ford") %>% left_join(blues)mm <- dd %>% group_by(manufacturer) %>% summarise(mcyl = weighted.mean(cyl, n)) %>% arrange(mcyl) %>% ungroup()gg_dd <- rbind(dd_p, dd_r, dd_b) %>% left_join(mm)main_plot <- gg_dd %>% ggplot(mapping = aes(x = reorder(manufacturer, mcyl), y = n, fill = colr)) + geom_bar(stat = "identity", position = "fill") + coord_flip() + scale_fill_identity()combined_legend<- purples %>% full_join(reds) %>% full_join(blues) %>% mutate(manufacturer = ifelse(is.na(manufacturer), "generic", manufacturer)) %>% ggplot(mapping = aes(y = manufacturer, x = factor(cyl), fill = colr)) + geom_tile() + scale_fill_identity() + coord_fixed()grid.arrange(main_plot, combined_legend, heights=c(10,2), ncol=1) 这篇关于使用多个调色板在条形图中创建自定义图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-13 20:16