Category: R

  • Bar chart race in R using ggplot2 and gganimate

    Bar chart race in R using ggplot2 and gganimate

    Here is a relatively simple way to create a bar chart race in R – bar chart with bars overtaking each other. An attractive data visualization to engage your auditory while showing how the situation unfolds. Using the ggplot2 and gganimate, you can create a bar chart race in R and adjust the dynamics of […]

  • Unpivot data in R with the pivot_longer from tidyr

    Unpivot data in R with the pivot_longer from tidyr

    Here are multiple examples with the pivot_longer from tidyr, which is an excellent choice if you want to unpivot data in R and transform the data frame from wide to long.

  • Pivot data in R, like in Excel PivotTable, from long to wide

    Pivot data in R, like in Excel PivotTable, from long to wide

    Here is how to pivot data in R from long to wide format and increase the number of columns. This transformation might be familiar to Microsoft Excel users because of the PivoTable tool. It might not be the most commonly used data transformation, but sometimes necessary to show data in a small table or transform […]

  • How to generate random dates or numbers in R

    How to generate random dates or numbers in R

    Here is how to generate random dates or numbers in R by using base functions. There are similarities in both of the tasks, and they are useful in creating reproducible examples.

  • How to create a simple heatmap in R ggplot2

    How to create a simple heatmap in R ggplot2

    Here is how to quickly build a heatmap in R ggplot2 and add extra formatting by using a color gradient, data labels, reordering, or custom grid lines. There might be a problem if the data contains missing values. At the end of this post is an example of how to deal with NA values in […]

  • Count by group in R using base, dplyr, and data.table

    Count by group in R using base, dplyr, and data.table

    Here are multiple examples of how to count by group in R using base, dplyr, and data table capabilities. Dplyr might be the first choice to count by the group because it is relatively easy to adjust to specific needs. Meanwhile data.table is good for speed, and base R sometimes is good enough.

  • How to determine if the number is even or odd in R

    How to determine if the number is even or odd in R

    You can tell if the number is even or odd in R programming by looking at the reminder after the number is divided by 2. If the remainder equals 0, it is an even number, otherwise, it is an odd number. There is a nifty way to get the reminder after division in R by […]

  • Keep trailing zeros in R ggplot2 geom_text

    Keep trailing zeros in R ggplot2 geom_text

    If you want to keep trailing zeros in R, and in particular for text labels in ggplot2 geom_text, try functions like sprintf, formatC, or digits from the formattable package. Add trailing zeros in the R data frame, ggplot2, and keep numerical properties using the function digits from the formattable.

  • Replace the first occurrence of a character or string in R

    Replace the first occurrence of a character or string in R

    Here is how to find and replace the first occurrence of a character or string in R. It will help you to separate words in multiple lines in ggplot2 visualizations or do other tasks.

  • Count excluding NA in R

    Count excluding NA in R

    If you want to count values excluding NA in R, here is a simple way to do that. You can detect non-NA values and get results as TRUE or FALSE that can be used by the sum function to get the result.