- 更改x轴分类标签的名称
scale_x_discrete(labels=c('A', 'B'))
- 更改x轴分类标签的顺序
#方法1,在x轴映射里面改因子数据的层次
factor(team, level=c('Mavs', 'Heat', 'Nets', 'Lakers')
#方法2,
scale_x_discrete(limit = c('a', 'd', 'c','b', 'f', 'e'))
ref:r - How do you specifically order ggplot2 x axis instead of alphabetical order? - Stack Overflow
- 更改facet分面标签文本及顺序
facet_grid(~factor(team, levels=c('C', 'D', 'A', 'B')),## 更改标签顺序
labeller = as_labeller(c(A='new1', B='new2', C='new3', D='new4'))) ## 更改标签内容
- 更改legend的标签文本及顺序
### 更改图例标题文本
+ labs(fill = "xxxx")
###翻转图例顺序
# Use guides function to reverse legend for
# a specific aesthetic (fill, linetype, shape or color)
p + guides(fill = guide_legend(reverse=TRUE))
# Or use scale_fill_discrete (same as above)
p + scale_fill_discrete(guide = guide_legend(reverse=TRUE))
### 自定义图例子标签顺序
# Change the order of legend items
p + scale_x_discrete(limits=c("B", "C", "A"))
### 更改图例标题和子标签文本
# Edit legend title and labels
p + scale_fill_discrete(name = "legend name", labels = c("A", "B", "C"))
## 或者
scale_fill_manual(
values = c("#7ecaa5", "#feb769"), ## 改颜色
name = NULL, ## 改名字
labels = c("Baseline\n(No-weighted)", "KE-weighted") ## 改子标签名字
)
- 更改y轴的位置,
scale_y_continuous(position = "right")
- 坐标轴数值转百分比
labels = scales::percent