【问题标题】:Strange behaviour of ternary plot in shiny app闪亮应用程序中三元图的奇怪行为
【发布时间】:2016-01-29 11:20:04
【问题描述】:

我想在闪亮的应用程序中包含一个三元图。我正在使用包 ggtern。

当我运行以下代码时:

dd <- data.frame(x=c(3,1,5), y=c(45,29,10), z=c(10,45,94),
                ss=c(58,75,109))

ggtern(data=dd,
         aes(x=x,y=y,z=z)) +
  geom_mask() +
  geom_point() +
  Larrowlab("var1") + Tarrowlab("var2") + Rarrowlab("var3") +
  theme_showarrows() +
  theme(tern.axis.arrow.show=T)#,
          #tern.axis.text.show=F)

我明白了:

在闪亮的应用中:

library(ggtern)
library(shiny)
## data ####
dd <- data.frame(x=c(3,1,5), y=c(45,29,10), z=c(10,45,94),
                ss=c(58,75,109))
################
runApp(

  ## UI ####
  list(ui = (basicPage(
    headerPanel("ternary test"),
    mainPanel(
      plotOutput("gg", click = "plot_click")
    )
  )),

  ## server ####
  server = function(input, output) {

    output$gg <- renderPlot({

      ggtern(data=dd,
             aes(x=x,y=y,z=z)) +
      geom_mask() +
      geom_point() +
      Larrowlab("var1") + Tarrowlab("var2") + Rarrowlab("var3") +
      theme_showarrows() +
      theme(tern.axis.arrow.show=T)#,
              #tern.axis.text.show=F)
    })

  }
  ))

我明白了:

为什么会有差异? 这是一个错误吗?反正就在附近?

谢谢, 安东尼奥

【问题讨论】:

  • 尝试在renderPlot 中执行a&lt;-ggtern... 后跟print(a),似乎可以解决问题。
  • 确实如此!非常感谢。

标签: r shiny ternary


【解决方案1】:

使用 print(ggtern_plot),如上面 cmets 中所建议的那样,不能正确转换绘图点击,也不能缩放绘图。

ggtern 不适用于 Shiny,因为 ggtern 重载 plot.ggplot 但 shiny 不使用重载函数。我设法以这种方式修复了情节:

custom_print.ggplot <- function(x) {
    ggtern:::ggint$set_last_plot(x) # this is line needed by ggtern
    # based on ggplot2:::custom_print.ggplot
    grid::grid.newpage()

    data <- ggplot_build(x)
    gtable <- ggplot_gtable(data)
    grid::grid.draw(gtable)
    invisible(data)
    structure(list(
        build = data,
        gtable = gtable
    ), class = "ggplot_build_gtable")
}
assignInNamespace("custom_print.ggplot", custom_print.ggplot, "shiny")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2018-05-30
    • 2017-03-31
    • 2013-08-10
    • 1970-01-01
    • 2016-02-09
    • 2016-07-20
    相关资源
    最近更新 更多