2

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

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
J.M
  • 25
  • 4
  • It looks like a bug, the code in this question: https://stackoverflow.com/questions/75138239/add-a-buffer-around-a-point-in-a-ternary-diagram is also not displaying the grid. Maybe a recent ggplot update broke the code. What version of ggplot2 do you have installed? Try downloading a previous version of ggplot to see if that fixes the problem. I would try "nick@ggtern.com". – Dave2e Mar 10 '23 at 23:31
  • Thank you for the response. The above code is from ggplot2_3.4.0. I tried an older version of ggplot2_3.3.6 package it did not support ggtern. I tried installing the older version of ggtern and unfortunately, it did not run. – J.M Mar 11 '23 at 12:47

2 Answers2

1

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

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • I tried ggplot2 3.3.5 and ggtern 3.3.5. It's not working. – J.M Mar 12 '23 at 01:16
  • The code works perfectly on https://rdrr.io/snippets/ with R version 4.0.3. I tried installing an older version of R on my PC, but the package doesn't work. @Quinten – J.M Mar 13 '23 at 14:37
  • I used R version 4.2.2, so maybe you could try with that version? Also please make sure you try this with a clean environment. – Quinten Mar 13 '23 at 17:40
0

This script worked

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)

install_version("ggtern", version = "3.3.5", repos = "http://cran.us.r-project.org")
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()+theme_showgrid()
J.M
  • 25
  • 4