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

asachet
  • 6,620
  • 2
  • 30
  • 74
Rooty1985
  • 83
  • 1
  • 3
  • 10

1 Answers1

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.

Thomas H
  • 51
  • 2
  • Hi Thomas, I get an error with this code: Error: 'to' must be a finite number – Rooty1985 Jun 12 '19 at 09:24
  • 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. – Thomas H Jun 12 '19 at 09:56
  • I get the following error when I call this code: > dput(pigs[1:5,]) Error in dput(pigs[1:5, ]) : object 'pigs' not found – Rooty1985 Jun 12 '19 at 10:08
  • 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") – Rooty1985 Jun 12 '19 at 10:19
  • Then it is definitely an error in the coercion. Can you open the .csv file with a text editor of your choice and post a few lines of data please – Thomas H Jun 12 '19 at 11:04
  • country,date,price Italy,30/11/15,75.51 Italy,07/12/15,76.45 Italy,14/12/15,77.82 Italy,21/12/15,78.33 Italy,23/11/15,78.78 Italy,28/12/15,81.53 Italy,16/11/15,81.6 Italy,04/01/16,81.6 Poland,30/11/15,82.25 Italy,11/01/16,82.93 Poland,07/12/15,83 Spain,07/12/15,84.36 – Rooty1985 Jun 12 '19 at 11:35
  • 1
    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") – Thomas H Jun 12 '19 at 12:31
  • just noticed that using this line of code (pigs <- read.csv("pigs_data.csv") pigs$date <- as.Date(pigs$date, format="%d/%m/%y")) stops the year select input from working, is there a way to get this connected again? – Rooty1985 Jun 12 '19 at 13:45