在闪亮的 ggplot2 图形上输入相关结果
Inputting results of a correlation on ggplot2 figure in shiny
我正在尝试在闪亮的应用程序中显示 ggplot2 图的相关结果。我在这个例子中使用 iris 数据集。具体来说,我想要 x
和 y
输入调用的两个变量的相关性。而且我可以准确地弄清楚我做错了什么。我不断收到错误消息:
Warning: Error in cor.test.default: not enough finite observations
但是,当我尝试评估闪亮应用程序之外的关系时,我知道情况并非如此:
cor.test(iris$Sepal.Width, iris$Petal.Width, alternative = "two.sided", method="spearman")
我已将我的 cor.test
调用包装在 reactive
函数中,我正在尝试使用 geom_text
显示元素。下面是我的 ui.R 和 server.R 代码。
谁能看出我做错了什么?
ui.R
library(shiny)
library(ggplot2)
library(dplyr)
dataset <- iris
shinyUI(pageWithSidebar(
headerPanel("Iris Data Explore"),
sidebarPanel(
selectInput('x', 'X', names(dataset), names(dataset)[[2]]),
selectInput('y', 'Y', names(dataset), names(dataset)[[4]]),
selectInput('species', 'Species', levels(dataset$Species), "virginica"),
selectInput('color', 'Color', c('None', names(dataset))),
checkboxInput('smooth', 'Smooth'),
checkboxInput('line', 'Line')
),
mainPanel(
plotOutput('plot')
)
))
server.R
library(shiny)
library(ggplot2)
library(dplyr)
shinyServer(function(input, output) {
dataset <- reactive({
filter(iris,Species==input$species)
})
cordf <- reactive({
cor.test(as.numeric(input$x), as.numeric(input$y), alternative = "two.sided", method="spearman")
})
output$plot <- renderPlot({
p <- ggplot(dataset(), aes_string(x=paste0("`",input$x,"`"),
y=paste0("`",input$y,"`")
)) +
geom_point() +
geom_text(data=cordf(), aes(x=mean(input$x), y=mean(input$y), label=paste0(cordf$estimate))) +
ggtitle(paste0(input$species))
if (input$color != 'None')
p <- p + aes_string(color=input$color)
if (input$smooth)
p <- p + geom_smooth(aes(group=1))
if (input$line)
p <- p + geom_line(aes(group=1), colour='seagreen')
print(p)
}, height=500)
})
问题是 dataset
甚至没有在您的函数 cordf()
中定义,因此出现错误 "not enough finite observation"。
另一个问题是你不能只做 mean(input$x)
因为 input$x
只是一些像 Sepal.Length
这样的字符串。我还认为你想要 annotate
而不是 geom_text
。
修改后的server.R
如下。我不熟悉 shiny,所以不确定这样做是否是最佳做法。
server.R
library(shiny)
library(ggplot2)
library(dplyr)
shinyServer(function(input, output) {
dataset <- reactive({
filter(iris,Species==input$species)
})
cordf <- reactive({
dataset<-dataset();
cor.test(as.numeric(dataset[,input$x]), as.numeric(dataset[,input$y]), alternative = "two.sided", method="spearman")
})
output$plot <- renderPlot({
dataset<-dataset()
p <- ggplot(dataset, aes_string(x=paste0("`",input$x,"`"),
y=paste0("`",input$y,"`")
)) +
geom_point() +
annotate("text", x=mean(dataset[,input$x]),y=mean(dataset[,input$y]), label=cordf()$estimate) +
ggtitle(paste0(input$species))
if (input$color != 'None')
p <- p + aes_string(color=input$color)
if (input$smooth)
p <- p + geom_smooth(aes(group=1))
if (input$line)
p <- p + geom_line(aes(group=1), colour='seagreen')
print(p)
}, height=500)
})
我正在尝试在闪亮的应用程序中显示 ggplot2 图的相关结果。我在这个例子中使用 iris 数据集。具体来说,我想要 x
和 y
输入调用的两个变量的相关性。而且我可以准确地弄清楚我做错了什么。我不断收到错误消息:
Warning: Error in cor.test.default: not enough finite observations
但是,当我尝试评估闪亮应用程序之外的关系时,我知道情况并非如此:
cor.test(iris$Sepal.Width, iris$Petal.Width, alternative = "two.sided", method="spearman")
我已将我的 cor.test
调用包装在 reactive
函数中,我正在尝试使用 geom_text
显示元素。下面是我的 ui.R 和 server.R 代码。
谁能看出我做错了什么?
ui.R
library(shiny)
library(ggplot2)
library(dplyr)
dataset <- iris
shinyUI(pageWithSidebar(
headerPanel("Iris Data Explore"),
sidebarPanel(
selectInput('x', 'X', names(dataset), names(dataset)[[2]]),
selectInput('y', 'Y', names(dataset), names(dataset)[[4]]),
selectInput('species', 'Species', levels(dataset$Species), "virginica"),
selectInput('color', 'Color', c('None', names(dataset))),
checkboxInput('smooth', 'Smooth'),
checkboxInput('line', 'Line')
),
mainPanel(
plotOutput('plot')
)
))
server.R
library(shiny)
library(ggplot2)
library(dplyr)
shinyServer(function(input, output) {
dataset <- reactive({
filter(iris,Species==input$species)
})
cordf <- reactive({
cor.test(as.numeric(input$x), as.numeric(input$y), alternative = "two.sided", method="spearman")
})
output$plot <- renderPlot({
p <- ggplot(dataset(), aes_string(x=paste0("`",input$x,"`"),
y=paste0("`",input$y,"`")
)) +
geom_point() +
geom_text(data=cordf(), aes(x=mean(input$x), y=mean(input$y), label=paste0(cordf$estimate))) +
ggtitle(paste0(input$species))
if (input$color != 'None')
p <- p + aes_string(color=input$color)
if (input$smooth)
p <- p + geom_smooth(aes(group=1))
if (input$line)
p <- p + geom_line(aes(group=1), colour='seagreen')
print(p)
}, height=500)
})
问题是 dataset
甚至没有在您的函数 cordf()
中定义,因此出现错误 "not enough finite observation"。
另一个问题是你不能只做 mean(input$x)
因为 input$x
只是一些像 Sepal.Length
这样的字符串。我还认为你想要 annotate
而不是 geom_text
。
修改后的server.R
如下。我不熟悉 shiny,所以不确定这样做是否是最佳做法。
server.R
library(shiny)
library(ggplot2)
library(dplyr)
shinyServer(function(input, output) {
dataset <- reactive({
filter(iris,Species==input$species)
})
cordf <- reactive({
dataset<-dataset();
cor.test(as.numeric(dataset[,input$x]), as.numeric(dataset[,input$y]), alternative = "two.sided", method="spearman")
})
output$plot <- renderPlot({
dataset<-dataset()
p <- ggplot(dataset, aes_string(x=paste0("`",input$x,"`"),
y=paste0("`",input$y,"`")
)) +
geom_point() +
annotate("text", x=mean(dataset[,input$x]),y=mean(dataset[,input$y]), label=cordf()$estimate) +
ggtitle(paste0(input$species))
if (input$color != 'None')
p <- p + aes_string(color=input$color)
if (input$smooth)
p <- p + geom_smooth(aes(group=1))
if (input$line)
p <- p + geom_line(aes(group=1), colour='seagreen')
print(p)
}, height=500)
})