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...
Amazing in depth tutorial, best I've seen on the subject by a long shot
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.
Keep the sentence /function meaningful with using the pipe %>%
users (python, etc) can easily understand your code
Very useful details. Thank you.
That's very nice to hear, thank you :)
Thanks
Hi thank you for this! Can you explain the difference between ggplot and ggplot2?
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.
What do I do if I want to assign shapes to more than 6? how do I assign them individually?
Hi Lucy, thanks for leaving a comment.
If you run the following code:
test_df
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.
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
@@TheDataDigest Thank you so much.
What if the R version doesn't have ggplot package. Is there any substitute package??
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.
@@TheDataDigest please provide me the first code as it showed an error when I typed
How would you add a regession line on this type of scatterplot?
+ 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.
Can you save the plot as html or whatever and keep it interactive?
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.
@@TheDataDigest Super! Appreciate it.
@@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.