1

I want to show a tick mark for theta axis in the ggplot2 poar plot. However, both axis.ticks.y and axis.ticks.y in the theme() does not work for theta axis. Any help would be appreciated, thanks

library(ggplot2)
df <- data.frame(
    start = c(0, 121, 241),
    end = c(120, 240, 359),
    group = letters[1:3]
)
# a example circular ring plot
base <- ggplot(df, aes(ymax = end, ymin = start, 
                       xmin = 0.8, xmax = 1, 
                       fill = group)) +
    geom_rect() +
    coord_polar(theta = "y") +
    xlim(c(0, 1))
base

# the tick of y axis can be changed
base + theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())

# set the tick of x axis not worked for the theta axis
base + theme(axis.ticks.x = element_line(color = "black", size = 2))

Thanks for @Vishal A., the answer from Controlling ticks and odd text in a pie chart generated from a factor variable in ggplot2 used the panel.grid.major.y. However, it will add the major grids rather than ticks like the following:

base + theme(panel.grid.major.y = element_line(colour = "black"))

Created on 2021-12-20 by the reprex package (v2.0.1)

tjebo
  • 21,977
  • 7
  • 58
  • 94
yang
  • 719
  • 3
  • 11
  • Have you checked [this](https://stackoverflow.com/a/45984579/9611950) answer? – Vishal A. Dec 20 '21 at 07:59
  • I guess [this](https://stackoverflow.com/a/33551864/9611950) answers your question. – Vishal A. Dec 20 '21 at 08:01
  • Does this answer your question? [Controlling ticks and odd text in a pie chart generated from a factor variable in ggplot2](https://stackoverflow.com/questions/33549739/controlling-ticks-and-odd-text-in-a-pie-chart-generated-from-a-factor-variable-i) – Vishal A. Dec 20 '21 at 08:01
  • @VishalA. Many thanks for your help. The answer you privided does not work normally for the circular ring plot, and I have updated the questoin. – yang Dec 20 '21 at 09:23

2 Answers2

1

I see two options. You can use the panel grids, but you need to hide them. The usefulness of this solution depends on your intended plot background. I've used white, but this can be customised, of course.

Second option is to fake the ticks with annotation, e.g., with the symbol "|".

Further smaller comments in the code below.

library(tidyverse)
df <- data.frame(
  start = c(0, 121, 241),
  end = c(120, 240, 359),
  group = letters[1:3]
)

ggplot(df) +
  ## annotate with a rectangle, effectively covering your central hole
  annotate(geom = "rect", xmin = 0, xmax = 1, ymin = min(df$start), ymax = max(df$end),
           fill = "white") +
  ## move aes to the geom_layer 
  geom_rect(aes(ymax = end, ymin = start, 
                xmin = 0.8, xmax = 1, 
                fill = group)) +
  coord_polar(theta = "y") +
  xlim(c(0, 1)) +
  theme(panel.grid.major.y = element_line(colour = "black"))

  
## Option 2 - fake the ticks
## the position along the theta axis is defined by y
## you need to change the angle of your fake ticks according to the angle. 
df_annot <- 
  data.frame(y = seq(0,300,100), x = Inf, angle = 360-seq(0,300,100))

ggplot(df) +
  ## annotate with text, along your y
  ## by placing it beneath your geom_rect layer it will automatically be covered 
  geom_text(data = df_annot, aes(x, y, label = "|", angle = angle)) +
  ## move aes to the geom_layer 
  geom_rect(aes(ymax = end, ymin = start, 
                xmin = 0.8, xmax = 1, 
                fill = group)) +
  coord_polar(theta = "y") +
  xlim(c(0, 1)) 

Created on 2021-12-21 by the reprex package (v2.0.1)

tjebo
  • 21,977
  • 7
  • 58
  • 94
0

You need to use panel.grid.minor.y instead of axis.ticks.y in order to change the ticks. Your code will look like this:

base + theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())
base + theme(panel.grid.minor.y = element_line(color = "black", size = 1))

The output will look like this:

enter image description here

Vishal A.
  • 1,373
  • 8
  • 19