Non-Linear Coordinate System

The non-linear coordinate system is used for map projection, polar coordinates, and applying an arbitrary transformation to the axes. There are three types of non-linear coordinate systems. They are as follows:

Transforming Scales with coord_trans()

When there is a non-linear relationship between the data, one input, ytrans or xtrans, is required by the coord_trans() method to specify the transformation to be applied to the y or x-axis, respectively. The scales of the plot are transformed using the coord_trans() function, and as a result, the grid lines and tick labels are modified to reflect the translation.

Syntax:

coord_trans(x = NULL, y = NULL, xlim = NULL, ylim = NULL, 
lambda = NULL, breaks = NULL, labels = NULL, trans = NULL)

Parameters:

  • x/y: It specifies the transformation for the x-axis and y-axis, respectively. They accept either a function object representing a custom transformation or a character string indicating the transformation function, such as “log10” for a logarithmic transformation.
  • xlim/ylim: It specifies the x-axis and y-axis boundaries. The boundaries remain unchanged if NULL.
  • lambda: The Box-Cox transformation, a family of power transformations that includes the logarithmic transformation as a specific case, has lambda as its parameter. When the default value is NULL, no Box-Cox transformation is carried out.
  • breaks and labels: To set the tick locations and labels for the converted axis, breaks and labels are used. They use the scale_*_continuous() functions’ format as their starting point.
  • trans: It is used to define a special transformation function that transforms a vector of values.

Let us see how to modify a line chart’s y-axis with coord_trans()

Example: Original graph

R




# load ggplot2
library(ggplot2)
 
# Create sample data
data <- data.frame(
  year = rep(c(2021, 2022, 2023), each = 4),
  month = rep(c("May", "June", "July", "August"), times = 3),
  value = rnorm(12)
)
 
# Create a basic line chart with faceted coordinate system
ggplot(data, aes(x = month, y = value, group = year)) +
  geom_line() +
  labs(title = "Line Chart with Faceted Coordinate System") +
  xlab("Month") +
  ylab("Value") +
  facet_wrap(~ year, nrow = 1)


Output:

Original Graph

Example: Graph after using coord_trans()

R




# load ggplot2
library(ggplot2)
 
# Create sample data
data <- data.frame(
  year = rep(c(2021, 2022, 2023), each = 4),
  month = rep(c("MAY", "JUNE", "JULY", "AUGUST"), times = 3),
  value = rnorm(12, mean = 50000, sd = 10000)
)
 
# Create a basic line chart with faceted coordinate system
ggplot(data, aes(x = month, y = value, group = year)) +
  geom_line() +
  labs(title = "Line Chart with Faceted Coordinate System") +
  xlab("Mon") +
  ylab("Val") +
  facet_wrap(~ year, nrow = 1) +
  coord_trans(y = "log10")


Output:

In this example. we can see how the y-axis is altered using a logarithmic scale using the coord_trans() method. Using the data.frame() method, we first built a dataset with three years’ worth of arbitrary data. Next, using the ggplot() function, we constructed a ggplot object with the dataset and aesthetic mappings.

Then, we used the geom_line() function to add a layer for the line chart and the coord_trans() function to change the y-axis to have a logarithmic scale. We can change the coordinate system scales by using the coord_trans() function, which accepts the scale transformation as an argument. In this instance, the y-axis scale was converted to a logarithmic scale using the formula y = “log10”.

Transform scale with coord_transf()

Polar  Coordinated with coord_polar()

The ggplot2 package is used to create a scatterplot with a polar coordinate system. This type of coordinates is used to produce circular plots, including pie charts and radial plots. The data are plotted using the theta and radius radial axes.

Syntax:

coord_polar(theta = "x")

Parameter:

  • theta: It specifies the angle (theta) determined by the x variable for polar coordinates.

Example:

R




# Load the ggplot2 package
library(ggplot2)
 
# Create a dataset
data <- data.frame(
  theta = c(300, 600, 900, 1200, 1500, 1800),
  radius = c(100, 200, 300, 400, 500, 600),
  group = c("P", "Q", "R", "P", "Q", "R")
)
 
# Create a ggplot object with the dataset and aesthetic mappings
p <- ggplot(data, aes(x = theta, y = radius, color = group))
 
# Add a layer for the scatterplot
p <- p + geom_point()
 
# Specify the Polar coordinate system
p <- p + coord_polar()
 
# Display the plot
print(p)


Output:

This example showed how to make a scatterplot with a polar coordinate system using ggplot2’s “coord_polar()”  function. In order to produce the scatterplot with a polar coordinate system, the algorithm first created a dataset with three columns: one for the angle, one for the radius, and one for the group.

Polar coordinates with coord_polar()

These examples all demonstrate how to use ggplot2 to produce visualizations with various coordinate systems, which can be useful for showcasing patterns or correlations in the data.



Coordinate systems in ggplot2

In R programming, ggplot2 is a well-liked library for data visualization. It offers a versatile method for producing a wide range of plots, including scatterplots, line plots, bar charts, histograms, and many more. Users of ggplot2 can produce visualizations that more clearly convey the patterns and relationships in their data by utilizing several coordinate systems.

Similar Reads

Coordinate System in ggplot2

To depict the values of the data, the ggplot2 package uses two primary coordinate systems. They are:...

Linear Coordinate System in ggplot2

The Linear coordinate system is the one that mostly uses the cartesian coordinate system and is the default coordinate system of the ggplot2.  It consists of the origin, which is where the two axis, i.e., the x and y axis intersect. This system is mainly used for zooming, fixing coordinates, and flipping coordinates. There are three types of linear coordinate systems. They are as follows:...

Non-Linear Coordinate System

...

Contact Us