从R修改C3图的x轴标签

问题描述

使用R脚本,我想修改使用c3软件包创建的绘图的x轴标签。

在文档here中,在xAxis下指出categories参数是一个字符向量,并且“可以用于修改轴标签。如果已经在数据中定义,则不需要”。

在Javascript中,here当然看起来可能。

这是我修改x轴标签的失败尝试。

library("tidyverse")
library("c3")

data.frame(a=c(1,2,3,2),b=c(2,1,5)) %>%
  c3() %>%
  xAxis(categories = c("A","B","C")) 

解决方法

问题是在帮助文件中找到了默认的xAxis type = 'indexed'

type    character on of 'indexed','timeseries' or 'category'

因此,如果您使用type="category",它将可以正常工作:

library("tidyverse")
library("c3")

data.frame(a=c(1,2,3,2),b=c(2,1,5)) %>%
  c3() %>%
  xAxis(type="category",categories = c("A","B","C")) 

enter image description here

因此,x轴现在具有A,B和C,但以3结束,因为它的索引为0、1、2、3,并且您仅提供了3个类别(A,B,C),因此它保持了最后一个类别为3

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...