X轴上的ggplot2日期全部合并 [英] ggplot2 date in X axis is all combined
问题描述
我是新手,我创建了一个应用程序,但是由于我的数据集很大,我的所有日期字段都被压缩在一起,并且用户不可读,因此我想尝试绘制选定年份(2014年)的每个特定日期以及与该日期,选定的年份和国家/地区链接的每个特定价格,但如果不将我不想这样做的csv文件中的日期/年份组合在一起,我似乎很难做到这一点.
GGPLOT示例:
我尝试如下图所示使用aes代码,但是即使将date字段分配给y轴,也会发生相同的情况.
库(发光)库(ggplot2)猪<-read.csv("pigs_data.csv")#定义应用程序的UIui<-fluidPage(#应用标题titlePanel(猪育种"),sidebarLayout(sidebarPanel(#允许用户选择更改图点分布的年份selectInput(inputId ="year",label =选择一年:",选择= c(2014、2015、2016、2017、2018),选择=否)),#显示生成的分布图mainPanel(plotOutput("stats"))))#定义服务器逻辑服务器<-函数(输入,输出){#对于selectInput,您可以使用以下watchEvent(input $ year,{output $ stats<-renderPlot({ggplot(猪,aes(x =价格,y =日期,col =国家))+geom_jitter(宽度= 0.3)+facet_grid(.〜input $ year)})}#运行应用程序ShinyApp(ui = ui,服务器=服务器)'
我希望该应用程序显示每个选定年份(2014)的各个日期(2014年4月23日)以及用于分配给每个国家的特定价格(123.40)的绘图点,即使这意味着要进行绘图./p>
数据集示例:
您的日期列仍然是字符串.为了解决这个问题,您可以尝试以下
猪<-read.csv("pigs_data.csv")pigs $ date<-as.Date(pigs $ date,format =%d-%b-%y")
它将获取您的字符串并将其转换为日期列.
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:
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:
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.
这篇关于X轴上的ggplot2日期全部合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!