简体   繁体   English

如何在单个绘图ggplot2中绘制多条线?

[英]How to graph multiple lines in a single plot ggplot2?

I'm working on a R Shiny program that can take any csv file and output graphs of it. 我正在开发一个R Shiny程序,该程序可以获取任何csv文件并输出其图形。 The user who uploads the csv has some guidelines on how the data should look, but I don't want it to be too strict. 上传csv的用户对数据的外观有一些指导,但我不希望它过于严格。

I'm currently trying to use ggplot2 to graph multiple lines of the same dataset on one plot for comparison. 我目前正在尝试使用ggplot2在一个图上绘制同一数据集的多条线以进行比较。

The data I am currently uploading looks like this (simplified, as the data has over 1000 rows): 我当前正在上传的数据如下所示(简化,因为数据有1000行以上):

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 to output a graph that has the time on the X-axis (the user chooses this via a sidebar, as he can choose any axis, but time makes the most sense here). 我正在尝试输出在X轴上具有时间的图形(用户可以通过边栏选择时间,因为他可以选择任何轴,但是时间在这里最有意义)。 For the Y axis, I want 4 lines, colored differently, plotting each variable over time. 对于Y轴,我想要4条线,用不同的颜色来绘制每个变量随时间的变化。

I have all of the 'user taking in input and choosing which columns to graph' implemented, but for simplicity's sake, we can assume that for the most part, this has been hard coded (so Y variable will actually be input$y, etc in my implementation) 我已经实现了所有“用户输入并选择要绘制图形的列”,但是为了简单起见,我们可以假定大部分代码都是硬编码的(因此Y变量实际上是输入$ y,依此类推)在我的实施中)

The portion of my code where I try to graph the 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 one of the lines, but I have no idea how to plot the others on the same plot. 这将绘制其中一条线,但我不知道如何在同一条线上绘制其他线。 I've read about using 'group' in the aes function, but this depends on having a classifier in the dataset, which this one currently does not have. 我已经读过有关在es函数中使用“组”的信息,但这取决于在数据集中有一个分类器,而该分类器目前还没有。

I have also looked into the melt() function from the reshape2 package but am not sure how it would help me (both for the multiple line problem and the greater sense of this project, so that the user doesn't have to abide by strict rules for upload format of the csv). 我还从reshape2包中研究了melt()函数,但不确定如何解决(这对于多行问题和项目意义更大,因此用户不必遵守严格的规范)。 csv上传格式的规则)。

Any help would be much appreciated! 任何帮助将非常感激!

Assuming you put the xaxis variable (Date) in selectedxaxis , the selected products in selectedproducts and with data holding the loaded data: 假设您将xaxis变量(Date)放在selectedxaxis ,则将所选产品放入selectedproducts ,并且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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM