r - How to graph multiple lines in a single plot ggplot2? -
i'm working on r shiny program can take csv file , output graphs of it. user uploads csv has guidelines on how data should look, don't want strict.
i'm trying use ggplot2 graph multiple lines of same dataset on 1 plot comparison.
the data uploading looks (simplified, data has on 1000 rows):
date hamburgers salads sodas fries 12-01 4 4 3 2 12-02 1 7 3 9 12-03 22 24 45 34 12-04 23 44 46 22 i'm trying output graph has time on x-axis (the user chooses via sidebar, can choose axis, time makes sense here). y axis, want 4 lines, colored differently, plotting each variable on time.
i have of 'user taking in input , choosing columns graph' implemented, simplicity's sake, can assume part, has been hard coded (so y variable input$y, etc in implementation)
the portion of code try graph data is:
output$plotline <- renderplot({ p <- ggplot(data, aes_string(x=x, y=y), environment = environment()) p <- p + geom_point(size = 3) p <- p + geom_line(aes(group=1)) print(p) }) this plots 1 of lines, have no idea how plot others on same plot. i've read using 'group' in aes function, depends on having classifier in dataset, 1 not have.
i have looked melt() function reshape2 package not sure how me (both multiple line problem , greater sense of project, user doesn't have abide strict rules upload format of csv).
any appreciated!
assuming put xaxis variable (date) in selectedxaxis, selected products in selectedproducts , data holding loaded data:
selectedxaxis = "date" selectedproducts = c("sodas", "salads") widedata = subset(data, select = c(selectedxaxis, selectedcolumns)) longdata = melt(widedata, id.vars=selectedxaxis, variable.name='product', value.name='count') ggplot(longdata) + geom_line(aes(date, count, color=product))
Comments
Post a Comment