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
CodePudding user response:
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)