color doesn't match in scatter plot - r

I'm trying to create a scatter plot using highchart, but the color in the graph and in the labels are different. The color should be defined by the column named "Check_color" beacuse i'm using into Rshiny app, and sometimes i don't have all the options in the graph and the color should be align with the column "Rank". I mean, if only "Yes" is selected all points should be green, if only "No", should be red, etc...
Ando Righ now in the graph "Yes" in green, but in the labels are blue, and the same for the rest. How can i math the colors in the labels? Thanks !!
This is my current code
data = data.table(
CJ(x = seq(as.Date("2019-01-01"), as.Date("2019-01-10"), by = "day"),
group = seq(1,20))
)
data[, value := round(runif(n=200, 0,5),4)]
data = data.table(data %>% mutate(cat=cut(value, breaks=quantile(data[value!=0]$value, seq(0,1,0.1)), labels=seq(1,10))))
colf = colorRampPalette(colors = c("red","yellow", "green"))
cols = colf(10)
data[, color := as.factor(cols[cat])]
data$x = datetime_to_timestamp(data$x)
data = data.table(data %>% group_by(x) %>% mutate(y = (order(order(value))-sum(value<0,na.rm=T))))
data[, name := group]
data$x <- runif(200, 100, 1000) / 10
data$y <- runif(200, 100, 1000) / 10
data$gp_ <- round(runif(200,1,5), digits = 0)
data$Index <- seq(1,200,1)
data$Rank <- ifelse(data$gp_ == 1 , "Yes", ifelse(data$gp_ == 2 , "No",ifelse(data$gp_ == 3 , "Minor Deficiency",ifelse(data$gp_ == 4 , "Major Deficiency",ifelse(data$gp_ == 5 , "Not Applicable","")))))
data <- data[1:71,]
data$Check_color <- ifelse(data$Rank == "Yes" , "#14E632", ifelse(data$Rank == "No" , "#FA0101",ifelse(data$Rank == "Minor Deficiency" , "#FF99FF",ifelse(data$Rank == "Major Deficiency" , "#FF9933",ifelse(data$Rank == "Not Applicable" , "#CACECE","")))))
hc_1 <- data %>%
hchart('scatter', hcaes(x = x, y = y , group = Rank, color = Check_color )) %>%
hc_title(text = "<b>PUBLIC COMPANY D&O COVERAGE HEAT MAP </b>") %>%
hc_chart(
borderColor = "#999999",
borderRadius = 20,
borderWidth = 3) %>%
hc_tooltip(pointFormat = 'Provision ID: {point.Index} <br/>
Provision: {point.Check_color} <br/>
Severity: {point.y:.2f} <br/>
Frequency: {point.x:.2f} ')
hc_1

The problem with the two color scales came from using group = Rank and color = Check_color in the hcaes. Remove color = Check_color, and you get matching color. However, I do not know how to specify the color from there... I tried hc_color() which didn't work. Maybe someone else can complete this answer!
hc_1 <- data %>%
hchart('scatter', hcaes(x = x, y = y , group = Rank )) %>%
hc_title(text = "<b>PUBLIC COMPANY D&O COVERAGE HEAT MAP </b>") %>%
hc_chart(
borderColor = "#999999",
borderRadius = 20,
borderWidth = 3) %>%
hc_tooltip(pointFormat = 'Provision ID: {point.Index} <br/>
Provision: {point.Check_color} <br/>
Severity: {point.y:.2f} <br/>
Frequency: {point.x:.2f} ')
hc_1

Related

Plotly animated map not showing countries with NA values

I posted this in the plotly community forum but got absolutely no activity! Hope you can help here:
I have map time-series data, some countries don’t have data and plotly does not plot them at all. I can have them outlined and they look different but it appears nowhere that the data is missing there (i.e. I want a legend entry). How can I achieve this? Here is a reprex:
library(plotly)
library(dplyr)
data = read.csv('https://github.com/lc5415/COVID19/raw/master/data.csv')
l <- list(color = toRGB("grey"), width = 0.5)
g <- list(
scope = 'world',
countrycolor = toRGB('grey'),
showframe = T,
showcoastlines = TRUE,
projection = list(type = 'natural earth')
)
map.time = data %>%
plot_geo() %>%
add_trace(z = ~Confirmed, color = ~Confirmed, frame = ~Date, colors = 'Blues',
text = ~Country, locations = ~Alpha.3.code, marker = list(line = l)) %>%
colorbar(title = 'Confirmed') %>%
layout(
title = 'Number of confirmed cases over time',
geo = g
) %>%
animation_opts(redraw = F) %>%
animation_slider(
currentvalue = list(
prefix = paste0("Days from ",
format(StartDate, "%B %dnd"),": "))) %>%
plotly_build()
map.time
Note that the countries with missing data (e.g. Russia) have as many data points as all other countries, the issue is not that they do not appear in the dtaframe passed to plotly.
The obvious way to handle this is to create a separate labels column for the tooltip that reads "No data" for NA values (with the actual value otherwise), then make your actual NA values 0. This will give a uniform appearance to all the countries but correctly tells you when a country has no data.
map.time = data %>%
mutate_if(is.numeric, function(x) {x[is.na(x)] <- -1; x}) %>%
plot_geo() %>%
add_trace(z = ~Confirmed, color = ~Confirmed, frame = ~Date, colors = 'Blues',
text = ~Country, locations = ~Alpha.3.code,
marker = list(line = l)) %>%
colorbar(title = 'Confirmed') %>%
layout(
title = 'Number of confirmed cases over time',
geo = g
) %>%
animation_opts(redraw = F) %>%
animation_slider(
currentvalue = list(
prefix = paste0("Days from ",
format(StartDate, "%B %dnd"),": "))) %>%
plotly_build()
Which gives:

How to create a highcharts dot plot graph? - R Highcharter

How can I create a highcharts graph like this using the R highcharter package?
It is a simple count of sectors (instances) above or bellow 0 and coloured to reflect the value.
This may sometime be termed a dot plot (https://ggplot2.tidyverse.org/reference/geom_dotplot.html)?
image from (https://graphics.wsj.com/job-market-tracker/)
Some sample data:
data = data.table(
CJ(date = seq(as.IDate("2019-01-01"), as.IDate("2019-01-10"), by = "day"),
group = seq(1,20))
)
data[, value := runif(n=200, -5,5)]
This is as far as I got:
library(highcharter)
library(data.table)
data = data.table(
CJ(date = seq(as.Date("2019-01-01"), as.Date("2019-02-10"), by = "day"),
group = seq(1,20))
)
# generate random value
data[, value := round(runif(n=dim(data)[1], -5,5),4)]
# categorize it from 1 to 10
data[, cat:=cut(value, breaks=quantile(data[value!=0]$value, seq(0,1,0.1)), labels=seq(1,10))]
# assign colour based on value
colf = colorRampPalette(colors = c("red","yellow", "green"))
cols = colf(10)
data[, color := as.factor(cols[cat])]
# generate x and y
data[, x := datetime_to_timestamp(date)]
data[, y := order(order(value))-sum(value<0), date]
data[, name := group]
highchart() %>%
hc_chart(type = "scatter") %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%d of %b')) %>%
hc_tooltip(pointFormat = "Performance = <b>{point.value}</b> <br> Group = <b>{point.name}</b>") %>%
hc_add_theme(hc_theme_flat(chart = list(backgroundColor = "#FFF"))) %>%
hc_add_series(data, groupPadding=0)
It also works with more points:

Use RGB Customers Colors by Group in R Plotly

I have several series which I would like to animate with plotly R. After following the example here (https://plot.ly/r/cumulative-animations/), I have the animation working. I figured out how to change the colors for the groups, however, I need specific colors for the groups (RGB custom colors).
I have two questions:
How do I assign RGB colors to groups in R Plotly...what am I missing here?
Is there an easier way to do this? I have several more "cities" than just two, and want to be able to dynamically assign the specific color. I was able to pull the colors in as a column in the data frame, and would like to be able to assign them that way...got it working for the regular colors, but need to get it for the RGB...
library(plotly)
# Helper function to create frames
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
# Pull in data and also create color columns
d <-
txhousing %>%
filter(year > 2005, city %in% c("Abilene", "Bay Area")) %>%
accumulate_by(~date) %>%
mutate(regular_color = if_else(city == "Abilene", 'red', 'black'),
RGB_color = if_else(city == "Abilene", 'rgb(229,18,18)', 'rgb(13,9,9)'))
# color vectors
reg_color_vector <-
d %>%
arrange(city) %>%
select(regular_color) %>%
distinct() %>%
pull()
RGB_color_vector <-
d %>%
arrange(city) %>%
select(RGB_color) %>%
distinct() %>%
pull()
p <- d %>%
plot_ly(
x = ~date,
y = ~median,
split = ~city,
frame = ~frame,
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F),
color = ~city,
# colors = c('red', 'black')
colors = c('rgb(229, 18, 18)', 'rgb(13, 9, 9)')
# colors = reg_color_vector
# colors = RGB_color_vector
) %>%
layout(
xaxis = list(
title = "Date",
zeroline = F
),
yaxis = list(
title = "Median",
zeroline = F
)
) %>%
animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
) %>%
animation_slider(
hide = T
) %>%
animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
p
rgb() is a function which outputs a hexadecimal value of the color you want. That is what you need to store. Remove the ' and it should be fine. And you need to add maxColorValue = 255 to the rgb() function.
d <-
txhousing %>%
filter(year > 2005, city %in% c("Abilene", "Bay Area")) %>%
accumulate_by(~date) %>%
mutate(regular_color = if_else(city == "Abilene", 'red', 'black'),
RGB_color = if_else(city == "Abilene",
rgb(229, 18, 18, maxColorValue = 255),
rgb(13, 9, 9, maxColorValue = 255)))
You can use in plot_ly than the RGB_color_vector to define the colors.
plot_ly(
x = ~date,
y = ~median,
split = ~city,
frame = ~frame,
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F),
color = ~city,
colors = RGB_color_vector
)

R: plotly multiple args in updatemenus with same lable

I am somewhat new to plotly and I am trying to make an before-after dot plot in which you can switch variables by dropdown menu. I actually achieved this, but I want to have a color legend feature that categorizes the direction of the before-after differences into "Before > After", "Before < After" etc. In the example I named this variable dir_y. The updatemenus updates the variables (in my example y and z) but I dont know how to update dir_y and dir_z while maintaining only the 2 dropdown options ("Var y" and "Var z"). Needless to say, I need to update dir_y and dir_z in oder to select only one category ("Before > After", "Before < After" etc) from the legend and that category needs to correspond to either y or z depending on which one is selected from the dropdown. I added 2 comments where I thought dir_y and dir_z updating should go, but nothing I tried worked.
Thank you. Any help is greatly appreciated.
Here is my code:
library(plotly)
library(tidyverse)
set.seed(81)
df <- data.frame(id = rep(1:100, 2),
x = c(rep("pre", 100), rep("post", 100)),
y = runif(200),
z = rnorm(200, mean = 50, sd = 10))
df <- df[-sample(1:nrow(df), size = 20) , ] # delete some rows at random to simulate missing values
df_plotly <-
df %>%
mutate(x = forcats::fct_relevel(x, "pre", "post")) %>% # relevel Pre Post for plot
mutate(jit_x = jitter(as.numeric(x))) %>% # add jitter to x discrete var before piping to plotly
mutate(y = round(y, 2),
z = round(z, 2)) %>% # round y & z
group_by(id) %>% # group by id
mutate(dif_y = coalesce(lag(y) - y, y - lead(y)), # do Pre - Post by id for y
dif_z = coalesce(lag(z) - z, z - lead(z))) %>% # do Pre - Post by id for z
mutate(dir_y = case_when(dif_y != 0 && dif_y > 0 ~ "Pre > Post",
dif_y != 0 && dif_y < 0 ~ "Pre < Post",
dif_y == 0 ~ "Pre = Post",
TRUE ~ "Unpaired"),
dir_z = case_when(dif_z != 0 && dif_z > 0 ~ "Pre > Post",
dif_z != 0 && dif_z < 0 ~ "Pre < Post",
dif_z == 0 ~ "Pre = Post",
TRUE ~ "Unpaired"))
p1 <-
df_plotly %>%
plot_ly(x = ~jit_x, y = ~y) %>%
add_trace(x = ~jit_x, y = ~y, color = ~dir_y, colors = c("red", "lightgrey", "green", "black"),
mode = 'markers+lines', type = 'scatter', hoverinfo = 'text+y',
text = ~paste("ID: ", id, "<br>")
) %>%
layout(
title = "",
xaxis = list(title = "",
tickvals = list(1, 2), # jitter(1:2) from 2 levels factor produces values around 1 & 2, should be fine
ticktext = list("Pre", "Post") ),
yaxis = list(title = "",
hoverformat = '.2f',
zeroline = F),
updatemenus = list(
list(
buttons = list(
list(method = "restyle",
args = list("y", list(df_plotly$y) # , "dir_y", list(df_plotly$dir_y)
),
label = "Var Y"),
list(method = "restyle",
args = list("y", list(df_plotly$z) # , "dir_y", list(df_plotly$dir_z)
),
label = "Var Z")))
))
p1

R - Highcharter: Drilldown on stacked column graph

I've created a stacked column chart in Highcharter using R and I am trying to be able to drilldown into it.
I.e. In the picture attached, I want to be able to drill down in the red section of column CRDT. So far, I can only get it so each color section of CRDT drills into the same information OR each red section drills into the same information. I need a combined filter.
Below is my code that drills "CRDT Red" information for all red sections:
Lvl1Grouping <- aggregate(WIPGate2$Receipt.Qty, by = list(WIPGate$Hold.Code,WIPGate2$Aging),FUN=sum)
Lvl1df <- data_frame(name = Lvl1Grouping$Group.1,
y = Lvl1Grouping$x,
stack = Lvl1Grouping$Group.2,
drilldown = tolower(stack)
)
hc <- highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "WIP") %>%
hc_xAxis(type = "category") %>%
hc_legend(enabled = FALSE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_add_series(name = "Greater than 30 days",data=Lvl1dfLvl1df$stack=="Greater than 30 days",], color = "#D20000") %>%
hc_add_series(name = "Between 20-30 days",data=Lvl1df[Lvl1df$stack=="Between 20-30 days",], color = "#FF7900") %>%
hc_add_series(name = "Between 10-20 days",data=Lvl1df[Lvl1df$stack=="Between 10-20 days",], color = "#F6FC00") %>%
hc_add_series(name = "Less than 10 days",data=Lvl1df[Lvl1df$stack=="Less than 10 days",], color = "#009A00")
hc
Lvl2GroupingCRDT <- WIPGate2[WIPGate2$Hold.Code == "CRDT",]
Lvl2GroupingCRDT4 <- Lvl2GroupingCRDT[Lvl2GroupingCRDT$Aging == "Greater than 30 days",]
Lvl2GroupingCRDT4 <- aggregate(Lvl2GroupingCRDT4$Receipt.Qty, by = list(Lvl2GroupingCRDT4$Customer.Name),FUN=sum)
dfCRDT4 <- data_frame(
name = Lvl2GroupingCRDT4$Group.1,
value = Lvl2GroupingCRDT4$x
)
hc <- hc %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(
id = "greater than 30 days",
name = "CRDT",
data = list_parse2(dfCRDT4)
)
)
)
hc
Current Situation .png
I have figured out the code, however it is not an eloquent solution...
The trick is instead of having a single data frame for the Level 1 information, there needs to be a separate data frame for each part of the stack. This way you can put an ID to it in order to be able to reference.
My code is hundreds of lines in order to splice out the data in the way it needs to be so if anyone has a better solution, please post it!! (my actually code includes 7 other groups besides "CRDT", so imagine "CRDT" lines below * 7!!!
FYI, I have changed some of my dashboard and variables, so they may not be the same as above...
WIPGate2Aging <- WIP_Ops_Filtered()[WIP_Ops_Filtered()$Hold.Code!="",]
WIPGate2G30 <- WIPGate2Aging[WIPGate2Aging$Aging == "Greater than 30 days",]
WIPGate22030 <- WIPGate2Aging[WIPGate2Aging$Aging == "Between 20-30 days",]
WIPGate21020 <- WIPGate2Aging[WIPGate2Aging$Aging == "Between 10-20 days",]
WIPGate2L10 <- WIPGate2Aging[WIPGate2Aging$Aging == "Less than 10 days",]
try(Lvl1GroupingG30 <- aggregate(WIPGate2G30$Receipt.Qty, by = list(WIPGate2G30$Hold.Code),FUN=sum),silent = TRUE)
if (exists("Lvl1GroupingG30")) {} else {Lvl1GroupingG30=data.table(Group.1=numeric(), x=numeric())}
try(Lvl1Grouping2030 <- aggregate(WIPGate22030$Receipt.Qty, by = list(WIPGate22030$Hold.Code),FUN=sum),silent = TRUE)
if (exists("Lvl1Grouping2030")) {} else {Lvl1Grouping2030=data.table(Group.1=numeric(), x=numeric())}
try(Lvl1Grouping1020 <- aggregate(WIPGate21020$Receipt.Qty, by = list(WIPGate21020$Hold.Code),FUN=sum),silent = TRUE)
if (exists("Lvl1Grouping1020")) {} else {Lvl1Grouping1020=data.table(Group.1=numeric(), x=numeric())}
try(Lvl1GroupingL10 <- aggregate(WIPGate2L10$Receipt.Qty, by = list(WIPGate2L10$Hold.Code),FUN=sum),silent = TRUE)
if (exists("Lvl1GroupingL10")) {} else {Lvl1GroupingL10=data.table(Group.1=numeric(), x=numeric())}
Lvl1dfG30 <- data_frame(name = Lvl1GroupingG30$Group.1, y = Lvl1GroupingG30$x, drilldown = tolower((paste(name,"4"))))
Lvl1df2030 <- data_frame(name = Lvl1Grouping2030$Group.1, y = Lvl1Grouping2030$x, drilldown = tolower((paste(name,"3"))))
Lvl1df1020 <- data_frame(name = Lvl1Grouping1020$Group.1, y = Lvl1Grouping1020$x, drilldown = tolower((paste(name,"2"))))
Lvl1dfL10 <- data_frame(name = Lvl1GroupingL10$Group.1, y = Lvl1GroupingL10$x, drilldown = tolower((paste(name,"1"))))
Lvl2GroupingCRDTG30 <- WIPGate2Aging[WIPGate2Aging$Hold.Code == "CRDT" & WIPGate2Aging$Aging == "Greater than 30 days",]
try(Lvl2GroupingCRDTG30b <- aggregate(Lvl2GroupingCRDTG30$Receipt.Qty, by = list(Lvl2GroupingCRDTG30$Customer.Name),FUN=sum),silent = TRUE)
if (exists("Lvl2GroupingCRDTG30b")) {} else {Lvl2GroupingCRDTG30b=data.table(Group.1=numeric(), x=numeric())}
Lvl2GroupingCRDT2030 <- WIPGate2Aging[WIPGate2Aging$Hold.Code == "CRDT" & WIPGate2Aging$Aging == "Between 20-30 days",]
try(Lvl2GroupingCRDT2030b <- aggregate(Lvl2GroupingCRDT2030$Receipt.Qty, by = list(Lvl2GroupingCRDT2030$Customer.Name),FUN=sum),silent = TRUE)
if (exists("Lvl2GroupingCRDT2030b")) {} else {Lvl2GroupingCRDT2030b=data.table(Group.1=numeric(), x=numeric())}
Lvl2GroupingCRDT1020 <- WIPGate2Aging[WIPGate2Aging$Hold.Code == "CRDT" & WIPGate2Aging$Aging == "Between 10-20 days",]
try(Lvl2GroupingCRDT1020b <- aggregate(Lvl2GroupingCRDT1020$Receipt.Qty, by = list(Lvl2GroupingCRDT1020$Customer.Name),FUN=sum),silent = TRUE)
if (exists("Lvl2GroupingCRDT1020b")) {} else {Lvl2GroupingCRDT1020b=data.table(Group.1=numeric(), x=numeric())}
Lvl2GroupingCRDTL10 <- WIPGate2Aging[WIPGate2Aging$Hold.Code == "CRDT" & WIPGate2Aging$Aging == "Less than 10 days",]
try(Lvl2GroupingCRDTL10b <- aggregate(Lvl2GroupingCRDTL10$Receipt.Qty, by = list(Lvl2GroupingCRDTL10$Customer.Name),FUN=sum),silent = TRUE)
if (exists("Lvl2GroupingCRDTL10b")) {} else {Lvl2GroupingCRDTL10b=data.table(Group.1=numeric(), x=numeric())}
dfCRDTG30 <- arrange(data_frame(name = Lvl2GroupingCRDTG30b$Group.1,value = Lvl2GroupingCRDTG30b$x),desc(value))
dfCRDT2030 <- arrange(data_frame(name = Lvl2GroupingCRDT2030b$Group.1,value = Lvl2GroupingCRDT2030b$x),desc(value))
dfCRDT1020 <- arrange(data_frame(name = Lvl2GroupingCRDT1020b$Group.1,value = Lvl2GroupingCRDT1020b$x),desc(value))
dfCRDTL10 <- arrange(data_frame(name = Lvl2GroupingCRDTL10b$Group.1,value = Lvl2GroupingCRDTL10b$x),desc(value))
highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(type = "category") %>%
hc_yAxis(gridLineWidth = 0) %>%
hc_legend(enabled = TRUE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_add_series(name = "Greater than 30 days",data=Lvl1dfG30, color = "#D20000") %>%
hc_add_series(name = "Between 20-30 days",data=Lvl1df2030, color = "#FF7900") %>%
hc_add_series(name = "Between 10-20 days",data=Lvl1df1020, color = "#F6FC00") %>%
hc_add_series(name = "Less than 10 days",data=Lvl1dfL10, color = "#009A00") %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(id = "crdt 4", data = list_parse2(dfCRDTG30), name="Customer"),
list(id = "crdt 3", data = list_parse2(dfCRDT2030), name="Customer"),
list(id = "crdt 2", data = list_parse2(dfCRDT1020), name="Customer"),
list(id = "crdt 1", data = list_parse2(dfCRDTL10), name="Customer")))

Resources