我和我的朋友正在用ggplot學習R的閃光點。我們發現,X軸的標簽超出了網格的范圍。
預先感謝。
uj5u.com熱心網友回復:
對于1)洗掉scale_x_discrete.
對于2)代替硬編碼min, max和value,你可以從資料中獲得這些值。
library(shiny)
library(shinydashboard)
library(dplyr)
library(tidyverse)
library(reshape)
library(scales)
ecd <- read.csv("ecd-figures.csv")
c()
"No of case" = "no_of_case",
"Minor Case" = "minor_case",
"All Non Fatal Case" = "all_non_fatal_case",
"致命案例" = "fatal_case"
) ->/span> vec
ui -<- fluidPage(/span>sidebarLayout()
側邊欄面板
()
checkboxGroupInput("feature",)
"特征",
vec),
sliderInput()
"year",
"年份",
min = min(ecd$年)。
max = max(ecd$年)。
值= 范圍(ecd$年)。
sep = "",
步驟 = 1
)
),
mainPanel(tabsetPanel()
tabPanel("Plot"。 plotOutput("correlation_plot")),/span>
tabPanel("Table"。 tableOutput("ecd"))
))
))
服務器 < - function(input,output) {>
yearrange <- reactive({)
ecd %> %子集(年%in%輸入$年[1]。 input$year[2]) %> %選擇(c(年份。 輸入$feature))
})
output$correlation_plot <- renderPlot({
ecdsubset <- yearrange()
ecdsubset < -融化(ecdsubset, id = "year")
validate(need(input$feature, '檢查至少一個專案。 '))
ggplot(ecdsubset。 aes(x=year,y=value, color=variable)) operator"> geom_line(size=1)
})
output$ecd <- renderTable({)
yearrange()
})
}
shinyApp(ui, server)。
uj5u.com熱心網友回復:
除了Ronak Shah的回答之外,把這個代碼放到yeararrange
mutate(across(where( is. numeric), as. 整數)) %>%
完整的代碼:
library(shiny)
library(shinydashboard)
library(dplyr)
library(tidyverse)
library(reshape)
library(scales)
ecd <- read.csv("ecd-figures.csv")
c()
"No of case" = "no_of_case",
"Minor Case" = "minor_case",
"All Non Fatal Case" = "all_non_fatal_case",
"致命案例" = "fatal_case"
) ->/span> vec
ui -<- fluidPage(/span>sidebarLayout()
側邊欄面板
()
checkboxGroupInput("feature",)
"特征",
vec),
sliderInput()
"year",
"年份",
min = min(ecd$年)。
max = max(ecd$年)。
range = c(min(ecd$year)。 max(ecd$年))。
sep="",
步驟 = 1
)
),
mainPanel(tabsetPanel()
tabPanel("Plot"。 plotOutput("correlation_plot")),/span>
tabPanel("Table"。 tableOutput("ecd"))
))
))
服務器 < - function(input,output) {>
yearrange <- reactive({)
ecd %>%
mutate(across(where( is. numeric), as. integer)) %>%
subset(年 %in%輸入$年[1]。 input$year[2]) %>。 %
select(c(年。 輸入$feature))
})
output$correlation_plot <- renderPlot({
ecdsubset <- yearrange()
ecdsubset < -融化(ecdsubset, id = "year")
validate(need(input$feature, '檢查至少一個專案。 '))
ggplot(ecdsubset。 aes(x=year,y=value, color=variable)) geom_line(size=1)
主題(legend.position = "none")
scale_x_discrete(limits=c(input$) operator">$年[1]。 input$year[/span>2]))
})
output$ecd <- renderTable({)
yearrange()
})
}
shinyApp(ui, server)。
uj5u.com熱心網友回復:
我最終采用了Joshua Cook的解決方案,即為標簽斷句撰寫一個額外的函式。完整的代碼:
library(shiny)
library(shinydashboard)
library(dplyr)
library(tidyverse)
library(reshape)
library(scales)
ecd <- read.csv("ecd-figures.csv")
c()
"No of case" = "no_of_case",
"Minor Case" = "minor_case",
"All Non Fatal Case" = "all_non_fatal_case",
"致命案例" = "fatal_case"
) ->/span> vec
ui -<- fluidPage(/span>sidebarLayout()
側邊欄面板
()
checkboxGroupInput("feature",)
"特征",
vec),
sliderInput()
"year",
"年份",
min = min(ecd$年)。
max = max(ecd$年)。
值= 范圍(ecd$年)。
sep = "",
步驟 = 1
)
),
mainPanel(tabsetPanel()
tabPanel("Plot"。 plotOutput("correlation_plot")),/span>
tabPanel("Table"。 tableOutput("ecd"))
))
))
服務器 < - function(input,output) {>
yearrange <- reactive({)
ecd %>%
subset(年%in%輸入$年[1]。 input$year[2]) %>。 %
select(c(年。 輸入$feature))
})
integer_breaks < - function(n = 5,. ...) {
fxn <- function(a) {>
突破< - floor(pretty(a, n,. ...))
names(breakaks) < - attr(breaks, "labs")
休息時間
}
return(fxn)
}
output$correlation_plot <- renderPlot({)
ecdsubset <- yearrange()
ecdsubset < -融化(ecdsubset, id = "year")
validate(need(input$feature, '檢查至少一個專案。 '))
ggplot(ecdsubset。 aes(x =年, y =價值。 color = variable)) ) 運算子"> geom_line(size = 1) scale_x_continuous(breaks= integer_breaks())
})
output$ecd <- renderTable({)
yearrange()
})
}
shinyApp(ui, server)。
我不明白的是,在integer_breaks <- function(n=5, ...) 中的...是什么意思?謝謝你的建議。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330212.html
標籤:
下一篇:在程式集中列印文本



