散点图和闪亮

时间:2019-03-05 12:38:31

标签: r ggplot2 shiny

我对于Shiny和ggplot还是很陌生,所以我不确定是什么引起了该问题,并且我之前在此代码中发布了不同的问题,但是我遇到了一个新问题...当我运行我的绘图时,我遇到了一个新问题。没有出现,而是出现错误:警告:FUN中的错误:找不到对象“ total_pigs”。

无论我在selectInput "x"函数中选择什么选项,我都会遇到此错误。

我在与app.R文件相同的目录中有一个单独的CSV文件。 selectInput(s)与CSV中的列和行匹配,我希望我的代码可以简单地读取其中存储的数据,并生成我的绘图点。

library(shiny)
library(ggplot2)
path <- file.path("eu_pigs.csv", stringsAsFactors = FALSE)
ui <- fluidPage(
titlePanel("Breeding Numbers 2016 - 2018 (pig)"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "x", 
          label = "Pig Breeds:", 
          choices = c("total_pigs", "female_breeding_herd", 
                      "in_pig_sows", "in_pig_gifts", "other_sows", 
                      "maiden_gilts", "boars_for_service", "other_pigs"),
          selected = "total_pigs"),
selectInput(inputId = "y",
          label = "Year by year change:",
          choices = c(2016, 2017, 2018, sep = ""),
          selected = 2016)
),
mainPanel(
plotOutput(outputId = "scatterplot")
)
)
)

server <- (function(input, output) {
output$scatterplot <- renderPlot({
ggplot(data = read.csv("eu_pigs.csv")) +
aes_string(x = input$x, y = input$y) +
geom_point()
})
})

shinyApp(ui, server)

我附上了csv文件的图像。

image of my csv file

1 个答案:

答案 0 :(得分:0)

正如DS_UNI所建议的那样,问题可能出在您的数据结构上。 ggplot2希望使用整洁的数据,因此在这种情况下,这意味着每一行代表一个案例,即一年。

这是我认为您需要做的,尽管这是对您希望最终结果看起来像什么做出的一些假设!

第一步:读取csv中的数据争用

pigs_data <- read.csv("eu_pigs.csv")

第二步:将第一列设置为行名

row.names(pigs_data) <- pigs_data$pig_breeds
pigs_data[1] <- NULL

第三步:转置数据

pigs_data <- t(pigs_data)

然后您将在ggplot数据参数中使用pigs_data

相关问题