我想在一个闪亮的应用程序中包含一个三元图。我使用的是包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)
})
}
))
我明白了:
为什么会有差异? 这是一个错误吗?不管怎么说呢?
谢谢, 安东尼奥
答案 0 :(得分:0)
使用print(ggtern_plot)
(如以上注释中所建议的那样)不能正确转换打印图的点击次数,也无法缩放打印图。
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")