ggplot2 - 在 Shiny 中绘制两个数据框中的列
问题描述
我想针对 df2.t 中的列绘制 df1.t 中的列。以前我只有一个数据框,但现在我将数据拆分为两个数据框。我不确定如何server
正确完成从不同数据框中选择列。请看我下面的例子
> dput(df1.t[1:5,][,1:5])
structure(list(`hsa-let-7a-3p` = c(5.58182112427671, 5.21705272399953,
5.42864356356758, -1.1383057411356, 5.06203248358181), `hsa-let-7a-5p` = c(17.0260439263402,
15.2857710138151, 17.1420214989373, 15.1034766165351, 14.5449390552056
), `hsa-let-7b-3p` = c(4.28580929310353, 2.46805733598209, 5.15298557165018,
4.63298501632773, -0.398732335974934), `hsa-let-7b-5p` = c(13.0477955183433,
10.880357260433, 12.2652935281359, 11.1312184397251, 7.45844929748327
), `hsa-let-7c-5p` = c(12.5551466619424, 9.6650262124332, 12.1037832874061,
9.557036296907, 9.92698639082262)), class = "data.frame", row.names = c("86",
"175", "217", "394", "444"))
> dput(df2.t[1:5,][,1:5])
structure(list(TSPAN6 = c(-0.828323126096606, -3.10305950914023,
1.29283167997387, 1.45789964523008, 2.5865078028694), TNMD = c(-3.10305950914023,
-2.24464338564074, -3.10305950914023, -2.40005739936056, -3.10305950914023
), DPM1 = c(5.28259829784066, 4.78188654848771, 4.64737618644607,
5.30924702614244, 5.31267531367151), SCYL3 = c(2.87362293573059,
4.28995424523396, 1.90557669028164, 3.40137165784651, 2.31237762728826
), C1orf112 = c(1.06700120906004, 4.32783509690622, 0.330332820167606,
0.442181000111075, 2.50079103019751)), class = "data.frame", row.names = c("86",
"175", "217", "394", "444"))
ui <- fluidPage(
mainPanel(
plotOutput("plot")
),
selectInput(inputId ="data1",
label = "Choose miRNA",
choices = names(df1.t),
selected = NULL
),
selectInput(inputId ="data2",
label = "choose Gene",
choices = names(df2.t),
selected = NULL
),
textOutput("result"))
server <- function(input,output){
library(ggplot2)
output$plot <- renderPlot({
data <- plot[, c(input$data1, input$data2)] #Needs edit
colnames(data) <- c("col1", "col2") #Needs edit
ggplot(data,aes(x=col1,y=col2)) +
geom_point(colour='black') +
labs(x = input$data1, y = input$data2) +
theme_classic(base_size = 8) +
geom_smooth(method="lm",se = F) +
stat_cor()
}, height = 400, width = 600)
}
解决方案
这应该有效:
library(shiny)
library(ggplot2)
df1.t <- structure(list(`hsa-let-7a-3p` = c(5.58182112427671, 5.21705272399953,
5.42864356356758, -1.1383057411356, 5.06203248358181), `hsa-let-7a-5p` = c(17.0260439263402,
15.2857710138151, 17.1420214989373, 15.1034766165351, 14.5449390552056
), `hsa-let-7b-3p` = c(4.28580929310353, 2.46805733598209, 5.15298557165018,
4.63298501632773, -0.398732335974934), `hsa-let-7b-5p` = c(13.0477955183433,
10.880357260433, 12.2652935281359, 11.1312184397251, 7.45844929748327
), `hsa-let-7c-5p` = c(12.5551466619424, 9.6650262124332, 12.1037832874061,
9.557036296907, 9.92698639082262)), class = "data.frame", row.names = c("86",
"175", "217", "394", "444"))
df2.t <- structure(list(TSPAN6 = c(-0.828323126096606, -3.10305950914023,
1.29283167997387, 1.45789964523008, 2.5865078028694), TNMD = c(-3.10305950914023,
-2.24464338564074, -3.10305950914023, -2.40005739936056, -3.10305950914023
), DPM1 = c(5.28259829784066, 4.78188654848771, 4.64737618644607,
5.30924702614244, 5.31267531367151), SCYL3 = c(2.87362293573059,
4.28995424523396, 1.90557669028164, 3.40137165784651, 2.31237762728826
), C1orf112 = c(1.06700120906004, 4.32783509690622, 0.330332820167606,
0.442181000111075, 2.50079103019751)), class = "data.frame", row.names = c("86",
"175", "217", "394", "444"))
ui <- fluidPage(
mainPanel(
plotOutput("plot")
),
selectInput(inputId ="data1",
label = "Choose miRNA",
choices = names(df1.t),
selected = NULL
),
selectInput(inputId ="data2",
label = "choose Gene",
choices = names(df2.t),
selected = NULL
),
textOutput("result"))
server <- function(input,output){
data <- eventReactive(c(input$data1,input$data2),{
data <- data.frame(df1.t[[input$data1]], df2.t[[input$data2]])
colnames(data) <- c("col1", "col2")
data
})
output$plot <- renderPlot({
ggplot(data(),aes(x=col1,y=col2)) +
geom_point(colour='black') +
labs(x = input$data1, y = input$data2) +
theme_classic(base_size = 8) +
geom_smooth(method="lm",se = F)
}, height = 400, width = 600)
}
shinyApp(ui, server)
推荐阅读
- mongodb - Mongoose 从多个文档更新多个子文档
- java - 如何使用 WebView 为 onClickListener 设置线性布局
- intellij-idea - 如果已通过 JetBrains Toolbox 安装 IntelliJ IDEA,如何在 Windows 上下文菜单中提供“在 IntelliJ IDEA 中打开此项目”选项?
- gcloud - 无法下载谷歌凭据 json 文件
- javascript - 如何使用 ES6 HOF Javascript 在数组中查找重复对象(所有键值对都应该相同)
- python - 在 Python 中使用不同的符号预处理文本数据
- amazon-web-services - 如何使用 AWS Lambda 捕获无服务器网站截图?
- asp.net-core - Swagger / Swashbuckle 参考循环问题
- c++ - 如何调用指针类型的方法(禁用多态)?
- javascript - 将 mp4 视频数组缓冲区添加到媒体源后,html 视频播放器冻结