-3

While executing the below Shiny code:

library("shiny")
library("ggplot2")
library("DT")
library("reshape2")
## load data ##

aqi = read.csv(file = "C:/Users/stan/Desktop/AIR/month.csv",
               header = T, sep = ",")
str(aqi)
colnames(aqi) = c("num","month", 
                  "PM10","PM2.5","NO","NO2")
## ui.R ##

ui = fluidPage(
  titlePanel('AIR'),
  sidebarLayout(
    sidebarPanel(
      selectInput('month',
                  'Choose a month:',
                  c('All', unique(as.character(aqi$month)
                  ))),
      selectInput('PM10',
                  'Number of :PM10',
                  c('All', unique(as.character(aqi$PM10)
                  ))),
      selectInput('PM2.5',
                  'Number of PM2.5:',
                  c('All',unique(as.character(aqi$PM2.5)
                  ))),
      selectizeInput("AIR", 
                     "Select Contaminant:", 
                     choices = c( "PM2.5", 
                                  "PM10"), 
                     selected = "PM2..5", 
                     multiple = TRUE )
  ),

    mainPanel(
      dataTableOutput('table'),
      plotOutput("plot")
    )
  )
)


## server.R ##

server = function(input, output) {
  # Filter data based on selections
  output$table <- DT::renderDataTable(DT::datatable({
    data <- aqi
    if (input$month != "All") {
      data <- data[aqi$month == input$month,]
    }
    if (input$PM10 != "All") {
      data <- data[aqi$PM10 == input$PM10,]
    }
    data
    if (input$PM2.5 != "All") {
      data <- data[aqi$PM2.5 == input$PM2.5,]
    }
    data
  }))
  output$plot = renderPlot({ 
    plot.data <- melt(aqi, id.vars= "month") 
    #not sure if input$cnt is a list or a vector 
    #may need to manipulate that before passing 
    plot.data <- plot.data[plot.data$variable %in% input$AIR, ] 
    ggplot(plot.data) + 
      geom_line(mapping = aes(x = month, y = value , colour =variable)) + 
      labs (x = "month", y = "value", title = "AIR") + 
      scale_colour_discrete(name = "AIR") 
  }) 
}

shinyApp(ui, server)

the errors were thrown:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

Should I adjust the grouping in aesthetics to avoid the errors above?

Here're my data:

1   num month   PM 10   PM 2.5  NO  NO2
2   1   1月  24  10  2.59    8.61
3   2   2月  45  20  2.14    9.94
4   3   3月  40  20  2.97    10.94
5   4   4月  51  20  2.16    11.27
6   5   5月  36  16  1.91    10.3
7   6   6月  33  13  1.89    8.85
8   7   7月  26  11  1.87    6.43
9   8   8月  28  13  2   9.4
10  9   9月  32  15  1.45    7.01
11  10  10月 35  15  1.26    7.77
12  11  11月 24  12  1.66    7.65
13  12  12月 21  11  2.06    8.22
Artem
  • 3,304
  • 3
  • 18
  • 41
stan
  • 11
  • 1
  • Please change the question title to something that describes your actual, specific problem. – Roland May 26 '19 at 05:45
  • Hi @stan, there's a couple issues with your question that make it hard for us to help you. First, it's just code. We need some text explaining what you're trying to do and what the problem is. Second, the example is not reproducible, as we don't have the `month.csv` file. See the answers here for how to write a good reproducible example. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Aaron left Stack Overflow May 29 '19 at 02:49
  • Also, the example should be minimal. The error seems to be a ggplot error, but there's lots of shiny code in the question right now. Is that necessary? – Aaron left Stack Overflow May 29 '19 at 02:49

1 Answers1

0

You can solve a problem adding group = 1 into aesthetics however the result will be not aesthetically pleasing as the Japanese unicode characters for months are converted to factor types.

Another approach is to use continuous scale while draw lines with the numbers for months from 1 to 12 then manually add labels and breaks - scale_x_continuous(breaks = 1:12, labels = month_label).

Please see the code below:

aqi <- structure(list(num = 1:12, month = structure(c(1L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 2L, 3L, 4L), .Label = c("1<U+6708>", "10<U+6708>", 
"11<U+6708>", "12<U+6708>", "2<U+6708>", "3<U+6708>", "4<U+6708>", 
"5<U+6708>", "6<U+6708>", "7<U+6708>", "8<U+6708>", "9<U+6708>"
), class = "factor"), PM10 = c(24L, 45L, 40L, 51L, 36L, 33L, 
26L, 28L, 32L, 35L, 24L, 21L), PM2.5 = c(10L, 20L, 20L, 20L, 
16L, 13L, 11L, 13L, 15L, 15L, 12L, 11L), NO = c(2.59, 2.14, 2.97, 
2.16, 1.91, 1.89, 1.87, 2, 1.45, 1.26, 1.66, 2.06), NO2 = c(8.61, 
9.94, 10.94, 11.27, 10.3, 8.85, 6.43, 9.4, 7.01, 7.77, 7.65, 
8.22)), row.names = c(NA, -12L), class = "data.frame")


aqi$num <- NULL

library(ggplot2)
library(reshape2)
month_label <- aqi$month
aqi$month <- 1:nrow(aqi)

plot.data <- melt(aqi, id.vars= "month") 


ggplot(plot.data) + 
  geom_line(mapping = aes(x = month, y = value , colour = variable)) + 
  labs (x = "month", y = "value", title = "AIR") +
  scale_x_continuous(breaks = 1:12, labels = month_label)

Output:

air quality graph

Artem
  • 3,304
  • 3
  • 18
  • 41