渐变不同颜色的树形图上的子组ggplot2 R [英] Different colors with gradient for subgroups on a treemap ggplot2 R

查看:117
本文介绍了渐变不同颜色的树形图上的子组ggplot2 R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树状图(如下所示).我唯一要更改的是将子组的颜色(图中的YEAR)更改为不同的颜色,而不是全部为蓝色.这有可能吗?

I have a treemap plot (shown below). The only change that I want to have is to change the color of subgroup (YEAR in the plot) to different colors, not all blue. Is this possible at all?

示例数据框

PL <- c(rep("PL1", 4), repl("PL2", 4), rep("PL3", 4), rep("PL4", 4))
CNT <- sample(seq(1:50), 16)
YEAR <- rep(c("2015", "2016", "2017", "2018"), 4)

df <- data.frame(PL, YEAR, CNT)

情节

PL <- c(rep("PL1", 4), repl("PL2", 4), rep("PL3", 4), rep("PL4", 4))
    CNT <- sample(seq(1:50), 16)
    YEAR <- rep(c("2015", "2016", "2017", "2018"), 4)

    df <- data.frame(PL, YEAR, CNT)

    # plot
library(ggplot2)
library(treemapify)
treeMapPlot <- ggplot(df, aes(area = CNT,
                              fill = CNT,
                              label=PL, 
                              subgroup=YEAR)) +
      geom_treemap() +
      geom_treemap_subgroup_border(colour = "white") +
      geom_treemap_text(fontface = "italic",
                        colour = "white",
                        place = "centre",
                        grow = F,
                        reflow = T) +
      geom_treemap_subgroup_text(place = "centre",
                                 grow = T,
                                 alpha = 0.5,
                                 colour = "#FAFAFA",
                                 min.size = 0)

treeMapPlot

如果在aes中更改fill,则可以得到此值,但会丢失渐变.我需要保留这些颜色,但要显示具有渐变颜色的图块,这意味着较小的CNT较亮,较大的CNT较暗

If I change the fill in aes I can get this, but I lose the gradient. I need to keep these colors, but show the tiles with gradient color, meaning small CNT lighter, larger CNT darker

treeMapPlot <- ggplot(df, aes(area = CNT,
                              fill = YEAR,
                              label = PL, 
                              subgroup = YEAR))

推荐答案

这不是最漂亮的解决方案,但是将计数映射到alpha会模拟每种颜色的明暗梯度.在geom_treemap内添加aes(alpha = CNT),并根据需要缩放Alpha.

It's not the most beautiful solution, but mapping count to alpha simulates a light-to-dark gradient for each color. Add aes(alpha = CNT) inside geom_treemap, and scale alpha however you want.

library(ggplot2)
library(treemapify)

PL <- c(rep("PL1",4),rep("PL2",4),rep("PL3",4),rep("PL4",4))
CNT <- sample(seq(1:50),16)
YEAR <- rep(c("2015","2016","2017","2018"),4)

df <- data.frame(PL,YEAR,CNT)

ggplot(df, aes(area = CNT, fill = YEAR, label=PL, subgroup=YEAR)) +

# change this line
    geom_treemap(aes(alpha = CNT)) +
    geom_treemap_subgroup_border(colour="white") +
    geom_treemap_text(fontface = "italic",
                                        colour = "white",
                                        place = "centre",
                                        grow = F,
                                        reflow=T) +
    geom_treemap_subgroup_text(place = "centre",
                                                         grow = T,
                                                         alpha = 0.5,
                                                         colour = "#FAFAFA",
                                                         min.size = 0) +
    scale_alpha_continuous(range = c(0.2, 1))

reprex软件包(v0.2.0)创建于2018-05-03.

Created on 2018-05-03 by the reprex package (v0.2.0).

编辑以添加:基于这篇文章通过将Alpha缩放图层放置在具有较深填充的图层之上来破解伪渐变.在这里,我使用了两个geom_treemap,一个用于fill = "black",另一个用于alpha缩放.仍然有一些不足之处.

Edit to add: Based on this post on hacking faux-gradients by putting an alpha-scaled layer on top of a layer with a darker fill. Here I've used two geom_treemaps, one with fill = "black", and one with the alpha scaling. Still leaves something to be desired.

ggplot(df, aes(area = CNT, fill = YEAR, label=PL, subgroup=YEAR)) +
    geom_treemap(fill = "black") +
    geom_treemap(aes(alpha = CNT)) +
    geom_treemap_subgroup_border(colour="white") +
    geom_treemap_text(fontface = "italic",
                                        colour = "white",
                                        place = "centre",
                                        grow = F,
                                        reflow=T) +
    geom_treemap_subgroup_text(place = "centre",
                                                         grow = T,
                                                         alpha = 0.5,
                                                         colour = "#FAFAFA",
                                                         min.size = 0) +
    scale_alpha_continuous(range = c(0.4, 1))

reprex软件包(v0.2.0)创建于2018-05-03.

Created on 2018-05-03 by the reprex package (v0.2.0).

这篇关于渐变不同颜色的树形图上的子组ggplot2 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆