Reputation: 10372
I want to use plotly
to draw a circular segment with a given centre point and radius.
I found no way in the plotly documentation and tried the following code, which works but I do not want to see the intermediate points.
My current attempt:
library(plotly)
from <- 0
to <- 180
by <- (to-from)/10
t <- seq(from*pi/180, to*pi/180 , by*pi/180)
x0 <- 1
y0 <- 1
r <- 5
y <- y0 + r*sin(t)
x <- x0 + r*cos(t)
plotly::plot_ly() %>%
add_trace(x=~x, y = ~y, line = list(shape = "spline"))
What I want to see:
Any hints? Is it possible to remove the intermediate points afterwards? Or plot a "perfect" circle direct with plotly? Thank you in advance!
Upvotes: 1
Views: 1566
Reputation: 41250
you could use a layout
with circle
shape:
library(plotly)
plot_ly() %>% layout(shapes = list(
list(type = 'circle',
xref = 'x', x0 = -4, x1 = 6,
yref = 'y', y0 = -4, y1 = 6,
line = list(color = 'blue'))),
yaxis = list(range=c(1,6.5)))
Another option to draw only one half circle is to use the line
shape (with more segments :
by <- (to-from)/100
):
line <- list(
type = "line",
line = list(color = "blue"),
xref = "x",
yref = "y"
)
lines <- list()
for (i in 2:length(t)) {
line[["x0"]] <- x[i-1]
line[["x1"]] <- x[i]
line[["y0"]] <- y[i-1]
line[["y1"]] <- y[i]
lines <- c(lines, list(line))
}
library(plotly)
plot_ly() %>% layout(shapes = lines,
yaxis = list(range=c(0,6.5)))
Upvotes: 3