Plotly R order scatter plot legend entries - r

I create a plot in the following manner:
## generate test data
getTestData <- function(seed_val=711, noise=1.0) {
set.seed(seed_val)
d <- seq(as.Date('2017/01/01'), as.Date('2017/01/08'), "days")
first_name <- rep("Jane", 8)
first_name <- append(first_name, rep("Fred", 8))
first_name <- append(first_name, rep("Sally", 8))
y1_vals <- seq(1, 3*8, 1)
y2_vals <- rnorm(3*8, mean=y1_vals, sd=noise)
dat <- data.frame(date=d, f_name=first_name, y1=y1_vals, y2=y2_vals,
stringsAsFactors = FALSE)
return(dat)
}
dat <- getTestData()
library(dplyr)
library(plotly)
p1 <- plot_ly(dat, x=~date, y=~y1, color=~f_name,
type = 'scatter', mode = "lines+markers") %>%
layout(yaxis = list(title = "some important y value")) %>%
add_trace(y=~y2, name='actual', showlegend=FALSE,
type='scatter', mode='lines',
line=list(width = 2, dash = 'dash'), color=~f_name)
Plotly orders the legend alphabetically by the f_name grouping, but I want this order to be: Jane, Fred, Sally which is the original order in the data frame
The accepted answer given here:
Plotly R order legend entries
in the section commented as #Set sort argument to FALSE and now orders like the data frame is very close to what I need, but this solution is for a pie chart. I need to reorder my legend in a scatterplot which doesn't appear to have a sort parameter available to set (like the pie chart does).

Is this what you want?
dat$f_name <- factor(dat$f_name, levels = c("Jane", "Fred", "Sally"))
plot_ly(dat, x=~date, y=~y1, color=~f_name,
type = 'scatter', mode = "lines+markers") %>%
layout(yaxis = list(title = "some important y value")) %>%
add_trace(y=~y2, name='actual', showlegend=FALSE,
type='scatter', mode='lines',
line=list(width = 2, dash = 'dash'), color=~f_name)

To add something to previous answer, you can also choose colors for names. Note I added Peter to show that if whenever it appears Peter (not this case), its color would be gray (I needed to choose colors in shiny and I needed them to be the same, whether they appeared or not, or in casi they appear)
dat$f_name <- factor(dat$f_name, levels = c("Jane", "Fred", "Sally", "Peter"))
ccolors <- c('black', 'blue','red', 'gray')
plot_ly(dat, x=~date, y=~y1, color=~f_name, colors=ccolors,
type = 'scatter', mode = "lines+markers") %>%
layout(yaxis = list(title = "some important y value")) %>%
add_trace(y=~y2, name='actual', showlegend=FALSE,
type='scatter', mode='lines',
line=list(width = 2, dash = 'dash'), color=~f_name)

Related

Adding hover annotations to a Heatmap plotly R

I'm currently using plotly's heatmap to make a risk matrix (if anyone knows a more efficient way to do it I'd be open to suggestions). Currently I add information about the different risks with annotations but I would like that instead of annotations it was a marker that contains the information when hovering over it, as in the image that I attached below.
Below I attach the code and an image of how I would like it to be.
I'll appreciate any suggestion, advice, etc. Thanks!
df.risk <- data.frame(
Risk = paste0("R",1:5),
Prob = runif(5),
Cons = runif(5,1,5))
m <- c(rep(0.1,4),0.5,rep(0.1,2),rep(0.5,3),0.1,rep(0.5,3),0.9,0.1,rep(0.5,2),rep(0.9,2),rep(0.5,2),rep(0.9,3))
scale <- matrix(m, nrow = 5, ncol = 5)
pal <- c("#1A9641",
"#EFE90F",
"#F44336")
fig <- plot_ly(
x = seq(1,16,by = 4),
y = seq(0,1,by = 0.2),
z = scale,
type = "heatmap",
colors = colorRamp(pal))
fig <- fig %>%
layout(xaxis = list(title = 'Cons'),
yaxis = list(title = 'Prob'))
fig %>%
add_annotations(y = df.risk$Prob,
x = df.risk$Cons,
text = df.risk$Risk,
showarrow = FALSE) %>% hide_colorbar()
Let me know if this is what you were looking for. I changed your data in df.risk, so that the template was a bit more obvious. Other than that the content leading up to the plot_ly object remained as you presented it in your question.
The data changes first
df.risk <- data.frame(
Risk = rep(c("Product rendered obsolete",
"Strict legal requirements",
"Sufficient human resources",
"Sufficient material resources",
"Sufficient manufacturing equipment",
"Sufficient sales",
"Reliable suppliers",
"Unknown/unintended costs exceeding ROI",
"Evidence technology will work",
"Sufficient market share long term"), 3),
Prob = runif(10),
Cons = runif(10,1,5))
Then the initial creation of the plot_ly object.
fig <- plot_ly(
x = seq(1,16,by = 4),
y = seq(0,1,by = 0.2),
z = scale,
hoverinfo = "none",
type = "heatmap",
colors = colorRamp(pal)) %>%
add_markers(
inherit = F,
x = ~Cons, y = ~Prob,
data = df.risk,
showlegend = F,
text = ~Risk,
color = I("white"), # I("transparent") or whatever color you prefer
hovertemplate = paste0("%{text}<br><br>", # risks
"Probability: %{y:.0%}<br>", # prob % rounded
"Severity: %{x:.2f}", # severity, rounded
"<extra></extra>")) # no trace info
fig %>%
layout(xaxis = list(title = 'Consequences/Severity'),
yaxis = list(title = 'Probability')) %>% hide_colorbar()
This is what it looks like.

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:

Plotly: How to customize colors in a stacked bar chart?

I would like to ask you if you could help me in customizing of colors in a stacked bar chart created by plotly.
The problem is following - I have to recreate a dashboard (from an excel file to a html file). A part of the dashboard is a chart providing us with information about early production of each entity. The chart is a stacked bar chart type by plotly. As each entity is defined by a specific color (defined in RGB) throughout whole dashboard, I need to keep these colors in the donut chart as well. But there is a problem. I always get the following warning:
Warning message:
In RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
and the resulting donut chart containts only one Entity with a not-specified color. Also, the colors in the legend are not those which are defined.
Any idea what to do with it? Thank you so much in advance.
Code:
library(dplyr)
library(plotly)
dt <- as.data.frame(matrix(ncol = 13, nrow = 19))
colnames(dt) <- c("Entity", month.abb)
for (i in 1:nrow(dt)) {
dt[i, 1] <- paste("Entity", i, sep="")
dt[i, -1] <- floor(runif(12, min=0, max=100))
}
# assign colors to entities
dt$"EntityColor" <- c("#074263", "#0B5394", "#3D85C6", "#6D9EEB", "#A4C2F4", "#CFE2F3", "#5B0F00", "#85200C", "#A61C00", "#CC4125", "#DD7E6B", "#E6B8AF", "#F8CBAD", "#F4CCCC", "#274E13", "#38761D", "#E06666", "#CC0000", "#20124D")
data.table::melt(dt) %>%
plot_ly(x = ~variable,
y = ~value,
type = "bar",
color = ~Entity,
marker = list(colors = ~EntityColor)
) %>%
layout(yaxis = list(title = ""),
xaxis = list(title = ""),
barmode = 'stack')
Plot:
Refined approach after comments:
Since the colors turned out to be a bit tricky (see initial suggestion below) I had to break the whole thing down and use a combination of plot_ly() and add_traces() in a loop to make sure that the plotly settings did not apply colors in the wrong order.
The following plot should be exactly what you're looking for.
Plot:
Note that I've appended a continuous numerical column ID. Why? Because you wanted the names in alphabetical order, and the rows are added to the plot in the order they appear in your source. And It's a bit tricky since a straight up ordering using dt %>% arrange((Entity)) would give you Entity1, Enitity10, Entity11 etc. Let me know if you'd like to adjust this in any other way.
Code:
library(dplyr)
library(plotly)
# data
set.seed(123)
dt <- as.data.frame(matrix(ncol = 13, nrow = 19))
colnames(dt) <- c("Entity", month.abb)
for (i in 1:nrow(dt)) {
dt[i, 1] <- paste("Entity", i, sep="")
dt[i, -1] <- floor(runif(12, min=0, max=100))
}
# assign colors to entities
dt$"EntityColor" <- c("#074263", "#0B5394", "#3D85C6", "#6D9EEB", "#A4C2F4", "#CFE2F3", "#5B0F00", "#85200C", "#A61C00", "#CC4125", "#DD7E6B", "#E6B8AF", "#F8CBAD", "#F4CCCC", "#274E13", "#38761D", "#E06666", "#CC0000", "#20124D")
# sort data
dt$ID <- seq.int(nrow(dt))
dt <- dt %>% arrange(desc(ID))
# specify month as factor variable to ensure correct order
months=names(dt)[2:13]
months<- factor(months, levels = c(months))
# plotly setup
p <- plot_ly(type = 'bar')
# add trace for each entity
nrows = nrow(dt)
for(i in 1:nrows) {
p <- p %>% add_trace(x=months, y = unlist(dt[i,2:13], use.names=F), type = 'bar',
#name = paste(dt[i,1], dt[i,14], sep = "_"),
name = dt[i,1],
type = 'bar',
marker=list(color = dt[i,14])) %>%
layout(barmode = 'stack')
}
# Edit layout
p <- p %>% layout(title = list(xanchor='right', text='Correct colors, orderered legend'),
yaxis = list(title = ''),
xaxis = list(title = 'month'))
p
Color correctness verification:
Initial suggestion
Here's an initial suggestion. First of all, color = ~Entity has got to go. And marker = list(color = ~EntityColor) versus marker = list(colors = ~EntityColor) gives two different results. What makes matters even stranger is that the pie chart documentation uses:
marker = list(colors = colors, ...)
... and the bar chart documentation uses:
marker = list(color = c('rgba(204,204,204,1)', 'rgba(222,45,38,0.8)', ...)
...without the s at the end of color.
Either way, you should test both marker = list(color = ~EntityColor) and marker = list(colors = ~EntityColor) and see what's correct for you.
Plot:
Code:
dt <- as.data.frame(matrix(ncol = 13, nrow = 19))
colnames(dt) <- c("Entity", month.abb)
for (i in 1:nrow(dt)) {
dt[i, 1] <- paste("Entity", i, sep="")
dt[i, -1] <- floor(runif(12, min=0, max=100))
}
# assign colors to entities
dt$"EntityColor" <- c("#074263", "#0B5394", "#3D85C6", "#6D9EEB", "#A4C2F4", "#CFE2F3", "#5B0F00", "#85200C", "#A61C00", "#CC4125", "#DD7E6B", "#E6B8AF", "#F8CBAD", "#F4CCCC", "#274E13", "#38761D", "#E06666", "#CC0000", "#20124D")
data.table::melt(dt) %>%
plot_ly(x = ~variable,
y = ~value,
name= ~Entity,
type = "bar",
#color = ~Entity,
marker = list(colors = ~EntityColor)
) %>%
layout(yaxis = list(title = ""),
xaxis = list(title = ""),
barmode = 'stack')
Take a look and see how it works out for you.

percent sign in the legend of a bar chart using plotly in shiny

I am using plotly package in R-Shiny to plot bar chart. The problem is that the percent sign is not shown in the .
The legend should be generated by using the following piece of code:
data1<- data[,list('MW (%)'=sum(as.numeric(Markw))/mw.g),by=Best]
data2<- data[,list('EL (%)'=sum(as.numeric(`EL absolut`))/EL.g),by=Best]
data3<- data[,list('VaR (%)'=sum(`VaR absolut`)/Var.g),by=Best]
.
.
.
I did not find any option to show (%) in the legend. I tried even the Unicode Character of %, however without success.
Any idea what could be the solution?
The whole code is really big and dose not really help to find the solution if one dose not know already the answer. Everyone can construct a minimal example in which one column is named: 'MW (%)'
However, the part of plot in server.R
output$plot <- renderPlotly({
d.plot<-data.frame(dataset())
# make traces:
x<- d.plot[,1]
y1<- d.plot[,2]*100
y2<- d.plot[,3]*100
y3<- d.plot[,4]*100
#make the plot data frame:
pl.d<- data.frame(x,y1,y2,y3)
#define the margin
m <- list(
b = 200
#autoexpand = TRUE
)
#plot
plot_ly(pl.d, x = ~x, y = ~y1, type = 'bar', name = colnames(d.plot)[2], marker = list(color = "#26478A"),
height=800) %>%
add_trace(y = ~y2, name = colnames(d.plot)[3], marker = list(color = "#FF6600")) %>%
add_trace(y = ~y3, name = colnames(d.plot)[4], marker = list(color = "#7E182F")) %>%
layout("title" = colnames(d.plot)[1],
xaxis = list(title = "", tickangle = 65,tickfont = list(family= "Arial",size = 14),
margin = m
),
yaxis = list(title = "",ticksuffix = "%"),
barmode = 'group'
#margin = m
)
})
You will need to check what is passed to the name arguement of plot_ly and add_trace:
library(plotly)
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
p <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'MW (%)') %>%
add_trace(y = ~LA_Zoo, name = 'EL (%)') %>%
layout(yaxis = list(title = 'Count'), barmode = 'group')
p
example source
Edit:
With the above example I wanted to point out, that this hasn't got to do anything with plotly.
The reason for the behaviour you describe is that the data.frame() function by default uses the make.names() function to check for syntactically valid names.
You can disable this option by setting check.names=FALSE in your call to data.frame().
Please see the following:
df1 <- data.frame('MW (%)' = 1:10)
print(names(df1))
# [1] "MW...."
df2 <- data.frame('MW (%)' = 1:10, check.names=FALSE)
print(names(df2))
# [1] "MW (%)"
I hope this helped to clarify.
actually I did not change the code really. Because of the answer above, I recognized the problem lies on
d.plot<-data.frame(dataset())
I changed just for instance
name = colnames(d.plot)[2]
into
name = colnames(dataset())[2],
where dataset() is a data.table.
In other words,data.frame replacing by data.table

Plot_ly Scatterplot connecting lines how do most elegant?

I'm asking myself how to solve the following problem the most elegant. My data encompasses of some actual values and some proposed values. Right now I have data that looks like the reproducible example below:
library(plotly)
library(dplyr)
test_dt <- data.frame(Age=1:5, Key=c("Actuals", "Actuals", "Actuals", "Other", "Other") , Value=rnorm(5))
plot_ly(data = (test_dt %>% group_by(., Key) %>% arrange(desc(Age))),
x = ~Age,
y = ~Value,
type = 'scatter',
mode = 'lines',
color = ~Key,
linetype = ~Key
) %>% layout(
yaxis = list(
title = "SD"),
margin = list(top=100, b=50)
)
The output of this code looks like this:
how plot a dashed line where i drew the red arrow?
My solution so far is that I access the last value of my actuals and insert this value as a new row for my "other" line. But I don't think that's very elegant and sometimes, if no other values exist which can happen in my data depending on the inputs then I have a legend plotted for my "other" line without actually having one.
act_age_max <- filter(test_dt, Key=="Actuals") %>% .[["Age"]] %>% max
propval_names <- filter(test_dt, Key!="Actuals") %>% .[["Key"]]
last_actual <- filter(test_dt, Age==act_age_max, Key=="Actuals") %>% .[["Value"]]
acts_year <- filter(test_dt, Age==act_age_max, Key=="Actuals") %>% .[["Year"]]
append_dt <- data.frame(Age=act_age_max, Key=propval_names, Value=last_actual)
plot_data <- rbind(test_dt, append_dt)
plot_ly(data = (plot_data %>% group_by(., Key) %>% arrange(desc(Age))),
x = ~Age,
y = ~Value,
type = 'scatter',
mode = 'lines',
color = ~Key,
linetype = ~Key
) %>% layout(
yaxis = list(
title = "SD"),
margin = list(top=100, b=50)
)

Resources