我制作了两个代码在Shiny / R中运行但是当我运行它们并通过滑动条来改变增长和成本的参数时,视觉上图形似乎没有改变。它确实发生了变化,因为轴上的数字改变了,但我宁愿保持数字(图的比例)固定,让图线移动。任何人都可以帮我修改图表的比例吗?
谢天谢地。
library(shiny)
ui <- fluidPage(titlePanel("Kost vrijwillige oppashulp"),
sidebarLayout(
sidebarPanel(
helpText("Kies een waarde voor de groei"),
sliderInput(
"growth",
"groei:",
min = 0.01,
max = 1,
value = 0.025,
step = 0.005
),
helpText("Kies een waarde voor de kostprijs"),
sliderInput(
"price",
"kostprijs:",
min = 1,
max = 3,
value = 1.85,
step = 0.05
)
),
mainPanel(plotOutput("graph"))
))
server <- function(input, output) {
output$graph <- renderPlot({
year <- 2016:2025
growth <- 0.025
price <- 1.85
costcentra <- 33523.25
totalhours <- c(1119784, rep(NA, 9))
for (i in 1:9) {
totalhours[i + 1] <- totalhours[i] + totalhours[i] * input$growth
}
cost <- totalhours * input$price
centraOH <- c(53, rep(NA, 9))
for (i in 1:9) {
centraOH[i + 1] <- centraOH[i] + 2
}
totalcostcentra <- centraOH * costcentra
totalcostOH <- cost + totalcostcentra
plot(year,
cost,
type = "l",
col = "red",
lwd = 2)
# Create a title with a red, bold/italic font
title(main = "Cost of volunteer help",
col.main = "blue",
font.main = 4)
# title(xlab="Year", col.lab=rgb(0,0.5,0))
# title(ylab="Cost", col.lab=rgb(0,0.5,0))
})
}
# Run the application
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
您可以使用以下方式指定轴限制:
plot(year, cost, type="l", col="red", lwd=2,ylim=c(1000000,5000000))
轴限制现在将始终保持不变。但是,这意味着您的绘图不会始终适合绘图窗口。或者,您可以执行以下操作:
plot(year, cost, type="l", col="red", lwd=2,ylim=c(1000000,max(5000000, max(cost))))
这样限制将在1到5百万之间,除非最大值超过5百万,在这种情况下y轴将适应。希望这会有所帮助。