Вы можете использовать sec.axis = dup_axis()
внутри scale_x_*()
, чтобы дублировать обе оси, а затем удалить то, что вам не нужно внутри theme()
.
ggplot(mtcars, aes(x=mpg, y=hp)) +
geom_point() +
labs(title="mpg vs hp") +
scale_y_continuous(position = 'right', sec.axis = dup_axis()) +
#remember to check this with the proper format
scale_x_continuous(position = "top", sec.axis = dup_axis()) +
theme(plot.title = element_text(hjust=0.5),
axis.text.x.top = element_blank(), # remove ticks/text on labels
axis.ticks.x.top = element_blank(),
axis.text.y.right = element_blank(),
axis.ticks.y.right = element_blank(),
axis.title.x.bottom = element_blank(), # remove titles
axis.title.y.left = element_blank())
Другой пример и с функцией theme_new()
:
theme_new <- function() {
theme(plot.title = element_text(hjust=0.5),
axis.text.x.top = element_blank(), # remove ticks/text on labels
axis.ticks.x.top = element_blank(),
axis.text.y.right = element_blank(),
axis.ticks.y.right = element_blank(),
axis.title.x.bottom = element_blank(), # remove titles
axis.title.y.left = element_blank())
}
ggplot(df, aes(x, y)) +
geom_tile(aes(fill = z), colour = "grey50") +
labs(title="some title") +
scale_y_continuous(position = 'right', sec.axis = dup_axis()) +
scale_x_continuous(position = "top", sec.axis = dup_axis()) +
theme_new()