本文介绍了如何在ggplot中为独立图层缩放颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个数据集,记录了三座建筑物的能源使用情况。我有一个可以从钻石组中模拟出来的融化的数据框: data 基本上,我从三个不同的建筑物(7个颜色因子)获得每个月的加热('深度')和冷却('桌子')数据。我想在每个月的条形图('cut')中并排绘制三个建筑物(7个颜色因素)。 我希望表示冷却('表')或加热('深度')的条形根据建筑物(颜色因子)改变其阴影,按月份('cut')分组。这是钻石数据可视化的一种不好的方式,但对于建筑物来说,它们应该很好地工作,因为它们的加热和冷却月份通常不会重叠。到目前为止,我有: p aes(color,value,group = cut ))p position ='dodge', aes(fill = variable)) print(p) 我试着玩scale_fill_manual,但想不出一个可行的策略: $ b ('#0000FF','#0033FF','#0066FF','#FF0000','#FF3300','#FF3300' #FF6600') p group = data $ variable) 解决方案有了一些诡计,这是可能的。派生一个基于钻石的数据集是非常好的,但我想用一个更小的数据集 set.seed(1234 ) data expand.grid(month = month.abb, building = c(Building A,Building B,Building C), hc = c(Heating,Cooling)) data $ value 您希望您的填充颜色基于变量( hc )和建筑物( ggplot(data,aes(building ,value,group = month))+ geom_bar(stat ='identity', position ='dodge', aes(fill = interaction(building,hc))) 我们可以选择代表不同颜色的颜色,使其更像您所想。我使用了 RColorBrewer 调色板的蓝调和红色中间。 颜色#library(RColorBrewer)#colors 并使用 scale_fill_manual 来分配这些颜色。 ggplot(data,aes(building,value,group =月))+ geom_bar(stat ='identity', position ='dodge', aes(fill = interaction(building,hc)))+ scale_fill_manual =颜色) 真正的诀窍是让图例变得更加复杂。我只列出了2个关卡(中间建筑的颜色),并给他们不同的名字(和图例中的不同标题)。 ggplot(data,aes(building,value,group = month))+ geom_bar(stat ='identity', position ='dodge', aes(fill = building(building,hc)))+ scale_fill_manual(Heating / cooling, values = colors, breaks = c(Building B.Heating,Building B.Cooling ), labels = c(Heating,Cooling)) I have a data set that documents the energy use of three buildings. I have a melted data frame that can be mimicked from the diamonds set:data <- melt(diamonds[,c('depth','table','cut','color')],id=c('cut','color'))Essentially, I have heating ('depth') and cooling ('table') data for each month('cut') from three different buildings (7 'color' factors). I would like to plot the three buildings (7 'color' factors) side by side in a bar plot for each month ('cut'). I want the bars representing either cooling ('table') or heating ('depth') to vary their shade based on the building ('color' factor) while remaining grouped by month ('cut'). This is a bad way to visualize the diamonds data, but should work well for buildings as their heating and cooling months typically don't overlap. So far I have:p <- ggplot(data, aes(color,value,group=cut))p <- p + geom_bar(stat = 'identity', position = 'dodge', aes(fill = variable))print(p)I tried playing with scale_fill_manual, but couldn't think of a strategy that works:colours <- c('#0000FF', '#0033FF', '#0066FF', '#FF0000', '#FF3300', '#FF6600')p <- p + scale_fill_manual(values = colours, group = data$variable) 解决方案 With some trickery, it is possible. Deriving a data set based on diamonds was very good, but I want to work with a smaller data set set.seed(1234)data <-expand.grid(month = month.abb, building = c("Building A", "Building B", "Building C"), hc = c("Heating", "Cooling"))data$value <- rnorm(nrow(data), 60, 10)You want your fill colour to be based both on the variable (hc) and the building (building), so set it to that interaction.ggplot(data, aes(building,value,group=month)) + geom_bar(stat = 'identity', position = 'dodge', aes(fill = interaction(building, hc)))We can pick colors that represent different near shades to make these more like what you want. I used the middle of the "Blues" and "Reds" of the RColorBrewer palettes. colours <- c("#FC9272", "#FB6A4A", "#EF3B2C", "#9ECAE1", "#6BAED6", "#4292C6")# library("RColorBrewer")# colours <- c(brewer.pal(9,"Reds")[4:6], brewer.pal(9,"Blues")[4:6])and used scale_fill_manual to assign these colours.ggplot(data, aes(building,value,group=month)) + geom_bar(stat = 'identity', position = 'dodge', aes(fill = interaction(building, hc))) + scale_fill_manual(values=colours)The real trickery is in making the legend less convoluted. I just list 2 of the levels (the middle building in terms of color) and give them different names (and a different title for the legend).ggplot(data, aes(building,value,group=month)) + geom_bar(stat = 'identity', position = 'dodge', aes(fill = interaction(building, hc))) + scale_fill_manual("Heating/cooling", values=colours, breaks=c("Building B.Heating", "Building B.Cooling"), labels=c("Heating", "Cooling")) 这篇关于如何在ggplot中为独立图层缩放颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-16 09:09