Method for drawing ternary diagram and RGB three-color space distribution diagram with Ternary package in R language

  This article introduces the detailed method of drawing a ternary plot based on the package in the R language; among them, we will take the RGB three-color distribution map as an example to introduce it in detail.Ternary

  The ternary diagram can reflect the characteristics of data from three different angles, so it is widely used in many fields; as shown in the figure below, it is the simplest ternary diagram. Among them, based on the package in the RTernary language , we can draw the ternary diagram very conveniently; this article introduces its specific drawing method.

insert image description here

  First, since we need to use the package in RTernary language , configure the package through the code shown below Ternary.

install.packages("Ternary")

  TernaryPackage provides two methods for drawing ternary plots. First of all, we can use the interactive interface provided by it to complete the drawing of simple ternary diagrams through mouse operations. We can open this interactive interface through the code shown below.

Ternary::TernaryApp()

  After the interactive interface is opened, as shown in the figure below. We can manually modify the ternary map according to the introduction of each button and parameter, and finally export the image.

insert image description here

  Here we mainly introduce the code- based drawing method, so the interactive interface will not be introduced too much; if you need it, you can try the interactive interface drawing method by yourself.

  First, to draw a ternary diagram through code, we need to import Ternarythe package.

library(Ternary)

  The simplest way to draw a ternary diagram is to generate a basic ternary diagram through the code under the diagram.

TernaryPlot()

  Run the above code, you will get the picture as shown below.

insert image description here

  Of course, we need to make personalized modifications to this simple ternary diagram based on actual needs. First, all the code used in this article is as follows.

library(Ternary)
# dev.off()

TernaryPlot(alab = "Terrain \u2192", blab = "Vegetation \u2192", clab = "\u2190 Climate",
            lab.col = c("red", "green", "blue"),
            main = "Test Ternary Plot",
            point = "up", lab.cex = 0.8, grid.minor.lines = 0,
            grid.lty = "solid", col = rgb(0.9, 0.9, 0.9), grid.col = "white",
            axis.col = rgb(0.6, 0.6, 0.6), ticks.col = rgb(0.6, 0.6, 0.6),
            axis.rotate = FALSE,
            padding = 0.08)

cols <- TernaryPointValues(rgb)
ColourTernary(cols, spectrum = NULL)

data_points <- list(
  O = c(255, 128, 0),
  Y = c(255, 255, 0),
  P = c(255, 0, 255),
  C = c(0, 255, 255)
)
AddToTernary(points, data_points, pch = 21, cex = 2.8,
             bg = vapply(data_points,
                         function (x) rgb(x[1], x[2], x[3], 255, maxColorValue = 255),
                         character(1))
             )
AddToTernary(text, data_points, names(data_points),cex = 0.8, font = 2)

legend("topright",
       legend = c("Orange", "Yellow", "Purple", "Cyan"),
       cex = 0.8, bty = "n", pch = 21, pt.cex = 1.8,
       pt.bg = c(rgb(255, 128, 0, 255, NULL, 255),
                 rgb(255, 255, 0, 255, NULL, 255),
                 rgb(255, 0, 255, 255, NULL, 255),
                 rgb(0, 255, 255, 255, NULL, 255)),
       )

  Running the above code, we get the final result image as shown in the figure below.

insert image description here

  Next, we introduce and explain the above code. It should be noted here that this article only briefly introduces the parameters we use, and the meaning of many of them is actually not clear to me; in actual use, if you have other doubts about the parameters, you can refer to the official Ternarypackage Help documentation: https://ms609.github.io/Ternary/reference/index.html.

  First of all, the first part of the code, that is, TernaryPlot()the function, is Ternarythe basic function of the package to draw the ternary diagram; we modify the parameters in it to modify the attributes of each part of the final diagram. Among them, the first line of the function, that is, the line alabat the beginning, indicates the characters to be displayed on the three sides of the triangle, that is, " label "; lab.colit indicates the text label of the coordinate axis corresponding to the three sides of the triangle, and what color needs to be used to Indicates; indicates the titlemain of the ternary diagram ; indicates the angle at which the triangle is placed (for example, what I have here means that one of the corners is placed upward); the next two parameters and indicate the font size and label of the coordinate axis respectively The smallest unit grid line of the ternary map ; since I am making a color space map in which the triangles respectively represent , , and three colors, there is no grid line set.point"up"lab.cexgrid.minor.linesRGB

  Then, the fifth line of the function indicates the type ofgrid.lty the grid line of the ternary graph , the subsequent parameter indicates the drawing color inside the ternary graph , and the subsequent parameter indicates the color of the grid line (of course, I have not set the grid line here); the next The parameter indicates the color of the three axes of the ternary diagram , and the parameter indicates the color of the corresponding labels (not text labels) on the three axes; the parameter indicates whether to rotate the label ; the last parameter is used to control the zoom of the image .colgrid.colaxis.colticks.colaxis.rotatepadding

  Next, since we want to realize the coloring of the ternary diagram by filling the three colors of R, G, Band , we declare a colsvariable and ColourTernary()fill it into the ternary diagram through the function.

In addition, in order to make it easier for everyone to see the picture, we hope to add a few points of key colors in   this triangle R, , Gand color space diagram, as a reference for everyone when viewing the picture. BTherefore, we determine several color points through R, G, and Bvalues, and store them in data_point; then, AddToTernary()import them into the ternary map through the function.

  Next, which is the last part of the code, we legend()add a legend to the image through the function . The legend here is actually the legend of several main color points we added to the picture in the previous step. First of all, the first parameter of the function "topright"indicates that we want to add the legend to the upper right corner of the picture; the next few lines of parameters are to adjust the font, font size, content to be displayed, etc. of the legend.

  After adjusting the code to obtain our satisfactory ternary diagram, if you use RStudio to write the code, you can select the " Export "→" Save as Image... " option in " Plots " to export the image; as shown in the figure below Show.

insert image description here

  However, it should be noted here that the images exported in RStudio.eps are often not clear enough; if you want to improve the accuracy of the images, you can choose the image material in the export format; as shown in the figure below.

insert image description here

  Then, open the format file just saved in the Adobe Illustrator ( AI.eps ) software ; at this time, not only can the picture be further modified directly, but also a picture with higher precision can be saved in the picture format.

insert image description here

  So far, you're done.

Welcome to pay attention: Crazy learning GIS

Guess you like

Origin blog.csdn.net/zhebushibiaoshifu/article/details/129050589