闪亮的初学者 - ggplot 的问题 geom_tile
Begginer in Shiny - Problems with ggplot geom_tile
我正在尝试在 R 中使用 ggplot 和 Shiny 制作热图,但该图并未按我预期显示。我使用的数据与 Shiny-RStudio 示例具有不同的结构,也许这就是为什么我没有得到好的结果。
我的数据 table 包含 152013 行和 7 个变量(区域、季节、周、捕鱼船队、长度、类别,vul1.indicator)。我想用 x=week 和 y=zone 制作变量“vul1.indicator”的热图,闪亮的应用程序用户可以选择类别和季节。我的问题是,当我在侧边栏中更改“季节”和“类别”的选择时,只有图例会发生变化,而情节不会发生变化。情节部分和选择之间似乎没有link。我不知道我哪里弄错了。你能帮帮我吗?
这是我的一个示例table(我有 10 个“类别”选项和 3 个“季节”选项:
> Vul1MoyZoneAgr
zone season week fishingfleet length category vul1.indicator
1 Z1 2012-2013 1 CAD T1 CAD-T1 1
2 Z1 2012-2013 2 CAD T1 CAD-T1 1
3 Z1 2012-2013 3 CAD T1 CAD-T1 1
4 Z1 2012-2013 4 CAD T1 CAD-T1 1
5 Z1 2012-2013 5 CAD T1 CAD-T1 1
6 Z1 2012-2013 6 CAD T1 CAD-T1 1
7 Z1 2012-2013 7 CAD T1 CAD-T1 1
8 Z1 2012-2013 46 CAD T1 CAD-T1 1
9 Z1 2012-2013 47 CAD T1 CAD-T1 1
10 Z1 2012-2013 48 CAD T1 CAD-T1 1
11 Z1 2012-2013 49 CAD T1 CAD-T1 1
12 Z1 2012-2013 50 CAD T1 CAD-T1 1
13 Z1 2012-2013 51 CAD T1 CAD-T1 1
14 Z1 2012-2013 52 CAD T1 CAD-T1 1
这是我的代码:
library(ggplot2)
Vul1MoyZoneAgr <- read.table("C:/Users/user/Documents/Vul1MoyZoneAgr.txt", sep="\t",quote="", dec=",", header = TRUE)
Vul1MoyZoneAgr$zone <- factor(Vul1MoyZoneAgr$zone, levels = c("Z1", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9", "Z10", "Z11", "Z12", "Z13", "Z14", "Z15", "ZI", "ZJ"))
Vul1MoyZoneAgr$week <- factor(Vul1MoyZoneAgr$week, levels = c("40","41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"))
# Define UI ----
ui <- fluidPage(
titlePanel("Vulnerability indicator"),
sidebarLayout(
sidebarPanel(
selectInput("category", "category:",
choices = c(unique(Vul1MoyZoneAgr$category))),
selectInput("season", "season:",
choices = c(unique(Vul1MoyZoneAgr$season))),
),
mainPanel(
# Output: Formatted text for caption ----
h3(textOutput("caption")),
# Output ----
plotOutput("vul1.indicatorPlot")
)
)
)
# Define server logic ----
server <- function(input, output) {
# Compute the formula text ----
# This is in a reactive expression since it is shared by the
# output$caption and output$vul1.indicatorPlot functions
formulaText <- reactive({
paste("vulnerability indicator ~", input$category, "~", input$season)
})
# Return the formula text for printing as a caption ----
output$caption <- renderText({
formulaText()
})
# Reactive expression for the data subsetted to what the user selected
# Generate a plot of the requested season and category ----
output$vul1.indicatorPlot <- renderPlot({
ggplot(as.formula(formulaText()),
data = Vul1MoyZoneAgr, mapping = aes(x=week, y=zone, fill=vul1.indicator)) + theme_bw() +
geom_tile(aes(fill = vul1.indicator), colour = "white") + scale_fill_gradient(low = "#F2E8EC", high = "#990000", limits=c(0, 7), breaks=seq(0,7,by=1)) +
labs(fill = "Vul. Ind.") +
theme(strip.background = element_rect(colour="black", fill="white"), axis.text.x = element_text(face= "bold", size = 10), axis.text.y = element_text(face= "bold",size = 10), axis.title.x = element_text(face= "bold",size = 12), axis.title.y = element_text(face= "bold",size = 12), strip.text.y = element_text(face= "bold",size = 10),strip.text.x = element_text(face= "bold",size = 12), legend.text = element_text(face= "bold",size = 12), legend.title = element_text(face= "bold",size = 12), legend.key.size = unit(1.5, "cm"), legend.key.width = unit(0.5,"cm"), panel.grid.major=element_line(colour = "white"))
})
}
shinyApp(ui, server)
谢谢!
简短的回答是您在生成绘图时没有使用任何类型的反应值,即当您更改选择时,它不会改变绘图的计算方式。我的意思是,您甚至已经在代码的注释部分留有空白,无法说明该代码的去向。
您需要像数据集一样的东西,当其中一个选择器发生变化时它会更新:
displayData <- reactive( <code to subset data> )
然后在创建情节时使用它
ggplot(data = displayData()...
谢谢马库斯!现在可以使用了。
我使用了这个代码:
# Reactive expression for the data subsetted to what the user selected
filteredData <- reactive({
req(input$category, input$season)
filter(Vul1MoyZoneAgr, category == input$category, season == input$season)
})
然后:
ggplot(data = filteredData(), ...
我正在尝试在 R 中使用 ggplot 和 Shiny 制作热图,但该图并未按我预期显示。我使用的数据与 Shiny-RStudio 示例具有不同的结构,也许这就是为什么我没有得到好的结果。
我的数据 table 包含 152013 行和 7 个变量(区域、季节、周、捕鱼船队、长度、类别,vul1.indicator)。我想用 x=week 和 y=zone 制作变量“vul1.indicator”的热图,闪亮的应用程序用户可以选择类别和季节。我的问题是,当我在侧边栏中更改“季节”和“类别”的选择时,只有图例会发生变化,而情节不会发生变化。情节部分和选择之间似乎没有link。我不知道我哪里弄错了。你能帮帮我吗?
这是我的一个示例table(我有 10 个“类别”选项和 3 个“季节”选项:
> Vul1MoyZoneAgr
zone season week fishingfleet length category vul1.indicator
1 Z1 2012-2013 1 CAD T1 CAD-T1 1
2 Z1 2012-2013 2 CAD T1 CAD-T1 1
3 Z1 2012-2013 3 CAD T1 CAD-T1 1
4 Z1 2012-2013 4 CAD T1 CAD-T1 1
5 Z1 2012-2013 5 CAD T1 CAD-T1 1
6 Z1 2012-2013 6 CAD T1 CAD-T1 1
7 Z1 2012-2013 7 CAD T1 CAD-T1 1
8 Z1 2012-2013 46 CAD T1 CAD-T1 1
9 Z1 2012-2013 47 CAD T1 CAD-T1 1
10 Z1 2012-2013 48 CAD T1 CAD-T1 1
11 Z1 2012-2013 49 CAD T1 CAD-T1 1
12 Z1 2012-2013 50 CAD T1 CAD-T1 1
13 Z1 2012-2013 51 CAD T1 CAD-T1 1
14 Z1 2012-2013 52 CAD T1 CAD-T1 1
这是我的代码:
library(ggplot2)
Vul1MoyZoneAgr <- read.table("C:/Users/user/Documents/Vul1MoyZoneAgr.txt", sep="\t",quote="", dec=",", header = TRUE)
Vul1MoyZoneAgr$zone <- factor(Vul1MoyZoneAgr$zone, levels = c("Z1", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9", "Z10", "Z11", "Z12", "Z13", "Z14", "Z15", "ZI", "ZJ"))
Vul1MoyZoneAgr$week <- factor(Vul1MoyZoneAgr$week, levels = c("40","41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"))
# Define UI ----
ui <- fluidPage(
titlePanel("Vulnerability indicator"),
sidebarLayout(
sidebarPanel(
selectInput("category", "category:",
choices = c(unique(Vul1MoyZoneAgr$category))),
selectInput("season", "season:",
choices = c(unique(Vul1MoyZoneAgr$season))),
),
mainPanel(
# Output: Formatted text for caption ----
h3(textOutput("caption")),
# Output ----
plotOutput("vul1.indicatorPlot")
)
)
)
# Define server logic ----
server <- function(input, output) {
# Compute the formula text ----
# This is in a reactive expression since it is shared by the
# output$caption and output$vul1.indicatorPlot functions
formulaText <- reactive({
paste("vulnerability indicator ~", input$category, "~", input$season)
})
# Return the formula text for printing as a caption ----
output$caption <- renderText({
formulaText()
})
# Reactive expression for the data subsetted to what the user selected
# Generate a plot of the requested season and category ----
output$vul1.indicatorPlot <- renderPlot({
ggplot(as.formula(formulaText()),
data = Vul1MoyZoneAgr, mapping = aes(x=week, y=zone, fill=vul1.indicator)) + theme_bw() +
geom_tile(aes(fill = vul1.indicator), colour = "white") + scale_fill_gradient(low = "#F2E8EC", high = "#990000", limits=c(0, 7), breaks=seq(0,7,by=1)) +
labs(fill = "Vul. Ind.") +
theme(strip.background = element_rect(colour="black", fill="white"), axis.text.x = element_text(face= "bold", size = 10), axis.text.y = element_text(face= "bold",size = 10), axis.title.x = element_text(face= "bold",size = 12), axis.title.y = element_text(face= "bold",size = 12), strip.text.y = element_text(face= "bold",size = 10),strip.text.x = element_text(face= "bold",size = 12), legend.text = element_text(face= "bold",size = 12), legend.title = element_text(face= "bold",size = 12), legend.key.size = unit(1.5, "cm"), legend.key.width = unit(0.5,"cm"), panel.grid.major=element_line(colour = "white"))
})
}
shinyApp(ui, server)
谢谢!
简短的回答是您在生成绘图时没有使用任何类型的反应值,即当您更改选择时,它不会改变绘图的计算方式。我的意思是,您甚至已经在代码的注释部分留有空白,无法说明该代码的去向。
您需要像数据集一样的东西,当其中一个选择器发生变化时它会更新:
displayData <- reactive( <code to subset data> )
然后在创建情节时使用它
ggplot(data = displayData()...
谢谢马库斯!现在可以使用了。
我使用了这个代码:
# Reactive expression for the data subsetted to what the user selected
filteredData <- reactive({
req(input$category, input$season)
filter(Vul1MoyZoneAgr, category == input$category, season == input$season)
})
然后:
ggplot(data = filteredData(), ...