rggplot2ternarygridlinesggtern

"ggtern" Ternary plot gridlines and axis ticks needed


I am trying to add gridlines and axis ticks to the ternary plot. I was able to adjust the numeric values of the axis, but unable to get axis ticks and grids inside the plot.

Here is an example R code:

library(ggplot2)
# Example data
x <- rnorm(100)
y <- rnorm(100)
z<-rnorm(100)
df <- data.frame(x, y, z)

# Create scatter plot with grid lines
ggtern(df, aes(x, y, z)) +
  geom_point() +
  theme_showgrid()+
  theme(panel.grid.major = element_line(color = "grey", linetype = "dashed"),
  panel.grid.minor = element_line(color = "grey", linetype = "dashed"))

I am getting this

Output Plot

I want this

Desired Plot


Solution

  • You could use an older version 3.3.5 from ggplot2 and use theme_bw like this:

    library(remotes)
    install_version("ggplot2", version = "3.3.5", repos = "http://cran.us.r-project.org")
    #> Downloading package from url: http://cran.us.r-project.org/src/contrib/Archive/ggplot2/ggplot2_3.3.5.tar.gz
    library(ggplot2)
    library(ggtern)
    
    # Example data
    x <- rnorm(100)
    y <- rnorm(100)
    z<-rnorm(100)
    df <- data.frame(x, y, z)
    
    # Create scatter plot with grid lines
    ggtern(df, aes(x, y, z)) +
      geom_point() +
      theme_bw()
    

    Created on 2023-03-11 with reprex v2.0.2