Scatterplots in R with geom_point() and geom_text/label()

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.ค. 2024
  • In this tutorial I show you how to create and label scatterplots in R with geom_point(), geom_text/label() and ggplot().
    I will explain the different function arguments, how you can map the color or shape to certain variables of your data frame and how to configure text you might want to add to the points.
    In the end I show you how you can quickly turn a scatterplot interactive with hover-over functionality.
    ⏱ Time Stamps ⌚
    0:00 - Intro
    0:26 - geom_point with iris
    1:50 - alpha and shapes
    5:25 - coloring
    9:02 - size and stroke
    10:36 - marginal distribution
    12:51 - geom_text/label
    13:36 - text alignment
    15:27 - font and color options
    17:50 - ggrepel
    18:45 - plotly interactive
    External Links:
    www.r-graph-gallery.com/scatt...
    r-charts.com/correlation/scat...
    ggplot2.tidyverse.org/article...
    www.sthda.com/english/wiki/ggp...
    rdrr.io/cran/ggrepel/man/geom...
    Background Music:
    • PARASITE EVE 2 SOUNDTR...
    Outro Animation:
    AA-VFX Motion Backgrounds
    • 4K Relaxing Live Wallp...

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

  • @jackdaws7125
    @jackdaws7125 2 ปีที่แล้ว +1

    Amazing in depth tutorial, best I've seen on the subject by a long shot

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

      Thanks for the comment. I try to fit as much information in as possible in a 15-20 minute video, with some good structure and examples. I though if someone spends the time to watch one of my videos, they should get the most out of it. Not sure whether I always succeed with this approach, but at least I try. :D glad you liked it.

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

    Keep the sentence /function meaningful with using the pipe %>%
    users (python, etc) can easily understand your code

  • @timmytesla9655
    @timmytesla9655 2 ปีที่แล้ว +1

    Very useful details. Thank you.

    • @TheDataDigest
      @TheDataDigest  ปีที่แล้ว

      That's very nice to hear, thank you :)

  • @vincenzo4259
    @vincenzo4259 2 ปีที่แล้ว +1

    Thanks

  • @milicaradakovic8098
    @milicaradakovic8098 9 หลายเดือนก่อน +1

    Hi thank you for this! Can you explain the difference between ggplot and ggplot2?

    • @TheDataDigest
      @TheDataDigest  9 หลายเดือนก่อน +1

      Sure. ggplot2 is the package name and ggplot is the plotting function.
      Try out: "help(ggplot2)" and "help(ggplot)" to see the difference and learn more.
      By the way, the "gg" stands for the "Grammar of Graphics" which is the concept between the different layers of the plot.

  • @lucy6692
    @lucy6692 5 หลายเดือนก่อน +1

    What do I do if I want to assign shapes to more than 6? how do I assign them individually?

    • @TheDataDigest
      @TheDataDigest  5 หลายเดือนก่อน

      Hi Lucy, thanks for leaving a comment.
      If you run the following code:
      test_df

  • @ditiromoloto3280
    @ditiromoloto3280 2 ปีที่แล้ว +1

    Hi, thanks for the video, it is quite helpful. However, I am stuck on the more than 6 shapes, I am working on 9 species and I am struggling with plotting the shapes for the other three. Your help would be highly appreciated.

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว +1

      Hi Ditiro, thanks for the comment. Here are a few ideas that might help you. If your species are somehow similar you could group them to reduce them from 9 shapes to 6. Or use the same shape for 2-3 species but work with different colors for them.
      There is a good reason that ggplot() by default only shows 6 shapes as they become more difficult to differentiate.
      Another option would be to work with facet wrap to split your species apart and only show 4-5 at the same time.
      And lastly, there is of course a way to force ggplot to use more than 6 shapes:
      stackoverflow.com/questions/26223857/more-than-six-shapes-in-ggplot

    • @ditiromoloto3280
      @ditiromoloto3280 2 ปีที่แล้ว +1

      @@TheDataDigest Thank you so much.

  • @nehanaaz1485
    @nehanaaz1485 11 หลายเดือนก่อน +1

    What if the R version doesn't have ggplot package. Is there any substitute package??

    • @TheDataDigest
      @TheDataDigest  11 หลายเดือนก่อน +1

      You should be able to install it. Just type in the console: install.packages("ggplot2")
      After that: library(ggplot2)
      You can also click in the packages panel of RStudio and search for it in CRAN with the install tab.

    • @nehanaaz1485
      @nehanaaz1485 11 หลายเดือนก่อน

      @@TheDataDigest please provide me the first code as it showed an error when I typed

  • @ldtrdl
    @ldtrdl 9 หลายเดือนก่อน +1

    How would you add a regession line on this type of scatterplot?

    • @TheDataDigest
      @TheDataDigest  9 หลายเดือนก่อน

      + geom_smooth(method = "lm", se = FALSE)
      Would add a regression line without standard error (by default you get a 95% confident interval). Depending on previous grouping (mapping color etc.) you would get several lines, one per group or just one line. Check out my latest 53 geoms video where I show some geom_smooth() examples to learn more.

  • @hassanhijazi4757
    @hassanhijazi4757 2 ปีที่แล้ว +1

    Can you save the plot as html or whatever and keep it interactive?

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว +1

      Hi Hassan, thanks for the comment. The R-Graph-Gallery also has tutorial on interactive graphs with a special section on how to save it as html or png.
      www.r-graph-gallery.com/interactive-charts.html
      Hope that already helps you.

    • @hassanhijazi4757
      @hassanhijazi4757 2 ปีที่แล้ว +1

      @@TheDataDigest Super! Appreciate it.

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

      @@hassanhijazi4757 In my new video tomorrow I will actually make use of the saveWidget() function so thanks again for the feedback/comment. Was a good opportunity for me to learn something new.