• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

ggplot2的分面标签和坐标轴位置

武飞扬头像
甲戌十三画生
帮助1

ggplot2的分面标签和坐标轴位置

分面标签位置

使用ggplot2作图经常要用到分面,然而分面标签的默认位置要么在上,要么在左。

ggplot(ToothGrowth, aes(x = dose, y = len))   
  geom_boxplot(aes(fill = supp), position = position_dodge(0.9))  
  facet_grid(dose ~ supp)

学新通
然而,某些情况下,需要将分面标签显示于左边或者下边。
百度了很久,没找到答案,快以为ggplot2没此功能时,毅然选择相信ggplot2(毕竟强大如斯)。
一个一个地查阅帮助文档,终于功夫不负有心人。
设置如下:

  ggplot(ToothGrowth, aes(x = dose, y = len))   
  geom_boxplot(aes(fill = supp), position = position_dodge(0.9))  
  facet_grid(supp~.,switch='y') #从右边移至左边
  #当switch='x'时,可将位于上边的分面标签移至下边

学新通

若要分面标签显示于坐标轴外侧,则可在theme中进行设置strip.placement=‘outside’。

ggplot(ToothGrowth, aes(x = factor(dose), y = len))   
  geom_boxplot(aes(fill = supp), position = position_dodge(0.9))  
  facet_grid(supp~.,switch='y') 
  theme(strip.placement='outside')

学新通

坐标轴位置

若要坐标轴也换位置,则需要在坐标轴的标度scale中进行设置,如下:

ggplot(ToothGrowth, aes(x = factor(dose), y = len))   
  geom_boxplot(aes(fill = supp), position = position_dodge(0.9))  
  scale_y_continuous(position = 'right') 
  facet_grid(supp~.,switch='y') 
  theme(strip.placement='outside')

学新通
以上,今日所学,望可以帮助到各位大兄弟。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggjibe
系列文章
更多 icon
同类精品
更多 icon
继续加载