【发布时间】:2021-03-05 13:38:54
【问题描述】:
我正在尝试在 R 中使用 ggplot 和 Shiny 制作热图,但该图未按预期显示。我使用的数据结构与 Shiny-RStudio 示例不同,也许这就是我没有得到好的结果的原因。
我的数据表包含 152013 行和 7 个变量(区域、季节、周、fishingfleet、长度、类别、vul1.indicator)。我想用 x=week 和 y=zone 制作变量“vul1.indicator”的热图,闪亮的应用程序用户可以选择类别和季节。我的问题是,当我在侧边栏中更改“季节”和“类别”的选择时,只有图例发生了变化,情节没有变化。情节部分和选择之间似乎没有联系。我不知道我在哪里弄错了。你能帮帮我吗?
这是我的表格示例(“类别”有 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)
谢谢!
【问题讨论】: