r-如何获得使用"coord_polar"的Shiny交互式ggplot的极坐标转换点击数据



我正在开发一个具有极坐标的交互式极坐标ggplot栏。最终目标是使每个分段中的条形图"上升",以满足鼠标在该扇区中单击的位置。

问题是极坐标ggplots的点击数据是笛卡尔的,而不是极坐标:图的左下角是c(0,0(,而不是中心。我该如何解决这个问题?有没有一个简单的坐标变换我可以使用,或者一个选项我可以设置?

你可以在这里看到该应用程序的原型,我的代码如下:

library(shiny)
library(ggplot2)
library(tibble)
xNames <-  c("Authentic", "Visionary", "Sustainable",
"Inspiring", "Collaborative", "Raising upnnew leaders")
segments <- length(xNames)

ui <- fluidPage(
mainPanel(
plotOutput("evaluation_wheel", click = "plot_click"),
p("Coordinates: "), textOutput("coordinates")
)
)
server <- function(input, output) {

## Plot evaluation wheel
wheel_plot <- function(wheel.data){
ggplot2::ggplot(wheel.data, aes(x = name, y = value)) + 
geom_bar(stat = "identity") +
scale_y_continuous(limits = c(0,10)) +
coord_polar()
}

modify_plot_data <- function(click.value, old.plot.data){
print(click.value)
if(is.null(click.value))(click.value <- c(0,0))
else{
cat("x: ")
print(click.value$x)
cat("y: ")
print(click.value$y)
click.value <- c(click.value$x, click.value$y)
}
click.value <- floor(click.value)
new.plot.data <- old.plot.data
new.plot.data[click.value[1]] <- click.value[2]
new.plot.data
}

plotData <- reactiveVal(integer(segments))

output$evaluation_wheel <- renderPlot({
# plotData <- modify_plot_data(input$plot_click, plotData)
plotData(modify_plot_data(input$plot_click, plotData()))

plotTibble <- tibble(
name = xNames,
value = plotData()
)
wheel_plot(plotTibble)

})

output$coordinates <- renderText({
paste0("c(", input$plot_click$x, ",", input$plot_click$y, ")")
})
}
# Run the application 
shinyApp(ui = ui, server = server)

您"只是";需要从极坐标投影转换回来。

modify_plot_data <- function(click.value, old.plot.data){
print(click.value)
if(is.null(click.value))(click.value <- c(0,0))
else{
# Number of categories
N_CAT = length(xNames)
# Max value
Y_LIMIT = 10 
# Center and rescale X
x=(click.value$x -( (N_CAT + 1) / 2 ) ) / N_CAT * Y_LIMIT / .4 
# Center and rescale Y
y=(click.value$y - ( Y_LIMIT / 2 ) ) / Y_LIMIT * Y_LIMIT / .4
# Compute angle from X and Y
angle = atan2( y, x)
# Compute item number from angle (might be simplified)
i = (( 5 * pi / 2 - angle ) %% ( 2 * pi )) / pi * ( N_CAT / 2 ) + 1
# Compute length from angle and X
j = min( Y_LIMIT, x / cos(angle) ) # length

click.value <- c(i, j)
}
new.plot.data <- old.plot.data
new.plot.data[floor(click.value[1])] <- click.value[2]
new.plot.data
}

为了让它按原样工作,你需要对你的价值观进行排序:

xNames <-  sort(c("Authentic", "Visionary", "Sustainable",
"Inspiring", "Collaborative", "Raising upnnew leaders"))

最新更新