gpt4 book ai didi

r - 如何使用ggplot2将 Axis 标签保留在一侧, Axis 标题保留在另一侧

转载 作者:行者123 更新时间:2023-12-02 02:40:50 32 4
gpt4 key购买 nike

我想知道是否有可能(我知道是)将绘图的 Axis 标签保留在绘图的一侧,将绘图的 Axis 标题保留在另一侧,特别是在离散的 geom_tile() 绘图中,如下所示: change axis title to other position

最佳答案

您可以在 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())

enter image description here

<小时/>

其他带有 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()

enter image description here

关于r - 如何使用ggplot2将 Axis 标签保留在一侧, Axis 标题保留在另一侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54828600/

32 4 0