Search This Blog

Radar Plot in ggplot2 Comparing Alim Öztürk and Uğur Demirok


This article runs through the creation of basic radar charts in R, plotting the data of two players, before creating a function to streamline the process.

This code defines two players' attributes by creating AlimOzturk and UgurDemirok vectors and then combines them into a data frame. It assigns the column names of the data frame to the variable Attributes and the number of attributes to the variable AttNo. Then it creates a new data frame by adding the first column of the data frame again at the end of the dataframe.

This code creates a function called circleFun which takes in three parameters: center, diameter and npoints. The function calculates the radius and creates a sequence of points on the circle using trigonometry. The function then returns a data frame with x and y coordinates of the points on the circle. Then it creates four circles with different diameters by calling the circleFun function four times and passing in different diameter values. Next, it splits the angles into segments by dividing 2pi by the number of attributes in the data frame and creating a sequence from 0 to 2pi with a length of angle_spilt.

This code creates two empty data frames LineData and TitlePositioning. The LineData data frame has two numeric columns x and y and stringsAsFactors is set to F. The TitlePositioning data frame has three columns: title, x and y with the title column being of character type and x and y being of numeric type. Also stringsAsFactors is set to F. These data frames are used to store the data needed to create the lines and labels in the radar chart.

This code creates the data needed for the background construction of the radar chart. It loops through the columns of the data frame and calculates the x and y coordinates for each segment of the chart by using trigonometry. It stores the x and y coordinates in the LineData data frame. It also creates the title data by looping through the columns of the data frame, calculates the x and y coordinates for the title position using trigonometry and stores the title, x and y coordinates in the TitlePositioning data frame.

Then it creates the value labeling data. It creates a values vector containing the values 25, 50, and 75. It calculates the radian for the values by dividing the angle_spilt by 2. Then it calculates the x and y coordinates for the values using trigonometry and stores the values, x and y coordinates in the ValuePositioning data frame.

It adds the origin values (0,0) for the lines by adding two columns x2 and y2 to the LineData data frame and assigns the values 0 to these columns.

It creates an empty data frame called polydata and creates the polygon data for the players by looping through the columns and rows of the data frame, calculates the x and y coordinates for each attribute of each player using trigonometry and stores the player name, value, radians, x and y coordinates in the polydata data frame. This data will be used to plot the polygons representing each player's attributes on the radar chart.

This code splits the data into two separate data frames for player 1 and player 2 by using the unique function to find the unique player names in the polydata data frame and then using the which function to filter the rows that correspond to each player.

It then creates the title strings for each player by using the gsub function to insert a space before any uppercase letters in the player names and then using the trimws function to remove any white spaces from the beginning and end of the player names. The Player1_title and Player2_title variables will be used to add titles to the radar chart.

This code generates a radar chart using the ggplot2 library in R. It creates several dataframes to hold the data for the chart, including:

LineData: the data for the lines that will form the segments of the radar chart

TitlePositioning: the data for the titles of the attributes/values that will be displayed on the chart

ValuePositioning: the data for the values that will be displayed on the chart

polydata: the data for the polygons that will be used to display the data for each player

It also creates four circles using the circleFun() function, which is defined at the beginning of the code. These circles will be used as the background of the chart.

The code then uses the ggplot() function to create the chart, adding the circles, segment lines, attribute/value titles, and polygons for each player. It also adds the title for each player and some additional formatting.

This code generates a radar chart using the ggplot2 library in R. It creates several dataframes to hold the data for the chart, including:

  • LineData: the data for the lines that will form the segments of the radar chart
  • TitlePositioning: the data for the titles of the attributes/values that will be displayed on the chart
  • ValuePositioning: the data for the values that will be displayed on the chart
  • polydata: the data for the polygons that will be used to display the data for each player




0 Comments:

Post a Comment