如何使用ggplot2在一侧保留轴标签而在另一侧保留轴标题 [英] How to keep axis labels in one side and axis title in another using ggplot2

查看:494
本文介绍了如何使用ggplot2在一侧保留轴标签而在另一侧保留轴标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能(我知道是)将图的轴标签保留在图的一侧,而将图的轴标题保留在另一侧,特别是在离散geom_tile()图中,如下所示:

I wonder if it is possible (I know it is) to keep the plot's axis labels in one side of the plot and the plot's axis title in the opposite side, specifically in a discrete geom_tile() plot as follows:

推荐答案

您可以在scale_x_*()内使用sec.axis = dup_axis()复制两个轴,然后删除theme()内不需要的内容.

You can use sec.axis = dup_axis() inside scale_x_*() to duplicate both axis then remove what you don't need 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())

其他示例并具有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()

这篇关于如何使用ggplot2在一侧保留轴标签而在另一侧保留轴标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆