How to create an Orbit Chart in R? (Plotly/ggplot2) - r

I have spent time researching with no direction on how to create an orbit chart
I would ideally like to be able to create interactive versions (such as Plotly) but a ggplot2 would suffice as well.
Any suggestions are much appreciated!

For a weekly vis contest some time ago, I created some charts like this. I think the commonly accepted term now is "connected scatterplot".
Here is the skeleton plotly code I used.
plot_ly(
df,
x = x_var,
y = y_var,
group = group_var,
mode = "markers") %>%
add_trace(
x = x_var,
y = y_var,
xaxis = list(title = ""),
yaxis = list(title = ""),
group = group_var,
line = list(shape = "spline"),
showlegend = FALSE,
hoverinfo = "none")
You can look at the github repo for my submission which includes the code for both ggplot and plotly to produce connected scatterplots.

Using ggplot2:
geom_path() connects the observations in the order in which they appear in the data. geom_line() connects them in order of the variable on the x axis.
Taken from the ggplot manual page: http://docs.ggplot2.org/current/geom_path.html
You may also try out geom_curve and geom_segment if you want more control.

Thanks to #Bishop, I was able to formulate something really close to my ideal orbit chat. I included some chart annotations, for the start and end date and a label for which direction is the optimal solution.
max_date <- final_data_grp[which.max(final_data_grp$week_num), ]
min_date <- final_data_grp[which.min(final_data_grp$week_num), ]
end <- list(
x = max_date$AreaWOH,
y = max_date$SLevel,
text = paste('End', max_date$MondayDate),
xref = "x",
yref = "y"
)
start <- list(
x = min_date$AreaWOH,
y = min_date$SLevel,
text = paste('Start', min_date$MondayDate),
xref = "x",
yref = "y"
)
best_label = list(
x = min(final_data_grp$AreaWOH),
y = max(final_data_grp$SLevel),
text = 'Best Scenario',
showarrow = FALSE,
bordercolor='#c7c7c7',
borderwidth=2,
borderpad=4,
bgcolor='#ff7f0e',
opacity=.7
)
plot_ly(
final_data_grp,
x = AreaWOH,
y = SLevel,
group = MondayDate,
showlegend = FALSE,
marker = list(size = 8,
color = 'black',
opacity = .6)) %>%
add_trace(
x = AreaWOH,
y = SLevel,
line = list(shape = "spline"),
hoverinfo = "none",
showlegend = FALSE) %>%
layout(annotations = list(start, end, best_label))

Related

PlotlyR - Scatter Legend

I'm trying to show a legend in R plotly based on 3 levels.
cols <- c(Poor = "steelblue",
Fair = "slateblue",
Good = "grey45")
I have read multiple entries stating the column must be class factor. Even with that mutate "showlegend = T" does not display a legend. Is it possible do do this without adding all 3 markers separately?
plot_data <- plot_data %>% mutate(label_col = as.factor(label_col))
p <- plot_ly(data = plot_data,
x = ~r_score,
y = ~b_score,
type = "scatter",
marker = list( title = "Rating",
size = ~plot_data$TTM_Units ,
color = ~cols[plot_data$label_col],
line = list(color = 'rgba(1, 0, 0, .8)',
width = 2),
opacity = .5),
showlegend = TRUE,
inherit = TRUE)
You could achieve your desired result by mapping a variable on the color attribute and setting you desired color palette via the colors argument.
Making use of the ggplot2::diamonds dataset as example data:
cols <- c(
Poor = "steelblue",
Fair = "slateblue",
Good = "grey45"
)
library(plotly)
plot_data <- diamonds %>%
filter(cut %in% names(cols))
plot_ly(
data = plot_data,
x = ~carat,
y = ~price,
color = ~cut,
size = ~depth,
type = "scatter",
mode = "markers",
colors = cols,
marker = list(
title = "Rating",
line = list(
color = "rgba(1, 0, 0, .8)",
width = 2
),
opacity = .5
)
)
#> Warning: `line.width` does not currently support multiple values.
#> Warning: `line.width` does not currently support multiple values.

Create a stacked bar chart with 3 traces for 2 bars

I am trying to replicate the following stacked bar chart with plotly. I attach one screenshot for every hover text I get when hovering on a bar. As you will see there are 2 issues. First I cannot achieve 3 colors, besides the fact that I create them in the legend and secondly I cannot put First dose as top bar besides the fact that I use factor() based on the levels. Maybe there is an issue with the way I have created my dataset. I have no problem if you have to reform it instead of fix the plotly code to replicate the chart.
library(plotly)
Category<-c("First dose","Full vaccination")
`Uptake first dose`<-c(19.8,7.6)
`Uptake full vaccination`<-c(0,0)
`Not vaccinated`<-c(80.2,92.4)
ch5<-data.frame(Category,`Uptake first dose`,`Uptake full vaccination`,`Not vaccinated`)
ch5$Category <- factor(ch5$Category, levels = ch5[["Category"]])
ax <- list(
title = "",
showticklabels = FALSE,
showgrid = FALSE
)
fig <- plot_ly(ch5, y = ~Category, x = ~`Uptake first dose`,
type = 'bar', name = 'Uptake first dose',marker = list(color = 'lightgreen'))
fig <- fig %>% add_trace(x = ~`Uptake full vaccination`, name = 'Uptake full vaccination',marker = list(color = 'green'))
fig <- fig %>% add_trace(x = ~`Not vaccinated`, name = 'Not vaccinated',marker = list(color = 'gray'))
fig <- fig %>% layout(yaxis = ax,xaxis=list(title="",showgrid=F), barmode = 'stack')
fig
There might be a problem with your dataset. The 7.6% of full vaccination is listed under first doese. Therefore your coloring might not work.
Furthermore I transformed the data into a long format for an easy way to create hovertemplates.
library(plotly)
library(tidyverse)
# data
Category<-c("First dose","Full vaccination")
`Uptake first dose`<-c(19.8,0)
`Uptake full vaccination`<-c(0,7.6)
`Not vaccinated`<-c(80.2,92.4)
ch5<-data.frame(Category,`Uptake first dose`,`Uptake full vaccination`,`Not vaccinated`)
# transform data
data.long <- ch5 %>%
pivot_longer(cols = -Category,
names_to = "vac",
values_to = "percent") %>%
mutate(vac = str_replace_all(vac, "\\.", " "),
vac = fct_rev(factor(vac)))
# add plot
plot_ly(data.long) %>%
add_bars(y = ~Category,
x = ~percent,
color = ~vac,
text = ~vac,
colors = c("darkgreen", "green", "gray"),
hovertemplate = paste('<b>%{y}</b>',
'<br>%{text}: %{x} ',
'<extra></extra>')) %>%
layout(barmode = "stack",
yaxis = list(autorange="reversed"),
hoverlabel = list(bgcolor = "black",
bordercolor = "black",
font = list(color = "white")),
shapes = list(type = "line",
y0 = 0, y1 = 1, yref = "paper",
x0 = 70, x1 = 70),
annotations = list(text = "Target (70.0%)",
showarrow = FALSE,
x = 70,
y = 1.05,
yref = "paper"))

Plotly bars with x-axis offset for timestamps

I want to plot some timestamps with plotly bars, with 1 bar indicating a whole hour.
My problem is that the ticks are centered in the middle and I would like to shift them to the left end of the bars.
When the plot isn't zoomed in, it's not such a problem, but when zooming in, more tick-labels will appear and they would be wrong.
EDIT: I need the option barmode = 'overlay' as I also have other traces to plot, which are not included in this example.
The picture below illustrates my current and exptected layout and here's some data to make that plot. (Some option I tried without success are also included in the xaxis configuration but uncommented).
library(plotly)
library(lubridate)
df <- data.frame(
ts = seq(as.POSIXct("2019-03-20 00:00:00"), by = "hour", length.out = 24),
val = sample(1:100, 24)
)
plot_ly() %>%
add_bars(data = df, x = ~ts, y = ~val) %>%
layout(dragmode = "select", autosize = TRUE, selectdirection = "h",
barmode = 'overlay',
bargap = 0.05,
xaxis = list(ticks = "outside",
type = "date",
# tickson="boundaries",
# offset=1800,
tickmode = "auto",
title = ""
)) %>%
config(scrollZoom = TRUE)
Would the following meet your needs?
One of the things that I take advantage of sometimes with plotly is that you can show different values in text that are independent of your the x and y values used to plot the data.
In this case, we can create a column with an offset time value, ts_x and plot the x values a half hour past the time for each row -- If you have a column for every hour, this effectively left-aligns the bars.
library(plotly)
df <- data.frame(
ts = seq(as.POSIXct("2019-03-20 00:00:00"), by = "hour", length.out = 24),
val = sample(1:100, 24)
)
## Create a dummy column with x offset values
df$ts_x <- df$ts + 1800
plot_ly() %>%
## Plot based on the dummy column
add_bars(data = df, x = ~ts_x, y = ~val,
## Cover up our tracks by not showing true x value on hoverinfo
hoverinfo = "text",
## Give text that includes the un-altered time values
text = ~paste0("Time: ",format(ts, format = "%B %d, %Y %H:%M"),
"<br>Value: ",val)) %>%
layout(dragmode = "select", autosize = TRUE, selectdirection = "h",
barmode = 'overlay',
bargap = 0.05,
xaxis = list(ticks = "outside",
type = "date",
tickmode = "auto",
title = ""
)) %>%
config(scrollZoom = TRUE)
By default, bars are centered, I didn't find how to change this.
One alternative is to add a second bar, because when there are 2 bars for each x-axis unity, one bar is at the left of the axis tick, and the second at the right (what you are trying to obtain with one bar).
Why not creating a second invisible bar ? :)
df <- data.frame(
ts = seq(as.POSIXct("2019-03-20 00:00:00"), by = "hour", length.out = 24),
val = sample(1:100, 24),
val0 = 0
)
plot_ly(df, type = 'bar') %>%
add_trace(x = ~ts, y = ~val0) %>%
add_trace(x = ~ts, y = ~val) %>%
layout(
showlegend = FALSE
) %>%
config(scrollZoom = TRUE)
This will create a legend (as there are 2 kind of bars, ones for val and ones for val0), so I removed it.
Are you sure you are not over engineering? Subtracting 30 minutes gives me a nice graph when zooming in.
I'm not suggest you actually edit the data, even if it's what I'm doing in the code. A small function in the call to add bars could solve it? If you overlay other data it could make a mess but I just wanted to suggest it.
library(plotly)
library(lubridate)
df2 <- data.frame(
ts = seq(as.POSIXct("2019-03-20 00:00:00"), by = "hour", length.out = 24) - minutes(30),
val = sample(1:100, 24)
)
plot_ly() %>%
add_bars(data = df2, x = ~ts, y = ~val) %>%
layout(dragmode = "select", autosize = TRUE, selectdirection = "h",
barmode = 'overlay',
bargap = 0.05,
xaxis = list(ticks = "outside",
type = "date",
# tickson="boundaries",
# offset=1800,
tickmode = "auto",
title = ""
)) %>%
config(scrollZoom = TRUE)

Modifying labels (x,y,z) in heatmap on plotly?

I want to rename labels in a heatmap. for example:
instead of the label says "x:", I want the label to say "Hour:"
instead of the label says "y:", I want the label to say "Day:"
Library(plotly)
p <- plot_ly(z = volcano, colors = colorRamp(c("red", "green")), type = "heatmap")
furthermore, it would be useful, for example if we use a transformation of data in order to intensify contrast, still the html interactive label show real data.
Example
What about
library(plotly)
dat <- expand.grid(x = 1:nrow(volcano), y = 1:ncol(volcano))
dat$z <- c(volcano)
plot_ly(height = 500) %>%
layout(autosize = FALSE,
xaxis=list(title = "Hour", titlefont = list(size=20)),
yaxis=list(title = "Day", titlefont = list(size=20))) %>%
add_trace(data = dat, x = ~x, y = ~y, z = ~z, type = "heatmap",
hoverinfo = 'text',
text = ~paste("Hour:", dat$x,
"<br> Day:", dat$y,
"<br> z:", dat$z))

Traces not coming up properly when working on dual axis charts with plotly R

I have written below snippet of code to plot dual-axis charts using plotly in R.
Code:
## Date creation
dtMasterWithtotals <- data.table("Period_Month" = c('7/1/2017', '9/1/2017'), A = c(171, 448), B = c(0, 655), C = c(476, 812))
## Vectors to select categories for primary and secondary axis
vecPrimaryAxis <- c("A", "B")
vecSecondaryAxis <- c("C")
## X-axis properties
ax <- list(
type = "category",
categoryorder = "array",
categoryarray = dtMasterWithtotals[order(as.Date(dtMasterWithtotals[, Period_Month])),],
showgrid = TRUE,
showline = TRUE,
autorange = TRUE,
showticklabels = TRUE,
ticks = "outside",
tickangle = 0
)
## arrange columns in an order – TBD
## The plot function below assumes that the data will be in format, Period_Month, A, B,C.
## Plot function
plot <- plot_ly(dtMasterWithtotals, x = ~Period_Month, y = dtMasterWithtotals[[2]], type = "scatter", mode = 'lines', name = names(dtMasterWithtotals)[2])
if(length(vecPrimaryAxis) > 1){
t <- (3 + length(vecPrimaryAxis) - 2)
for (i in 3:t){
plot <- add_trace(plot, x = ~Period_Month, y = dtMasterWithtotals[[i]], type = "scatter", mode = "lines", name = names(dtMasterWithtotals)[i]) %>%
layout(xaxis = ax)
}
}
if(length(vecSecondaryAxis) > 0){
p <- 2 + length(vecPrimaryAxis)
q <- p + length(vecSecondaryAxis) - 1
for (j in (p:q)){
plot <- add_trace(plot, x = ~Period_Month, y = dtMasterWithtotals[[j]], type = "scatter", mode = "lines", yaxis = "y2", name = names(dtMasterWithtotals)[j]) %>%
layout(yaxis2 = list(overlaying = "y", side = "right"), xaxis = ax)
}
}
When trying to plot A and B on primary y-axis and C on secondary y-axis, the last trace (in this case C) overlaps the second trace (in this case B), resulting in two traces instead of three. However, on hover the new trace shows the correct value labels, but comes up incorrectly (at the wrong position) in the visualization.
Let me know if you require any other detail.
Thanks.
If I understand correctly what you want, in fact, there is no problem with your code. You just need to set manually your y and y2 axis. To make it easier to visualize I simply reversed the y2 axis. If you try this:
plot_ly() %>%
add_lines(data=dtMasterWithtotals, x = ~Period_Month, y = ~A, name = "A") %>%
add_lines(data=dtMasterWithtotals, x = ~Period_Month, y = ~B, name = "B") %>%
add_lines(data=dtMasterWithtotals, x = ~Period_Month, y = ~C, name = "C", yaxis = "y2") %>%
layout(xaxis = ax, yaxis2 = list(overlaying = "y", side = "right", autorange="reversed"))
It will give you this:
As you can see, all three lines are visible and all three displayed the right values.

Resources