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.
Related
I need to achieve the scatter plot with color, size, symbol and text.
My goal is setting text with different color variable from scatter plot.
The color is from Species
The size is from Sepal.Width
The shape is from shape_1
The text is from Sepal.Width with color from color_1
If I run the first chunk code, it is correct.
Then if I add_trace to display the text, the color messed up
Any idea will be great
dt <- iris
dt$shape_1 <- c("Yes","No")
dt$color_1 <- c("Medium","Large","Small")
plot_ly(dt,
x=1:nrow(iris),
y=~Sepal.Length,
type="scatter",
mode='markers',
color=~Species,
colors = c("#4477AA","#DDCC77","#CC6677"),
size = ~Sepal.Width,
symbol = ~shape_1,
symbols = c("triangle-up", "circle"),
inherit = F,
sizes = c(10, 100) * 10)
dt <- iris
dt$shape_1 <- c("Yes","No")
dt$color_1 <- c("Medium","Large","Small")
plot_ly(dt,
x=1:nrow(iris),
y=~Sepal.Length,
type="scatter",
mode='markers',
color=~Species,
colors = c("#4477AA","#DDCC77","#CC6677"),
size = ~Sepal.Width,
symbol = ~shape_1,
symbols = c("triangle-up", "circle"),
inherit = F,
sizes = c(10, 100) * 10) %>%
add_trace(type="scatter",
mode = "text",
text=~Sepal.Width,
textposition = "top right",
color = ~color_1,
colors = c("black","green","blue"),
textfont = list(size = 10)
)
You can change the text argument in the add_trace() function to use the same color scale as the rest of the markers, also the colour codes should match, that's where the issue is.
plot_ly(
dt,
x = 1:nrow(iris),
y = ~ Sepal.Length,
type = "scatter",
mode = 'markers',
color = ~ Species,
colors = c("#4477AA", "#DDCC77", "#CC6677"),
size = ~ Sepal.Width,
symbol = ~ shape_1,
symbols = c("triangle-up", "circle"),
inherit = F,
sizes = c(10, 100) * 10
) %>%
add_trace(
type = "scatter",
mode = "text",
text = ~ Sepal.Width,
textposition = "top right",
color = ~ Species,
colors = c("#4477AA", "#DDCC77", "#CC6677"),
textfont = list(size = 10
)
)
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"))
I found an issue when creating ternary diagrams in R using plotly. If the color aesthetic is a continuous variable the ternary diagram appears to have a binary diagram in the background. Discrete color aesthetics seem to work fine. Does someone know a fix?
library(tidyverse)
library(plotly)
# Make a dataframe
df<-data.frame(ID = 1:6,
ID2 = c(rep("B14",4),"B16","B16"),
Location = c(40.9,96,120,308.5,322,420),
Classifier = c(rep("A",3),rep("B",3)),
A = c(0.06,0.06,0.02,0.02,0.01,0.01),
B = c(0.04,0.01,0.03,0.04,0.02,0.06),
C= c(0.26,0.06,0.43,0.35,0.29,0.74),
X = c(363,696,757,1650,609,392)
)
Example of ternary with discrete colors
# Colours by discrete variable work fine
df %>%
plot_ly(
a = ~A,
b = ~B,
c = ~C,
text = ~ID,
color = ~Classifier,
colors = "Set1",
type = 'scatterternary',
mode = 'markers',
marker = list(
symbol ='circle',
opacity = 0.6,
size = 15
))
Colors by continuous variable create two additional axis in the background of the ternary.
df %>%
plot_ly(
a = ~A,
b = ~B,
c = ~C,
text = ~ID,
color = ~X,
colors = "Spectral",
type = 'scatterternary',
mode = 'markers',
marker = list(
symbol ='circle',
opacity = 0.6,
size = 15
))
This isn't a fix - more of a mask for the problem. You can hide the x and y axes like this:
ax <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = FALSE,
showgrid = FALSE
)
# use your df:
df %>%
plot_ly(
a = ~A,
b = ~B,
c = ~C,
text = ~ID,
color = ~X,
colors = "Spectral",
type = 'scatterternary',
mode = 'markers',
marker = list(
symbol ='circle',
opacity = 0.6,
size = 15
)) %>%
layout(xaxis = ax, yaxis = ax)
I've a boxplot, but I want to use manual colors for the fill of eachone.
code:
p <- plot_ly(tvs, x = ~ecommerce, y = ~precio_actual, color = ~ecommerce, type = "box") %>%
layout(boxmode = "group")
p
colors I need to use:
ecommerce_colors <- c("lacuracao" = "#ffda33","ripley" = "#802D69","falabella" = "#BED800",
'wong' = "red", "tottus" = "#fa3e14")
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))