我正在尝试做一个热图,并在R中闪闪发亮,但是这个图并没有像我预期的那样显示。我使用的数据结构与闪亮的RStudio示例不同,这也许就是我没有好结果的原因。
我的数据表包含152013行和7个变量(区域,季节,周,捕鱼船队,长度,类别,外阴1.指示器)。我想用x=week和y=zone制作一个变量“vul1.dicator”的热图,而闪亮的应用程序用户可以选择类别和季节。我的问题是,当我改变“季节”和“类别”在侧边栏中的选择,只有传奇变化,而不是情节。似乎在情节部分和选择之间没有任何联系。我不知道我犯了什么错。你能帮帮我吗?
这是我的表格的一个例子(我有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)谢谢!
发布于 2021-03-05 19:21:22
简单的回答是,在生成绘图时,您没有使用任何类型的反应性值,也就是说,当您更改您的选择时,它并没有改变绘图的计算方式。我的意思是,您甚至已经在代码中有一个注释部分,这段代码对于它的去向是空白的。
您需要在选择器更改时更新数据集的内容:
displayData <- reactive( <code to subset data> )然后在创建你的地块时使用这个
ggplot(data = displayData()...发布于 2021-03-08 15:30:23
谢谢马库斯!现在起作用了。
我使用了这个代码:
# 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(), ...https://stackoverflow.com/questions/66493596
复制相似问题