I just started using plotly in R, and I've made a plot like this
with a syntax like this:
maximum <- rnorm(30, mean = 20)
datasource <- rnorm(30, mean = 15)
minimum <- rnorm(30, mean = 10)
x <- c(1:30)
data <- data.frame(x,datasource,maximum,minimum)
fig <- plot_ly(data, x = ~x, y = ~datasource, name = 'trace 1', type = 'scatter', mode = 'lines+markers', marker = list(size = 10))
fig <- fig %>% add_trace(y = ~maximum, name = 'maximum', mode = 'markers', marker = list(size = 10))
fig <- fig %>% add_trace(y = ~minimum, name = 'minimum', mode = 'markers', marker = list(size = 10))
fig <- fig %>% layout(title = "XBAR CHART",
xaxis = list(title = "ID"),
yaxis = list (title = "Measurement Value (mm)"))
fig
I want to add a line connecting the minimum and maximum markers like the following.
I've been trying but haven't found the solution.
I hope I find a solution here.
thanks
calculate the points that defined the lines
v_lines <- mutate(rowwise(data),
vline=list(list(type="line",
x0=x,
x1=x,
y0=minimum,
y1=maximum,
line=list(color="blue")))) |> pull(vline)
add them to the layout
fig <- fig %>% layout(title = "XBAR CHART",
shapes= v_lines,
xaxis = list(title = "ID"),
yaxis = list (title = "Measurement Value (mm)"))
Like this?
ggplot(data=data, aes(x,datasource))+
geom_segment(data = data, aes(x = x, y = minimum, xend = x, yend = maximum), color = "light blue", size = 1)+
geom_line(data = data, aes(x = x, y = datasource), color= "blue", size = 1)+
geom_point(data=data, aes(x, minimum), color = "green", size=3)+
geom_point(data=data, aes(x, maximum), color = "orange", size=3)+
geom_point(color = "blue", size=3)+
xlab("X - axis text")+ylab("Y - axis text")+
theme_light(base_size = 18, base_rect_size = 1)
I'm trying to set the colour in R for "points" or markers in plotly with a custom palette. It doesn't work. I'm also trying to add a title to the graph, but it won't appear. In addition, I'd like to remove some elements of the legend (like the extra "z") and add a title to the legend elements. Nothing seems to work, even if it is present in the code.
library(plotly)
library(tidyverse)
set.seed(123456)
pal <- c("black", "orange", "darkgreen", "pink")
pal <- setNames(pal, levels(as.factor(c("gr1","gr2","gr3","gr4"))))
linedat = data.frame(x = rep(mean(1:50),2),
y = rep(mean(1:50),2),
z = c(0,1))
zoom = 3
pg = plotly::plot_ly(x = 1:50,
y = 1:50,
z = outer(1:50,1:50,"+")/100) %>%
add_surface(contours = list(
z = list(show = TRUE, start = 0, end = 1, size = 0.05)),
opacity = 1) %>%
add_markers(x = rnorm(50,25,5),
y = rnorm(50,25,5),
marker = list(opacity = 0.9),
type="scatter3d",
mode = "markers",inherit = FALSE,
colors = pal,
color = as.factor(sample(1:4,50,replace = T)),
z = rbinom(50,1,.5)) %>%
layout(scene=list(title = "Title won't show up????",
xaxis = list(title = 'trait1'),
yaxis = list(title = 'trait1',autorange = "reversed"),
camera = list(eye = list(x = cos(0.8*pi)*zoom, y = sin(pi*1.3)*zoom, z= 2.00)),
legend = list(title=list(text='<b> Groups </b>')))) %>%
add_trace(data=linedat, x=~x, y=~y, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[1],
width = 14),name = "Avg. data1")%>%
add_trace(data=linedat, x=~x+20, y=~y+20, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[4],
width = 14),name = "Avg. data2")
pg
See how here I'm able to set the colour of the points, but I'm not able to get the names of the colours to show in the legend (I modified the code to match what #Kat suggested):
library(plotly)
library(tidyverse)
set.seed(123456)
pal <- c("black", "orange", "darkgreen", "pink")
pal <- setNames(pal, levels(as.factor(c("gr1","gr2","gr3","gr4"))))
linedat = data.frame(x = rep(mean(1:50),2),
y = rep(mean(1:50),2),
z = c(0,1))
zoom = 3
pg = plotly::plot_ly(x = 1:50,
y = 1:50,
z = outer(1:50,1:50,"+")/100) %>%
add_surface(contours = list(
z = list(show = TRUE, start = 0, end = 1, size = 0.05)),
opacity = 1, colorbar = list(title = "Only one Z")) %>%
add_markers(x = rnorm(50,25,5),
y = rnorm(50,25,5),
marker = list(color = pal[as.factor(sample(1:4,50,replace = T))],opacity = 0.9),
type="scatter3d",
mode = "markers",inherit = FALSE,
colors = pal,
z = rbinom(50,1,.5)) %>%
layout(title = "Title that won't show",
margin = list(t = 40),
legend = list(title = list(
text = "<br>Legends deserve names, too")),
scene=list(xaxis = list(title = 'trait1'),
yaxis = list(title = 'trait1',autorange = "reversed"),
camera = list(eye = list(x = cos(0.8*pi)*zoom,
y = sin(pi*1.3)*zoom, z= 2.00)))) %>%
add_trace(data=linedat, x=~x, y=~y, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[1],
width = 14),name = "Avg. data1")%>%
add_trace(data=linedat, x=~x+20, y=~y+20, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[4],
width = 14),name = "Avg. data2");pg
Your original plot_ly call and add_trace calls can remain as is. I've included the changes needed for the layout call and added the call needed for colorbar.
The layout first.
layout(title = "Title that won't show", # <------ give my plot a name
margin = list(t = 40), # don't smash that title, either
legend = list(title = list(
text = "<br>Legends deserve names, too")), # <--- name my legend
scene=list(title = "Title won't show up????", # <- this can go away
xaxis = list(title = 'trait1'),
yaxis = list(title = 'trait1',autorange = "reversed"),
camera = list(eye = list(x = cos(0.8*pi)*zoom,
y = sin(pi*1.3)*zoom, z= 2.00)),
legend = list(title=list(text='<b> Groups </b>')))) %>%
colorbar(title = "Only one Z") # <--- give me one z colorbar title
(Some colors are different in the image; I didn't realize I had the pal object...sigh)
Update
This addresses your additional questions.
First, I'm not getting an error from the method in which I title the color bar. You'll have to share what error you're getting.
I didn't realize that it was ignoring the colors you set in markers, either. The easiest way to address this is to call that trace first. Since nothing else was declared within markers, I called opacity outside of the list, but it's fine the way you have it.
First, I commented out the setNames call, because that won't work for the marker's trace and it doesn't matter to the other traces.
library(plotly)
set.seed(123456)
pal <- c("black", "orange", "darkgreen", "pink")
# pal <- setNames(pal, levels(as.factor(c("gr1","gr2","gr3","gr4"))))
linedat = data.frame(x = rep(mean(1:50),2),
y = rep(mean(1:50),2),
z = c(0,1))
zoom = 3
I made plot_ly() empty and put all the data for the surface in that trace. I also added inherit = false so that the layout could go at the end without the data errors.
set.seed(123456)
pg = plotly::plot_ly() %>%
add_trace(inherit = F,
x = rnorm(50,25,5),
y = rnorm(50,25,5),
type="scatter3d",
mode = "markers",
opacity = .8,
colors = pal,
color = as.factor(sample(1:4, 50, replace = T)),
z = rbinom(50,1,.5)) %>%
add_surface(x = 1:50,
y = 1:50,
z = outer(1:50,1:50,"+")/100,
colorscale = "Viridis",
contours = list(
z = list(show = TRUE, start = 0, end = 1, size = 0.05)),
opacity = 1) %>%
add_trace(data=linedat, x=~x, y=~y, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[1],
width = 14),name = "Avg. data1", inherit = F) %>%
add_trace(data=linedat, x=~x+20, y=~y+20, z=~z,
type="scatter3d", mode="lines",
line = list(color = pal[4],
width = 14),name = "Avg. data2", inherit = F) %>%
The last part is the layout, but this is not different than my original answer.
layout(title = "Title that won't show", # <------ give my plot a name!
margin = list(t = 40), # don't smash that title, either
legend = list(title = list(
text = "<br>Legends deserve names, too"),
tracegroupgap = 350), # <--- name my legend!
scene=list(title = "Title won't show up????", # <--- this can go away!
xaxis = list(title = 'trait1'),
yaxis = list(title = 'trait1',autorange = "reversed"),
camera = list(eye = list(x = cos(0.8*pi)*zoom,
y = sin(pi*1.3)*zoom, z= 2.00)),
legend = list(title=list(text='<b> Groups </b>')))) %>%
colorbar(title = "Only one Z") # <--- give me one z for the title!
pg
I have a plot using plotly r . As of now the variations cant be seen that much. So in order to do so if the y-axis is 0-70% is there a way to show just 50%-70% on y axis so the variation can be seen more clearly.
Below is code I am using
output$plot <- renderPlotly({
if (is.null(ab()))
return(NULL)
y <- list(title = "Percentange")
x <- list(title = "Months")
plot_ly(ab(), x = ~ Month_considered, y = ~ pct * 100,type = 'scatter', mode = 'marker',
fill = 'tozeroy', line = list(color = 'rgb(205, 12, 24)', width = 4)) %>%layout(xaxis = x, yaxis = y)})
You don't provide a reproducible example so it is hard to test, but you could try:
y <- list(title = "Percentage", range = c(50,70))
plot_ly(ab(), x = ~ Month_considered, y = ~ pct * 100,type = 'scatter', mode = 'marker', fill = 'tozeroy', line = list(color = 'rgb(205, 12, 24)', width = 4)) %>% layout(xaxis = x, yaxis = y)})
Out of curiosity I am trying to reconstruct a ggplot graph with plotly.
It is an example of a simple linear regression. The graph shows the observed data, the regression line and vertical lines showing the errors.
The ggplot looks like this:
The reconstructed plotly graph looks like this:
Is there a way to push the vertical lines showing the errors to the back of the points?
Is there a better approach?
The data may be found here:
Advertising.csv
This is the code used to make the plots:
library(ggplot2)
library(plotly)
#### prepare data ####
adv <- read.csv("Advertising.csv")
fit_tv <- lm(sales ~ TV, data = adv)
adv_plot <- data.frame(adv, fit = fit_tv$fitted.values)
#### ggplot ####
p1 <- ggplot(adv_plot, aes(x = TV, y = sales)) +
geom_segment(aes(x = TV, xend = TV, y = sales, yend = fit), size = 0.5, color = "lightgrey") +
geom_point(color = "red") +
geom_point(aes(y = fit), color = "blue")
p1
#### Plotly ####
p2 <- plot_ly(adv_plot, x = ~TV, y = ~sales, type = "scatter", mode = "markers", marker = list(color = "red", size = 5)) %>%
add_trace(x = ~TV, y = ~fit, type = "scatter", mode = "markers", marker = list(color = "blue", size = 5))
line <- list(
type = "line",
line = list(color = "lightgrey"),
xref = "x",
yref = "y"
)
lines <- list()
for (i in 1:length(adv_plot$sales)) {
line[["x0"]] <- adv_plot$TV[i]
line[["x1"]] <- adv_plot$TV[i]
line[["y0"]] <- adv_plot$sales[i]
line[["y1"]] <- adv_plot$fit[i]
lines <- c(lines, list(line))
}
p2 <- layout(p2, shapes = lines, showlegend = FALSE)
p2
At the end managed to find the answer myself. The order of the segments and traces keep the error lines in the background.
The data is here: Advertising.csv
This is the code:
library(ggplot2)
library(plotly)
adv <- read.csv("Advertising.csv")
fit_tv <- lm(sales ~ TV, data = adv)
adv_plot <- data.frame(adv, fit = fit_tv$fitted.values)
p <- plot_ly(adv_plot, x = ~TV) %>%
add_segments(x = ~TV, y = ~fit, xend = ~TV, yend = ~sales, mode = 'line', line = list(color = "lightgrey")) %>%
add_trace(y = ~sales, name = 'trace 0', type = "scatter", mode = 'markers', marker = list(color = "red", size = 5)) %>%
add_trace(y = ~fit, name = 'trace 1', type = "scatter", mode = 'markers', marker = list(color = "blue", size = 5)) %>%
layout(showlegend = FALSE)
p
I'm trying to plot overlaid line and bar charts using plotly, grouped by the same feature grp.
I need to use fixed colors for both lines and bars.
It works well when I first add the bar-trace :
library(plotly)
df <- data.frame(year = rep((2000:2017), each=2),
grp = rep(c("Grp1", "Grp2"), 18),
y1 = rnorm(36, 100, 40),
y2 = rnorm(36, 50, 10)
)
plot_ly(df) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp,
colors = colorRamp(c("grey", "black"))) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
But If I switch bar trace and line trace, my color selection for bars disappears.
plot_ly(df) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp,
colors = colorRamp(c("grey", "black"))) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
Obviously my syntax is incorrect : does someone know how to write this code properly to ensure colors are stable whatever the order is ?
Many thanks !
Try to specify colors inside plot_ly:
plot_ly(df, colors = colorRamp(c("grey", "black"))) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))