如何使用ggplot2将轴标签保留在一侧并将轴标题保留在另一侧

Ber*_*rdo 4 plot label axis r ggplot2

我想知道是否有可能(我知道是)将绘图的轴标签保留在绘图的一侧,并将绘图的轴标题保留在另一侧,特别是在离散的 geom_tile() 图中,如下所示: 将轴标题更改为其他位置

RLa*_*ave 5

您可以使用sec.axis = dup_axis()insidescale_x_*()复制两个轴,然后删除您不需要的 inside 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())
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


其他示例和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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明