0

I am new to shiny and I have created an app however due to my large dataset all my date fields are squashed together and not readable for the user, I want to try and plot each specific date for the selected year (2014) along with each specific price that is linked to that date, selected year and country but I can not seem to do this easily without combining dates/years in the csv file which I do not want to do.

GGPLOT Sample:

GGPLOT

I have tried to play around with the aes code as shown below but even when the date field is allocated to the y axis the same thing happens.

 library(shiny)
 library(ggplot2)
 pigs <- read.csv("pigs_data.csv")
 # Define UI for application 
 ui <- fluidPage(
 # Application title
 titlePanel("Pig Breeding"),
 sidebarLayout( 
 sidebarPanel(
 #Allows user to choose a year which changes the distribution of plot points
 selectInput(inputId = "year",
            label = "Choose a year:",
            choices = c(2014, 2015, 2016, 2017, 2018),
            selectize = FALSE
            )
 ),
 # Show a plot of the generated distribution
 mainPanel(
 plotOutput("stats")
 )
  )
   )
 # Define server logic
 server <- function(input, output) { #for the selectInput you can use the following observeEvent(input$year, {
 output$stats <- renderPlot({
                    ggplot(pigs, 
                    aes(x = price, y = date, col = country)) + 
                    geom_jitter(width = 0.3) +
                    facet_grid(. ~input$year)
 })
 }
 # Run the application 
 shinyApp(ui = ui, server = server)'

I expect the app to show the individual dates (23/04/14) for each selected year (2014) along with a plot point for a specfic price (123.40) allocated to each country even if this means over plotting.

Sample of dataset:

Sample of dataset

3
  • 2
    It's squished because "date" column is not date Commented Jun 12, 2019 at 8:45
  • date column in the csv file or in the aes coding for the plot? Commented Jun 12, 2019 at 9:05
  • 1
    check this - stackoverflow.com/questions/4310326/…, also I would use geom_line since there are multiple groups and it's continuous Commented Jun 12, 2019 at 9:07

1 Answer 1

0

You date columns is still a string. In order to fix it you can try the following

pigs <- read.csv("pigs_data.csv") pigs$date <- as.Date(pigs$date, format="%d-%b-%y")

which takes your string and converts it to a date column.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi Thomas, I get an error with this code: Error: 'to' must be a finite number
My guess is, that there are NA's introduced when transforming the string to the date format. This can happen if the abbreveations do not match your local r installation. Can you call the following code please and send me the output? dput(pigs[1:5,]) It creates r Code that corresponds to the data. This way i can read it into my r session and see if i can reproduce the error.
I get the following error when I call this code: > dput(pigs[1:5,]) Error in dput(pigs[1:5, ]) : object 'pigs' not found
structure(list(country = structure(c(5L, 5L, 5L, 5L, 5L), .Label = c("Denmark", "EU Other", "France", "Germany", "Italy", "Poland", "Spain", "UK"), class = "factor"), date = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date"), price = c(75.51, 76.45, 77.82, 78.33, 78.78)), row.names = c(NA, 5L), class = "data.frame")
So the format was different than i expected. Some programms like excel change the date into a more readable format. This should work then. pigs$date <- as.Date(pigs$date, format="%d/%m/%y")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.