使用 ggplot2 在 shiny 中绘制基因 FPKM 归一化计数值
Plot the gene FPKM normalized counts value with ggplot2 in shiny
我只想绘制一个图像,x 命名为不同的样本,y 命名为不同的基因符号。我还想在 shiny.
中使用 ggplot2 添加 geom_errorbar
我希望如果我输入一个基因符号,旁边就会出现情节。
但是试了几次不知道为什么不显示
有两个输入files.One是每个样本的平均值,另一个是sd值文件。
我的代码示例是这样的:
library(shiny)
library(ggplot2)
library(ggpubr)
mean_data<-data.frame(Name=c(paste0("Group_",LETTERS[1:20])),
matx<-matrix(sample(1:1000,1000,replace = T),nrow = 20)
)
names(mean_data)[-1]<-c(paste0("Gene_",1:50))
sd_data<-data.frame(Name=c(paste0("Group_",LETTERS[1:20])),
matx<-matrix(runif(1000,5,10),nrow = 20)
)
names(sd_data)[-1]<-c(paste0("Gene_",1:50))
# Define UI for app that draws a histogram ----
ui <- fluidPage(
h4("Gene_FPKM Value Barplot"),
br(),
sidebarLayout(
sidebarPanel(
textInput(inputId = "GeneSymbol",
label = "Input your Gene Symbol:",
value = "", width = NULL, placeholder = 'e.g. Igfbp7,Zzz3'
),
actionButton("button", "show")
),
# Main panel for displaying outputs ----
mainPanel(
plotOutput(outputId = "barplot")
)
)
)
server <- function(input, output) {
gene <- reactive({
gene<-input$GeneSymbol
})
observeEvent(input$button, {
cat("Showing", input$GeneSymbol)
})
p <- reactive({ggplot(data=mean_data,aes_string(x=mean_data$Name,y=mean_data$input$GeneSymbol,fill=randomColor(74)))+
geom_bar(stat='identity',position=position_dodge(0.5),width=0.9)+
geom_errorbar(aes(ymin = mean_data$input$GeneSymbol-totalsd$input$GeneSymbol, ymax = mean_data$input$GeneSymbol+totalsd$input$GeneSymbol),width=.2)+
theme_classic2()+
rotate_x_text(angle = 45)+
theme(legend.position = "none")+
labs(title=input$GeneSymbol,x=NULL,y="FPKM_value")+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.margin = unit(c(20,1,1,1), "mm"))
})
output$barplot <- renderPlot({
print(p())
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
我知道有很多错误代码。
我是闪亮的新手。
请帮助这个child。
非常感谢!!
代码中有几个错误。但是在我看来,您的代码可以通过在开始时重塑数据来简化并避免错误:
- 使用例如将您的均值和标准差绑定在一个数据框中
dplyr::bind_rows
- 使用
tidyr::pivot_longer/wider
重塑您的数据集。
这样做之后,我们最终得到一个包含四列的数据框,即 Name
(示例?)、Gene
、mean
和 sd
。
仅此一项就简化了绘图代码并避免了所有错误,因为您可以引用固定的列名。
为了仅显示 selected 基因,我添加了反应性 plot_data
以相应地过滤数据。
除了文本输入外,我还添加了 selectInput
到 select 所需的基因
library(shiny)
library(ggplot2)
library(ggpubr)
library(dplyr)
library(tidyr)
mean_data <- data.frame(
Name = c(paste0("Group_", LETTERS[1:20])),
matx <- matrix(sample(1:1000, 1000, replace = T), nrow = 20)
)
names(mean_data)[-1] <- c(paste0("Gene_", 1:50))
sd_data <- data.frame(
Name = c(paste0("Group_", LETTERS[1:20])),
matx <- matrix(runif(1000, 5, 10), nrow = 20)
)
names(sd_data)[-1] <- c(paste0("Gene_", 1:50))
# Prepare dataset.
# 1. Bind mean and sd data
# 2. Reshape
data <- dplyr::bind_rows(list(
mean = mean_data,
sd = sd_data
), .id = "stat")
data_long <- data %>%
tidyr::pivot_longer(-c(Name, stat), names_to = "Gene", values_to = "value") %>%
tidyr::pivot_wider(names_from = "stat", values_from = "value")
# Define UI for app that draws a histogram ----
ui <- fluidPage(
h4("Gene_FPKM Value Barplot"),
br(),
sidebarLayout(
sidebarPanel(
textInput(
inputId = "GeneSymbol",
label = "Input your Gene Symbol:",
value = "", width = NULL, placeholder = "e.g. Igfbp7,Zzz3"
),
selectInput(
"slctGeneSymbol",
"Select Gene Symbols:",
choices = unique(data_long$Gene),
multiple = TRUE
)
#actionButton("button", "show")
),
# Main panel for displaying outputs ----
mainPanel(
plotOutput(outputId = "barplot")
)
)
)
server <- function(input, output) {
observeEvent(input$button, {
cat("Showing", input$GeneSymbol)
})
plot_data <- reactive({
subset(data_long, Gene %in% input$slctGeneSymbol)
})
output$barplot <- renderPlot({
ggplot(data = plot_data(), aes(x = Name, y = mean, fill = Gene)) +
geom_bar(stat = "identity", position = position_dodge(0.9), width = 0.9) +
geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = .2, position = position_dodge(0.9)) +
theme_classic2() +
rotate_x_text(angle = 45) +
theme(legend.position = "none") +
labs(title = input$GeneSymbol, x = NULL, y = "FPKM_value") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.margin = unit(c(20, 1, 1, 1), "mm"))
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
我只想绘制一个图像,x 命名为不同的样本,y 命名为不同的基因符号。我还想在 shiny.
中使用 ggplot2 添加 geom_errorbar我希望如果我输入一个基因符号,旁边就会出现情节。
但是试了几次不知道为什么不显示
有两个输入files.One是每个样本的平均值,另一个是sd值文件。
我的代码示例是这样的:
library(shiny)
library(ggplot2)
library(ggpubr)
mean_data<-data.frame(Name=c(paste0("Group_",LETTERS[1:20])),
matx<-matrix(sample(1:1000,1000,replace = T),nrow = 20)
)
names(mean_data)[-1]<-c(paste0("Gene_",1:50))
sd_data<-data.frame(Name=c(paste0("Group_",LETTERS[1:20])),
matx<-matrix(runif(1000,5,10),nrow = 20)
)
names(sd_data)[-1]<-c(paste0("Gene_",1:50))
# Define UI for app that draws a histogram ----
ui <- fluidPage(
h4("Gene_FPKM Value Barplot"),
br(),
sidebarLayout(
sidebarPanel(
textInput(inputId = "GeneSymbol",
label = "Input your Gene Symbol:",
value = "", width = NULL, placeholder = 'e.g. Igfbp7,Zzz3'
),
actionButton("button", "show")
),
# Main panel for displaying outputs ----
mainPanel(
plotOutput(outputId = "barplot")
)
)
)
server <- function(input, output) {
gene <- reactive({
gene<-input$GeneSymbol
})
observeEvent(input$button, {
cat("Showing", input$GeneSymbol)
})
p <- reactive({ggplot(data=mean_data,aes_string(x=mean_data$Name,y=mean_data$input$GeneSymbol,fill=randomColor(74)))+
geom_bar(stat='identity',position=position_dodge(0.5),width=0.9)+
geom_errorbar(aes(ymin = mean_data$input$GeneSymbol-totalsd$input$GeneSymbol, ymax = mean_data$input$GeneSymbol+totalsd$input$GeneSymbol),width=.2)+
theme_classic2()+
rotate_x_text(angle = 45)+
theme(legend.position = "none")+
labs(title=input$GeneSymbol,x=NULL,y="FPKM_value")+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.margin = unit(c(20,1,1,1), "mm"))
})
output$barplot <- renderPlot({
print(p())
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
我知道有很多错误代码。 我是闪亮的新手。 请帮助这个child。 非常感谢!!
代码中有几个错误。但是在我看来,您的代码可以通过在开始时重塑数据来简化并避免错误:
- 使用例如将您的均值和标准差绑定在一个数据框中
dplyr::bind_rows
- 使用
tidyr::pivot_longer/wider
重塑您的数据集。
这样做之后,我们最终得到一个包含四列的数据框,即 Name
(示例?)、Gene
、mean
和 sd
。
仅此一项就简化了绘图代码并避免了所有错误,因为您可以引用固定的列名。
为了仅显示 selected 基因,我添加了反应性
plot_data
以相应地过滤数据。除了文本输入外,我还添加了
selectInput
到 select 所需的基因
library(shiny)
library(ggplot2)
library(ggpubr)
library(dplyr)
library(tidyr)
mean_data <- data.frame(
Name = c(paste0("Group_", LETTERS[1:20])),
matx <- matrix(sample(1:1000, 1000, replace = T), nrow = 20)
)
names(mean_data)[-1] <- c(paste0("Gene_", 1:50))
sd_data <- data.frame(
Name = c(paste0("Group_", LETTERS[1:20])),
matx <- matrix(runif(1000, 5, 10), nrow = 20)
)
names(sd_data)[-1] <- c(paste0("Gene_", 1:50))
# Prepare dataset.
# 1. Bind mean and sd data
# 2. Reshape
data <- dplyr::bind_rows(list(
mean = mean_data,
sd = sd_data
), .id = "stat")
data_long <- data %>%
tidyr::pivot_longer(-c(Name, stat), names_to = "Gene", values_to = "value") %>%
tidyr::pivot_wider(names_from = "stat", values_from = "value")
# Define UI for app that draws a histogram ----
ui <- fluidPage(
h4("Gene_FPKM Value Barplot"),
br(),
sidebarLayout(
sidebarPanel(
textInput(
inputId = "GeneSymbol",
label = "Input your Gene Symbol:",
value = "", width = NULL, placeholder = "e.g. Igfbp7,Zzz3"
),
selectInput(
"slctGeneSymbol",
"Select Gene Symbols:",
choices = unique(data_long$Gene),
multiple = TRUE
)
#actionButton("button", "show")
),
# Main panel for displaying outputs ----
mainPanel(
plotOutput(outputId = "barplot")
)
)
)
server <- function(input, output) {
observeEvent(input$button, {
cat("Showing", input$GeneSymbol)
})
plot_data <- reactive({
subset(data_long, Gene %in% input$slctGeneSymbol)
})
output$barplot <- renderPlot({
ggplot(data = plot_data(), aes(x = Name, y = mean, fill = Gene)) +
geom_bar(stat = "identity", position = position_dodge(0.9), width = 0.9) +
geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = .2, position = position_dodge(0.9)) +
theme_classic2() +
rotate_x_text(angle = 45) +
theme(legend.position = "none") +
labs(title = input$GeneSymbol, x = NULL, y = "FPKM_value") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.margin = unit(c(20, 1, 1, 1), "mm"))
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)