tidyr Functions
Wide to Long Example
Plotting with ggplot2
Tidyrtidyr FunctionsA set of functions that will tidy up a data set such that:
Every Column is a variable
Every Row is an observation
Every Cell is a single value
pivot_longer()pivot_longer() function grabs the variables that repeated in an observation places them in one variablepivot_wider()pivot_wider() function then converts long data to wide data.separate()separate() function will separate a variable to multiple variables:We work on converting data from wide to long using the functions in the tidyr package. For many statistical analysis, long data is necessary.
Use the read_csv() to read data_3_4.csv into an object called data1;
pivot_longer()pivot_longer() function grabs the variables that repeated in an observation places them in one variable:separate()separate() function will separate a variable to multiple variables:pivot_wider()pivot_wider() function then converts long data to wide data.df3 <- data1 %>%
pivot_longer(`v1/mean`:`v4/median`,
names_to = "measurement",
values_to = "value") %>%
separate(measurement,c("time","stat"),sep="/") %>%
pivot_wider(names_from = stat,
values_from = value)#> pivot_longer: reorganized (v1/mean, v1/sd, v1/median, v2/mean, v2/sd, …) into (measurement, value) [was 1000x13, now 12000x3]
#> pivot_wider: reorganized (stat, value) into (mean, sd, median) [was 12000x4, now 4000x5]
ggplot2 is an R package used to create plots. The main idea is to use a data frame and a set of aesthetics (variables in the data frame) to create a base plot. Then, ggplot2 will layer geometries (plots) to the base plot to create a data visualization.
All new changes to the plot are layered on with the + symbol.
Using the penguins data set from palmerpenguins package. Create any plot and make it publication ready. Use the following resources to customize the plot: R Graphics Cookbook, R Graph Gallery, R Charts, and ggplot2