D3partitionR icon indicating copy to clipboard operation
D3partitionR copied to clipboard

Limiting colors to a subset of steps/levels

Open jxg852 opened this issue 7 years ago • 10 comments

Is there a way to limit the number of colors to only a specified subset of the steps/levels in the data? The lowest level in my dataset has many unique values, and assigning colors to them all makes the chart look very messy. image

When I assign a categorical variable to the color parameter to add_data, everything turns into one color. image

jxg852 avatar Oct 19 '17 15:10 jxg852

Is there a way to limit the number of colors to only a specified subset of the steps/levels in the data?

Currently, there is not. I hope to be able to add it for the next version.

When I assign a categorical variable to the color parameter to add_data, everything turns into one color.

Did you specify this extra variable in add_nodes_data ? If you want to use a specific color you can also use set_discrete_color_scale and use a named list with the name of your leaves.

AntoineGuillot2 avatar Oct 19 '17 19:10 AntoineGuillot2

Did you specify this extra variable in add_nodes_data ? If you want to use a specific color you can also use set_discrete_color_scale and use a named list with the name of your leaves.

The variable is one of the steps in add_data. For example, using the Titanic dataset, I would like to assign colors based on the variable "Sex"- blue for males and red for females

D3partitionR() %>% add_data(data_plot, count = 'N', steps=c('Sex','Embarked','Pclass','Survived'), tooltip=c('name','N'), label='name', color='Sex') %>% set_chart_type('treemap') %>% set_discrete_color_scale(list("Sex male"="blue","Sex female"="green")) %>% plot() image

Sorry if this is a dumb question.

jxg852 avatar Oct 19 '17 20:10 jxg852

Actually, you can only use a numeric variable as color when you use a variable from the dataframe, since we are doing some aggregations, we cannot deal with categorical variable (not sure if it is clear). To have categorical coloring you have two options:

  • Adding a color variable with add_nodes_data, but this is currently broken ...
  • Adding colors with set_discrete_color_scale, for instance: set_discrete_color_scale(c("#ff0000","#ff0000")%>%names<-(c('Sex male','Sex female'))) You need to download it from github as the option is broken in the current CRAN version.

AntoineGuillot2 avatar Oct 19 '17 21:10 AntoineGuillot2

option 2 is giving me an error (I downloaded the github version of D3partitionR) image

jxg852 avatar Oct 19 '17 21:10 jxg852

This should work (a quote disappeared in GitHub):

## Loading packages
library("data.table")
library("D3partitionR")
library(titanic)
require(magrittr)
require(RColorBrewer)
## Reading data
titanic_data = data.table(titanic::titanic_train)

##Agregating data to have unique sequence for the 4 variables
var_names=c('Sex','Embarked','Pclass','Survived')
data_plot=titanic_data[,.N,by=var_names]
data_plot[,(var_names):=lapply(var_names,function(x){data_plot[[x]]=paste0(x,' ',data_plot[[x]])
})]


color_list=c("#ffff00","#0000ff","#ff0000")
names(color_list)<-c('Sex male','Sex female','Survived 0')

D3partitionR() %>%
  D3partitionR::add_data(data_plot,count = 'N',tooltip=c('name'),steps=c('Sex','Embarked','Pclass','Survived')) %>%
  set_chart_type('treemap') %>%
  set_legend_parameters(zoom_subset=T)%>%
  set_discrete_color_scale(color_list)%>%
  plot()

AntoineGuillot2 avatar Oct 20 '17 20:10 AntoineGuillot2

Really appreciate your help, but I think I must have confused you by splitting up my initial question. I was wondering if there's a way to assign colors only based on Sex, i.e. blue for male (and all the children nodes) and green for female (and all the children nodes). I'm guessing that might not be possible based on your first response.

jxg852 avatar Oct 22 '17 19:10 jxg852

It's possible but tedious, you have to manually add the color for each of the child nodes using the function set_discrete_color_scale.

AntoineGuillot2 avatar Oct 23 '17 19:10 AntoineGuillot2

Actually, you can only use a numeric variable as color when you use a variable from the dataframe, since we are doing some aggregations, we cannot deal with categorical variable (not sure if it is clear). To have categorical coloring you have two options:

  • Adding a color variable with add_nodes_data, but this is currently broken ...
  • Adding colors with set_discrete_color_scale, for instance: set_discrete_color_scale(c("#ff0000","#ff0000")%>%names<-(c('Sex male','Sex female'))) You need to download it from github as the option is broken in the current CRAN version.

Has the CRAN version been amended to account for this? I have been trying to set discrete colors with no success.

philbo22 avatar Aug 28 '19 15:08 philbo22

No, currently it is only on the github version.

AntoineGuillot2 avatar Sep 21 '19 11:09 AntoineGuillot2

Hi,

I have the same issues with the Github version. set_continous_color_scale doesn't work and set_discrete_color_scale doesn't change anything.

My problem is that I have too many categories set in black in my circle_treemap and I don't know how to change that.

solavaud avatar Jan 14 '20 22:01 solavaud