This is my first post here on StackOverflow and I will try to be as precise as possible with my question.
I also want to point out that I have tried all solutions recommended in How to display only integer values on an axis using ggplot2 . Unfortunately, I could not solve the issue with any of them.
I have created a Shiny app that produces line graphs of annual data on a variety of variables. This works out nicely for most parameterizations:
No non-integer breaks
However, if I choose certain time spans on the slider, it produces graphs that have non-integer breaks on the x-axis, which makes no sense for a yearly data.
With non-integer breaks
Edit : Here a minimal reproducible version of the application
library(tidyverse)
library(shiny)
options(scipen = 999)
# Data
data1<-data.frame(values = c(15500,
16300,
18200,
28300,
23500,
23700,
31500,
35800,
34700,
36900,
40000,
44700,
53300,
55800,
69800,
89500,
1.13E+5,
1.53E+5,
1.77E+5,
1.83E+5,
1.99E+5),
year = seq(1990, 2010, 1))
#Shiny app
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("period", "Year:", min = 1990, max = 2010, value = c(1990, 2010), sep = "")),
mainPanel(plotOutput("ggplot2"))))
server <- function(input, output) {
data1_subset <- reactive({
filter(data1, year >= input$period[1] & year <= input$period[2])
})
output$ggplot2 <- renderPlot({
ggplot(data = data1_subset(), aes(x = year, y = values)) +
geom_line(aes(color = "red")) +
scale_x_continuous(name = "Year") +
scale_color_discrete(guide=FALSE)+
theme_minimal()
})
}
shinyApp(ui = ui, server = server)
To see the problem, select eg time span 2000-2010
Is there any way to suppress non-integer breaks as there are clearly nonsensical with annual data?
Thanks a lot in advance for your help!
You should take care to use the correct class for your date column year
(something like Date
or POSIXct
). As the comment suggests, you need to pick a particular date, like Jan 1st
. This will inform ggplot2
about what's probably your intention and it will make it easier for you to steer the display format to something meaningful.
It will work out of the box, you can tune it more with ggplot
's date/time scales .
The reprex with the presented idea implemented (and one more glitch regarding the line colour fixed):
library(tidyverse)
library(shiny)
# Data
tibble(values = c(15500,16300,18200,28300,23500,23700,
31500,35800,34700,36900,40000,44700,
53300,55800,69800,89500,1.13E+5,
1.53E+5,1.77E+5,1.83E+5,1.99E+5),
year = seq(1990, 2010, 1)) %>%
mutate(year = lubridate::ymd(year, truncated = 2L)) ->
data1
# Shiny app
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("period", "Year:",
min = 1990, max = 2010,
value = c(1990, 2010), sep = "")),
mainPanel(plotOutput("ggplot2"))))
server <- function(input, output) {
data1_subset <- reactive({
filter(data1,
year >= lubridate::ymd(input$period[1], truncated = 2L),
year <= lubridate::ymd(input$period[2], truncated = 2L))
})
output$ggplot2 <- renderPlot({
ggplot(data = data1_subset(), aes(x = year, y = values)) +
geom_line(color = "red") +
xlab("Year") +
theme_minimal()
})
}
shinyApp(ui = ui, server = server)
Thanks for your help! It seems that the answer was much simpler as I thought. Putting breaks = function(x) unique(floor(pretty(x)))
in my scale_x_continuous()
function produced integer-only breaks, even without transforming the data into Date
format. Removing the unique()
does not change the behavior in my case, but it might do in other cases.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.