From 82fd11ca21911ea3ca9a349af852578fa4cadb1e Mon Sep 17 00:00:00 2001 From: Lyu111 Date: Mon, 18 Apr 2022 11:22:07 -0400 Subject: [PATCH 1/2] ggplot2_cheatsheet_lyupeng --- ggplot2_cheatsheet.Rmd | 129 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 ggplot2_cheatsheet.Rmd diff --git a/ggplot2_cheatsheet.Rmd b/ggplot2_cheatsheet.Rmd new file mode 100644 index 0000000..3c15a4b --- /dev/null +++ b/ggplot2_cheatsheet.Rmd @@ -0,0 +1,129 @@ +# ggplot2_cheatsheet + +Lyu Peng + +```{r, include=FALSE} +knitr::opts_chunk$set(warning = FALSE, message = FALSE) +``` + +```{r} +library(tidyverse) +library(ggplot2) +``` + + +## Overview +As a statistic tool, r is more common used in statistical learning, however, for most people, they are not statistic experts so that they do not have strong ability and motivation to use r. i will provide some details to r using and explain how to help people who know python to learn r is an important topic, especially in statistical graphing. + +## Introduce the rule graphing: + +First we have to determine which type of graphs we want, there are several majority plots that we usually draw, scatterplot, histogram, barplot, boxplot, heatmap, etc... for r, people usually use package "ggplot2" to get their graphs, but first of all, we need to do data preprocessing. + +### Scatter plot + +```{r} +ggplot(mtcars, aes(mpg, disp)) + geom_point() +``` + +### Bar plot + +```{r} +ggplot(quakes, aes(x = depth)) + geom_bar() +``` + +### Histogram + +```{r} +ggplot(data = mpg) + + geom_histogram(aes(x = hwy)) +``` + +### Boxplot + +```{r} +ggplot(mpg) + + geom_boxplot(aes(x = class, y = hwy)) +``` + +There are two parts we need to care about, which are `ggplot()` and `geom_point()`, `aes()` shows aesthetics, which should include data for x axis, and data for y axis. by adjusting the words after "geom_" with "bar","line","point","boxplot","histogram". etc... + +### Adding rules + +If want to create labels or title or theme for your graph, we can by using "+" to combine our command, which is a common rule for r graphing, for example, with `labs()` and `theme_classic()`. +```{r} +ggplot(mpg) + + geom_point(aes(x = displ, y = hwy, color = class)) + + labs(title = "mpg", + subtitle = "disply vs hwy", + x = "x", + y = "y", + color = "Class") + + theme_classic(16) +``` + +### Ordering + +If want to show the data in some orders, there are some functions that we can show x or y with some order in graphing, use `fct_reorder()`, `fct_inorder()`, `fct_relevel()`, `fct_infreq()`, `fct_rev()`, etc... +```{r} +ggplot(mpg) + + geom_point(aes(x = fct_infreq(fl), y = hwy, color = class)) + + labs(title = "mpg", + subtitle = "fl vs hwy", + x = "x", + y = "y", + color = "Class") + + theme_classic(16) +``` + +## Facet + +If want to generate multiply graphs by some criterias, in other word, facet by multiple variables, people can use `facet_warp()` and `facet_grid()` to make several graphs in some criterias, for which the description of them is not well documented. + +### Facet_grid + +```{r} +p <- ggplot(mpg, aes(displ, cty)) + geom_point() +p + facet_grid(rows = vars(drv)) +p + facet_grid(cols = vars(cyl)) +p + facet_grid(vars(drv), vars(cyl)) +``` + +We see `facet_grid()` can generate columns and rows seperately or simultaneously, which facet in a 2d grid of panels. + +### Facet_warp() +```{r} +p <- ggplot(mpg, aes(displ, cty)) + geom_point() +p + facet_wrap(vars(drv)) +p + facet_wrap(vars(cyl)) +# You can facet by multiple variables +ggplot(mpg, aes(displ, cyl)) + + geom_point() + + facet_wrap(vars(cyl, drv)) +# it only generate 9 labels +``` +From above, we know `facet_warp()` can return less labels, even though they have same parameters, but does not loss any information. we can say facet_warp() and facet_grid() are quite similar functions, which can represent the same data, but with different labels or panels. + +## python + +Python use "numpy","pandas","sklearn" for datapreprocessing. + +### package and function for graphing + +The predominant package for graphing called "matplotlib.pyplot", which includes many functions to help graphing. +There are two options that we can show a graph: +~ `plt.show()` +~ `%matplotlib inline` + +For goal to graph a line plot,people can use build-in function. +~`plot()` +~`bar()` +~`hist()` +~`scatter()` +~`pie()` +To setup xlab and ylab, python can use +~ `plt.xlabels()` +~ `plt.ylabels()` + + + + From 95be906fd3d5617cb00f5bf477c09f37491190d2 Mon Sep 17 00:00:00 2001 From: Tianyu <46831711+ytyky@users.noreply.github.com> Date: Fri, 6 May 2022 22:50:05 -0400 Subject: [PATCH 2/2] Update _bookdown.yml --- _bookdown.yml | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/_bookdown.yml b/_bookdown.yml index 0653b18..c1031f5 100644 --- a/_bookdown.yml +++ b/_bookdown.yml @@ -17,10 +17,52 @@ rmd_files: [ # Part: Cheatsheets 'whatwedid.Rmd', # tmap -- contains PART header 'ggplot2_graphics_cheatsheet.Rmd', + 'ggplot2_cheatsheet_group16.Rmd', + 'data_preprocessing_cheat_sheet_in_r.Rmd', + 'data_wrangling_and_visualization_in_r_cheatsheet.Rmd', + 'ggplot_cheatsheet.Rmd', + 'cheatsheet-for-tidyverse.Rmd', + 'ccgitsubmit.Rmd', + 'cheatsheet_for_multiple_graphics.Rmd', + 'ggplot_useful_settings.Rmd', + 'circlize_cheatsheet.Rmd', + 'texts_in_graphs_cheatsheet.Rmd', + 'dygraphs_intro.Rmd', + 'course_cheatsheet.Rmd', + 'webscraping_table_xpath.Rmd', + 'basic_visualization_cheatsheet.Rmd', + 'ggplot2_cheatsheet.Rmd', # Part: Tutorials - + 'visualizing_geographic_time_series_with_messy_country_name.Rmd', # contains PART header + 'plotly_package.Rmd', + 'web_scraping.Rmd', + 'guide_through_ggplot_via_example_plots.Rmd', + '5commongraphs_ggplot2.Rmd', + 'cdwopbmah.Rmd', + 'twitter_api_in_r.Rmd', + 'ggmap_ggplot_tmap_maps.Rmd', + 'shiny_map_tutorial.Rmd', + 'plotly_package.Rmd', + 'neural_network_ploting_with_bp_algorithm_and_without_algorithm.Rmd', + 'wordcloud.Rmd', + 'linear_models_soccer_example.Rmd', + 'timeseriesanalysis.Rmd', + 'animation_graph.Rmd', + 'forest_plots.Rmd', + 'differences_between_some_packages_of_visualization.Rmd', + 'lecture_helper.Rmd', + 'basic_introduction_to_tidyverse.Rmd', + 'class_material_code_throughout_this_semester.Rmd', + 'calendar_heat_map_tutorial.Rmd', + 'basic_time_series_analysis_and_prediction.Rmd', + 'r_shiny_interactive_map.Rmd', + 'image2data.Rmd', + 'Tutorial_for_the_R_Graph_Gallery.Rmd', + 'polar_line_plots.Rmd', + # Part: Workshops + 'data_visualization_with_base_r_and_ggplot.Rmd', # Part: Appendices 'appendix_initial_setup.Rmd', # contains PART header