How to plot multiple lines on the same graph using R

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ก.ค. 2024
  • In this tutorial, you will find step-by-step instructions to create line graphs, especially multiple line graphs, and to place legends right on top of the lines. We will use R and ggplot2 to create these plots.
    You will learn about a few best data viz practices along the way. To find more dataviz recipes and best-practices go to: ds4fr.nandeshwar.info/
    Here are the files used in this tutorial:
    -- Complete R code file: arn.la/lineplotsr
    -- Apple stocks data file: arn.la/applestock
    -- Tech stocks data file: arn.la/techstocks
    -- Entertainment stocks data file: arn.la/entstocks
    A couple of books mentioned in the tutorial:
    -- White Space is Not Your Enemy by Kim Golombisky amzn.to/2E1fRYM
    -- Visualizing Data by William Cleveland amzn.to/2n5hYmc
    Also, read more about the banking function in R here:
    -- www.rdocumentation.org/packag...
    An another good paper on banking by Jeff Heer: vis.berkeley.edu/papers/bankin...
    For more tips, check out: nandeshwar.info/courses

ความคิดเห็น • 33

  • @KartheeKSoNTI
    @KartheeKSoNTI 3 ปีที่แล้ว +1

    I am searching for this from the time I learnt about R studio. One very important point is we Excel users are used to plot data based on column name but here we need to stack the data in one single column. It took me many days to figure out. 😁

  • @olivernicholls1477
    @olivernicholls1477 3 ปีที่แล้ว +2

    3 days of suffering trying to figure this out, you helped me do it in 5 minutes. thank you so much

  • @TheMunishk
    @TheMunishk 4 ปีที่แล้ว +1

    So nicely explained concepts of GG Plot. Keep it up.

  • @chidiebereogbuchi
    @chidiebereogbuchi 4 ปีที่แล้ว +1

    You just saved me from headache...THANKS!!!

  • @ROHITSingh-pd2co
    @ROHITSingh-pd2co 5 ปีที่แล้ว +2

    Pls make video on two dot plot in same graph with the help of ggplot 2

  • @alejandrosantos373
    @alejandrosantos373 4 ปีที่แล้ว

    Thank you very much! your graph is really nice.
    I have a question:
    How can I create a double axis graph?

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  4 ปีที่แล้ว +1

      Alejandro, it is not exactly straightforward and also it is frowned upon. But I'll put another video together on that topic.

  • @justinatuulikefonangolo5509
    @justinatuulikefonangolo5509 5 ปีที่แล้ว

    Thank you very much, it is a nice tutorial

  • @nightangel8087
    @nightangel8087 4 ปีที่แล้ว +1

    thank you

  • @sarabdn2068
    @sarabdn2068 4 ปีที่แล้ว

    Thanks for this video!!
    Is there a possibility to add multuple lines but from another Dataframe?
    I have one large dataframe but made subsets. I want to plot the lines of the subsets which is based on a condition of a variable.
    Or is there a possibility to load one big dataframe and to make different lines based on a condition?

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  4 ปีที่แล้ว

      Yes, it is possible to overlay graphs from different data frames. Once you have your main plot, you can add another geom, say geom_line with its data argument populated. Something like this: g + geom_line(data = another_df, ...). If you can give an example, I can help you further.

    • @sarabdn2068
      @sarabdn2068 4 ปีที่แล้ว

      @@AshutoshNandeshwar Thanks! I will explain my question a little bit more.
      I want to plot the arrival pattern of trucks for every producttype. I have a dataframe of 27,000 datapoints. The variable "arrival" shows at what time a truck will arrive, for example at 7:02. I cut the arrivals in timeframes of half an hour, so the arrival I just showed, will have the timeframe 7:00-7:30.
      I made a new dataframe and counted all the timeframes and calculated the relative frequency.
      Within the big dataframe of 27,000 datapoints, one variable shows which producttype the truck is bringing to the terminal.
      With ggplot, I plotted the dataframe of the timeframes(x), with the frequency (y).
      However, this is only for one producttype.
      Or I have to make a dataframe with frequencies and timeframes and add a distinction of the producttype (but how?), or is there a possibility to add a ggplot to the plot I already created?
      I hope you understand me, because it is a little bit complex.
      Thanks for your help!

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  4 ปีที่แล้ว

      Sara,
      I think I understand what you are trying to do. I added some code to github. Let me know whether this helps you: github.com/ashutoshn/r-analytics/blob/master/dot-bar-charts-multiple-time-groupings.md

  • @CCoreOW
    @CCoreOW 3 ปีที่แล้ว +1

    How would you make multiple lines for different variables? I.e ones that are not grouped. For example, I have a data frame with the Egg mass of a quail egg, and I want to compare the shell mass with each of the egg components (Shell mass, yolk mass and albumen mass). Each of those components are under a new column in my data frame: How would I plot them all on the same graph against the egg mass?

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  3 ปีที่แล้ว

      Thanks for the question.
      Initially, I didn't see how line plots in your situation would help. It seemed to me that you were after scatter-plots as you have two variables to compare or see relationships between them. I'd recommend Visualizing Data by Cleveland for more details on choosing the right chart type.
      I posted a document on my github showing such scatter plots. github.com/ashutoshn/r-analytics/blob/master/multi-line-loess-scatter-plot.md
      But I think you want to see lines of fit. In that case, you will need to collapse the data frame to convert some columns into rows. I have posted that example also.
      Let me know if you need further help.

  • @Mads_Vel
    @Mads_Vel 4 ปีที่แล้ว

    Do you know how to plot multiple lines in the same graph, and start from the same value, for example $100?
    Then like you can compare Netflix, Amazon, Apple from 2009 to 2019?

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  4 ปีที่แล้ว +2

      Yes, you chain in another item: + ylim(100, NA) or + xlim(2009, 2019). But obviously, it would remove all the other data points that fall outside of these limits. For dates, you will have to use scale_x_date(). Check ggplot2.tidyverse.org/reference/scale_date.html

  • @TOPAZBLUEFOX
    @TOPAZBLUEFOX 3 ปีที่แล้ว

    It is possible to plot two density histograms from two different data frames in a single graph?

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  3 ปีที่แล้ว

      Yes, it is possible. You may be able to combine those two in one and add a column to specify the data frame. It will make many things easier.

  • @babikhandelwal9612
    @babikhandelwal9612 3 ปีที่แล้ว

    Sir, I made a ggplot using the geom_line() function two times to show the two different lines on the same plot. My data is bivariate data. I am unable to put the legends for both two line. Kindly help me to resolve it. If you share your email id, I can share with you my codes. Sir, I am at the very initial stage of programming. Plz help if possible

  • @zasaconsulting
    @zasaconsulting 3 ปีที่แล้ว +1

    nice tutorial! how to export graphs in png /jpg?

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  3 ปีที่แล้ว

      If you're using Rstudio, you can using the export button. Or use the ggsave function.

  • @harrisonbooth8653
    @harrisonbooth8653 2 ปีที่แล้ว

    can I extend this to more than 3 sets of stock data?

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  2 ปีที่แล้ว

      Yes, Harrison. This example shows three lines. Do you mean something else?

  • @ManhwasForLife
    @ManhwasForLife 2 ปีที่แล้ว

    which software you have used for creating this graph?

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  2 ปีที่แล้ว

      Hi, Afra, this video shows how to create such a plot using the R programming language.

  • @arunjohn492
    @arunjohn492 3 ปีที่แล้ว

    You saved me Boss - Times when I realize that Python is a shit-tool for basic visualization

  • @tommy.h.descartes
    @tommy.h.descartes 3 ปีที่แล้ว

    Really good video. But you rushed the part on creating multiple lines.

    • @AshutoshNandeshwar
      @AshutoshNandeshwar  3 ปีที่แล้ว

      Thanks for your comment, Tommy. Can you specify the parts which need more explaining? I can explore that in later videos.