您可以在scale_x_*()
内部使用sec.axis = dup_axis()
来复制两个轴,然后在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()
discrete
的解决方法 https://dev59.com/41cO5IYBdhLWcg3wtzxg。 - RLaveas.numeric()
,然后就可以使用scale_*_continuos()
进行操作了。 - RLave