在 ggplot2 shiny 上使用缩放时遇到问题
Trouble using zoom on ggplot2 shiny
我是这里的新用户,但我疯狂地搜索了我在 Rstudio 中尝试创建带有 shiny 的数据可视化应用程序时遇到的问题。
问题是,我想读取一个 .csv,了解它的列,select 我希望将列作为 x 轴和 y 轴,用我选择的图表类型绘制它们并能够随时放大第二个情节。
我快到了,问题是我尝试做的带画笔的缩放不能正常工作。它不能正确理解轴的值,而是好像两个轴都只从 0 到 1,然后以正确的方式放大,但 xlim 和 ylim 错误。
这是我的 ui.R:
library(shiny)
library(ggplot2)
base = read.csv("TESTE.csv", sep = ";")
tipos <- c("Dispersão", "Histograma", "Boxplot", "Área")
shinyUI(fluidPage(
titlePanel("MGM"),
sidebarLayout(
sidebarPanel(
selectInput("selectedColX", "Select colum for X axis", choices = colnames(base), selected = colnames(base)[7]),
selectInput("selectedColY", "Select colum for Y axis", choices = colnames(base), selected = colnames(base)[4]),
selectInput("selectedColor", "Select colum for colour axis", choices = colnames(base), selected = colnames(base)[6]),
selectInput("seletedGraph", "Select type of graph", choices = tipos, selected = tipos[1])
),
fluidRow(
column(width = 12, class = "well",
h4("Left plot controls right plot"),
fluidRow(
column(width = 10,
plotOutput("Disp", height = 300,
brush = brushOpts(
id = "Disp_brush",
clip = TRUE,
resetOnNew = TRUE
)
)
),
column(width = 10,
plotOutput("DispZoom", height = 300)
)
)
)
)
# mainPanel(
#
# plotOutput("Hist"),
# plotOutput("Box"),
# plotOutput("Ar")
# )
)
))
然后是我的 Server.R:
library(shiny)
library(ggplot2)
base = read.csv("TESTE.csv", sep = ";")
tipos <- c("Dispersão", "Histograma", "Boxplot", "Área")
shinyServer(function(input, output) {
output$Disp <- renderPlot({
validate(need(input$seletedGraph=="Dispersão", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_point()
plot(gg)
})
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$DispZoom <- renderPlot({
validate(need(input$seletedGraph=="Dispersão", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) + geom_point() + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
plot(gg)
})
output$Hist <- renderPlot({
validate(need(input$seletedGraph=="Histograma", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis))
gg <- gg + geom_histogram()
gg
})
output$Box <- renderPlot({
validate(need(input$seletedGraph=="Boxplot", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_boxplot()
gg
})
output$Ar <- renderPlot({
validate(need(input$seletedGraph=="Área", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_area()
gg
})
observe({
brush <- input$Disp_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- NULL
ranges2$y <- NULL
}
})
})
忽略其他不属于geom_point的地块。一旦我让这个工作,其他的应该工作得很好,我想...
非常感谢,我正在努力解决这个问题!
有些文字是葡萄牙语,但我觉得一切都很好理解。
您的刷点在 brushOpts
中从 0 到 1 不等,因为您 print
或 plot
您的变量而不是仅仅返回它。
1.简短演示
这个简短的应用程序根据返回方式显示了刷点比例之间的差异。
library(shiny)
ui <- fluidPage(
fluidRow(
column(6,
# My plot rendering with print or plot
h4("Plot with print or plot variable"),
plotOutput("plot1", height = 300, brush = brushOpts(id = "plot1_brush", clip = TRUE, resetOnNew = TRUE)),
p(),
# Brushed points
"Brushed points informations, scale from 0 to 1",
verbatimTextOutput("brush1")
),
column(6,
# My plot rendering without print or plot
h4("Plot with a return variable"),
plotOutput("plot2", height = 300, brush = brushOpts(id = "plot2_brush", clip = TRUE, resetOnNew = TRUE)),
p(),
# Brushed points
"Brushed points informations, scale according to x and y variables",
verbatimTextOutput("brush2")
)
)
)
server <- function(input, output) {
data <- iris
# Plot1 I render with print or plot
output$plot1 <- renderPlot({
gg <- ggplot(data, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_point()
plot(gg)
})
# Brush points from plot1
output$brush1 <- renderPrint({
input$plot1_brush
})
# Plot2 I render just returning the variable
output$plot2 <- renderPlot({
gg <- ggplot(data, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_point()
return(gg)
})
# Brush points from plot2
output$brush2 <- renderPrint({
input$plot2_brush
})
}
shinyApp(ui = ui, server = server)
2。您问题中的可重现示例
下面我使用 iris
数据集做了一个可重现的例子。
另外,由于口音,我更改了一些字符。
ui.R
library(shiny)
library(ggplot2)
shinyUI(fluidPage(
titlePanel("MGM"),
sidebarLayout(
sidebarPanel(
uiOutput("plots_parameters")
),
mainPanel(
fluidRow(
column(12,
h4("Plot without zoom"),
plotOutput("Disp", height = 300, brush = brushOpts(id = "Disp_brush", clip = TRUE, resetOnNew = TRUE))
)
),
fluidRow(
column(12,
h4("Zoomed plot"),
plotOutput("DispZoom", height = 300)
)
)
)
)
))
server.R
library(shiny)
library(ggplot2)
base = iris
shinyServer(function(input, output) {
output$plots_parameters <- renderUI({
tipos <- c("Dispersao", "Histograma", "Boxplot", "Área")
choices <- colnames(base)
div(
selectInput("selectedColX", "Select colum for X axis", choices = choices, selected = "Sepal.Length"),
selectInput("selectedColY", "Select colum for Y axis", choices = choices, selected = "Petal.Length"),
selectInput("selectedColor", "Select colum for colour axis", choices = choices, selected = "Species"),
selectInput("seletedGraph", "Select type of graph", choices = tipos, selected = "Dispersao")
)
})
output$Disp <- renderPlot({
req(input$seletedGraph == "Dispersao")
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_point()
# Return variable without print or plot
gg
})
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$DispZoom <- renderPlot({
req(input$seletedGraph == "Dispersao")
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) + geom_point() +
coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
# Return variable without print or plot
gg
})
observe({
brush <- input$Disp_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- NULL
ranges2$y <- NULL
}
})
})
我是这里的新用户,但我疯狂地搜索了我在 Rstudio 中尝试创建带有 shiny 的数据可视化应用程序时遇到的问题。
问题是,我想读取一个 .csv,了解它的列,select 我希望将列作为 x 轴和 y 轴,用我选择的图表类型绘制它们并能够随时放大第二个情节。
我快到了,问题是我尝试做的带画笔的缩放不能正常工作。它不能正确理解轴的值,而是好像两个轴都只从 0 到 1,然后以正确的方式放大,但 xlim 和 ylim 错误。
这是我的 ui.R:
library(shiny)
library(ggplot2)
base = read.csv("TESTE.csv", sep = ";")
tipos <- c("Dispersão", "Histograma", "Boxplot", "Área")
shinyUI(fluidPage(
titlePanel("MGM"),
sidebarLayout(
sidebarPanel(
selectInput("selectedColX", "Select colum for X axis", choices = colnames(base), selected = colnames(base)[7]),
selectInput("selectedColY", "Select colum for Y axis", choices = colnames(base), selected = colnames(base)[4]),
selectInput("selectedColor", "Select colum for colour axis", choices = colnames(base), selected = colnames(base)[6]),
selectInput("seletedGraph", "Select type of graph", choices = tipos, selected = tipos[1])
),
fluidRow(
column(width = 12, class = "well",
h4("Left plot controls right plot"),
fluidRow(
column(width = 10,
plotOutput("Disp", height = 300,
brush = brushOpts(
id = "Disp_brush",
clip = TRUE,
resetOnNew = TRUE
)
)
),
column(width = 10,
plotOutput("DispZoom", height = 300)
)
)
)
)
# mainPanel(
#
# plotOutput("Hist"),
# plotOutput("Box"),
# plotOutput("Ar")
# )
)
))
然后是我的 Server.R:
library(shiny)
library(ggplot2)
base = read.csv("TESTE.csv", sep = ";")
tipos <- c("Dispersão", "Histograma", "Boxplot", "Área")
shinyServer(function(input, output) {
output$Disp <- renderPlot({
validate(need(input$seletedGraph=="Dispersão", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_point()
plot(gg)
})
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$DispZoom <- renderPlot({
validate(need(input$seletedGraph=="Dispersão", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) + geom_point() + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
plot(gg)
})
output$Hist <- renderPlot({
validate(need(input$seletedGraph=="Histograma", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis))
gg <- gg + geom_histogram()
gg
})
output$Box <- renderPlot({
validate(need(input$seletedGraph=="Boxplot", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_boxplot()
gg
})
output$Ar <- renderPlot({
validate(need(input$seletedGraph=="Área", message=FALSE))
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_area()
gg
})
observe({
brush <- input$Disp_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- NULL
ranges2$y <- NULL
}
})
})
忽略其他不属于geom_point的地块。一旦我让这个工作,其他的应该工作得很好,我想...
非常感谢,我正在努力解决这个问题! 有些文字是葡萄牙语,但我觉得一切都很好理解。
您的刷点在 brushOpts
中从 0 到 1 不等,因为您 print
或 plot
您的变量而不是仅仅返回它。
1.简短演示
这个简短的应用程序根据返回方式显示了刷点比例之间的差异。
library(shiny)
ui <- fluidPage(
fluidRow(
column(6,
# My plot rendering with print or plot
h4("Plot with print or plot variable"),
plotOutput("plot1", height = 300, brush = brushOpts(id = "plot1_brush", clip = TRUE, resetOnNew = TRUE)),
p(),
# Brushed points
"Brushed points informations, scale from 0 to 1",
verbatimTextOutput("brush1")
),
column(6,
# My plot rendering without print or plot
h4("Plot with a return variable"),
plotOutput("plot2", height = 300, brush = brushOpts(id = "plot2_brush", clip = TRUE, resetOnNew = TRUE)),
p(),
# Brushed points
"Brushed points informations, scale according to x and y variables",
verbatimTextOutput("brush2")
)
)
)
server <- function(input, output) {
data <- iris
# Plot1 I render with print or plot
output$plot1 <- renderPlot({
gg <- ggplot(data, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_point()
plot(gg)
})
# Brush points from plot1
output$brush1 <- renderPrint({
input$plot1_brush
})
# Plot2 I render just returning the variable
output$plot2 <- renderPlot({
gg <- ggplot(data, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_point()
return(gg)
})
# Brush points from plot2
output$brush2 <- renderPrint({
input$plot2_brush
})
}
shinyApp(ui = ui, server = server)
2。您问题中的可重现示例
下面我使用 iris
数据集做了一个可重现的例子。
另外,由于口音,我更改了一些字符。
ui.R
library(shiny)
library(ggplot2)
shinyUI(fluidPage(
titlePanel("MGM"),
sidebarLayout(
sidebarPanel(
uiOutput("plots_parameters")
),
mainPanel(
fluidRow(
column(12,
h4("Plot without zoom"),
plotOutput("Disp", height = 300, brush = brushOpts(id = "Disp_brush", clip = TRUE, resetOnNew = TRUE))
)
),
fluidRow(
column(12,
h4("Zoomed plot"),
plotOutput("DispZoom", height = 300)
)
)
)
)
))
server.R
library(shiny)
library(ggplot2)
base = iris
shinyServer(function(input, output) {
output$plots_parameters <- renderUI({
tipos <- c("Dispersao", "Histograma", "Boxplot", "Área")
choices <- colnames(base)
div(
selectInput("selectedColX", "Select colum for X axis", choices = choices, selected = "Sepal.Length"),
selectInput("selectedColY", "Select colum for Y axis", choices = choices, selected = "Petal.Length"),
selectInput("selectedColor", "Select colum for colour axis", choices = choices, selected = "Species"),
selectInput("seletedGraph", "Select type of graph", choices = tipos, selected = "Dispersao")
)
})
output$Disp <- renderPlot({
req(input$seletedGraph == "Dispersao")
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis))
gg <- gg + geom_point()
# Return variable without print or plot
gg
})
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$DispZoom <- renderPlot({
req(input$seletedGraph == "Dispersao")
y_axis <- input$selectedColY
x_axis <- input$selectedColX
color_axis <- input$selectedColor
gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) + geom_point() +
coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
# Return variable without print or plot
gg
})
observe({
brush <- input$Disp_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- NULL
ranges2$y <- NULL
}
})
})