R Plotly极坐标方程玫瑰曲线图问题
R Plotly Polar Equation Rose Curve Graph Issue
我正在尝试使用 plotly 在 R 中绘制极坐标方程。但是,当我绘制玫瑰曲线时,我没有在图中得到正确数量的花瓣。
我的代码...
library(plotly)
f <- function(){
output <- matrix(ncol=2, nrow = 361)
for (i in 0:360){
output[i,1] <- i
output[i,2] <- 3 * cos(2 * (i * pi/180))
}
return(output)
}
mf <- f()
df <- data.frame("theta" = mf[,1], "r"=mf[,2])
p <- plot_ly(
df,
type = 'scatterpolar',
mode = 'lines'
) %>%
add_trace(
r = ~r,
theta = ~theta,
name = 'Function',
line = list(
color = 'red'
)
) %>%
layout(
title = 'Polar Graph',
font = list(
family = 'Arial',
size = 12,
color = '#000'
),
showlegend = F
)
p
结果图...
图表应该类似于什么...
任何人都可以告诉我我做错了什么,或者在 R 中是否有更简单的方法来做到这一点?谢谢
对于普通的极坐标图,我习惯于圆心的半径为零,但在你的情况下,圆心的半径为 -3。因此在使用 plot_lt() 时你得不到预期的结果。 plot_lt() 可能允许您在配置中更改它,但我找不到它。
一个可能的解决方案是移动角度和半径,使半径始终大于零。这是在下面的函数“shift_center_zero”中完成的。对于每个负半径,乘以 -1 为正,并且对于这些行进行角度移动,使半径位于中心的另一侧。角度偏移是通过添加半个圆(180 度)并取一个完整圆(360 度)的模数来限制角度,使其在一个圆内。
shift_center_zero <- function(m){
m_negative <- m[,2]<0 # get negative rows
m[m_negative,1] <- (m[m_negative,1]+180)%%360 # angle shift
m[m_negative,2] <- -1*m[m_negative,2] # radius shift
return(m)
}
您的代码的其余部分几乎相同,nrow = 360 代替 361 以删除 NA 并使用新的 "shift_center_zero" 函数。
library(plotly)
f <- function(){
output <- matrix(ncol=2, nrow = 360)
for (i in 0:360){
output[i,1] <- i
output[i,2] <- 3 * cos(2 * (i * pi/180))
}
return(output)
}
mf <- f()
# make the shift
mf<-shift_center_zero(mf)
df <- data.frame("theta" = mf[,1], "r"=mf[,2])
p <- plot_ly(
df,
type = 'scatterpolar',
mode = 'lines'
) %>%
add_trace(
r = ~r,
theta = ~theta,
name = 'Function',
line = list(
color = 'red'
)
) %>%
layout(
title = 'Polar Graph',
font = list(
family = 'Arial',
size = 12,
color = '#000'
),
showlegend = F
)
p
我正在尝试使用 plotly 在 R 中绘制极坐标方程。但是,当我绘制玫瑰曲线时,我没有在图中得到正确数量的花瓣。
我的代码...
library(plotly)
f <- function(){
output <- matrix(ncol=2, nrow = 361)
for (i in 0:360){
output[i,1] <- i
output[i,2] <- 3 * cos(2 * (i * pi/180))
}
return(output)
}
mf <- f()
df <- data.frame("theta" = mf[,1], "r"=mf[,2])
p <- plot_ly(
df,
type = 'scatterpolar',
mode = 'lines'
) %>%
add_trace(
r = ~r,
theta = ~theta,
name = 'Function',
line = list(
color = 'red'
)
) %>%
layout(
title = 'Polar Graph',
font = list(
family = 'Arial',
size = 12,
color = '#000'
),
showlegend = F
)
p
结果图...
图表应该类似于什么...
任何人都可以告诉我我做错了什么,或者在 R 中是否有更简单的方法来做到这一点?谢谢
对于普通的极坐标图,我习惯于圆心的半径为零,但在你的情况下,圆心的半径为 -3。因此在使用 plot_lt() 时你得不到预期的结果。 plot_lt() 可能允许您在配置中更改它,但我找不到它。
一个可能的解决方案是移动角度和半径,使半径始终大于零。这是在下面的函数“shift_center_zero”中完成的。对于每个负半径,乘以 -1 为正,并且对于这些行进行角度移动,使半径位于中心的另一侧。角度偏移是通过添加半个圆(180 度)并取一个完整圆(360 度)的模数来限制角度,使其在一个圆内。
shift_center_zero <- function(m){
m_negative <- m[,2]<0 # get negative rows
m[m_negative,1] <- (m[m_negative,1]+180)%%360 # angle shift
m[m_negative,2] <- -1*m[m_negative,2] # radius shift
return(m)
}
您的代码的其余部分几乎相同,nrow = 360 代替 361 以删除 NA 并使用新的 "shift_center_zero" 函数。
library(plotly)
f <- function(){
output <- matrix(ncol=2, nrow = 360)
for (i in 0:360){
output[i,1] <- i
output[i,2] <- 3 * cos(2 * (i * pi/180))
}
return(output)
}
mf <- f()
# make the shift
mf<-shift_center_zero(mf)
df <- data.frame("theta" = mf[,1], "r"=mf[,2])
p <- plot_ly(
df,
type = 'scatterpolar',
mode = 'lines'
) %>%
add_trace(
r = ~r,
theta = ~theta,
name = 'Function',
line = list(
color = 'red'
)
) %>%
layout(
title = 'Polar Graph',
font = list(
family = 'Arial',
size = 12,
color = '#000'
),
showlegend = F
)
p