在R中绘图(_L):将轴标签添加到定义了categoryorder/categoryarray的轴

t30tvxxf  于 2023-01-18  发布在  Go
关注(0)|答案(1)|浏览(121)

我有类似于下面的数据,我定义了我的x轴categoryorder & categoryarray,这样我的x轴就不是按字母顺序排序了(见:a),已按规定订购(参见:(b)。

a <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar") %>% 
  layout(xaxis = list(title = "x"),
         yaxis = list(title = "y"))
  
xform <- list(categoryorder = "array",
              categoryarray = c("giraffes", 
                                "orangutans", 
                                "monkeys"))

b <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar") %>% 
  layout(xaxis = xform,
         yaxis = list(title = "y"))

我的问题是,一旦我在layout()中写入"xaxis = xform",我就无法为x轴指定任何附加的美观性,除非顺序恢复到(a)中的顺序。例如,我无法添加x轴标题或更改x轴标签的字体大小。
我已经尝试了大量的垃圾邮件组合,即,尝试这个结果在没有x轴标题:

c <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar") %>% 
  layout(xaxis = xform,
         xaxis = list(title = "x"),
         yaxis = list(title = "y"))

...这将添加轴标题,但x轴现在的顺序错误:

d <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar") %>% 
  layout(xaxis = list(xform, title = "x"),
         yaxis = list(title = "y"))

我无法通过搜索找到任何其他人有这个问题的例子,所以这可能是令人尴尬的直接-真的感谢任何帮助。

xuo3flqw

xuo3flqw1#

请尝试xaxis = append(xform, list(title = "x"))xaxis = c(xform, list(title = "x"))

xform <- list(categoryorder = "array",
              categoryarray = c("giraffes", 
                                "orangutans", 
                                "monkeys"))

plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar") %>% 
  layout(xaxis = append(xform, 
                        list(title = "x")),
         yaxis = list(title = "y"))

如果你在layout函数中多次使用一个参数,就像你在plot c中所做的那样,Plotly似乎只识别第一次使用的参数,而忽略重复的参数,因此你的图只有一个有序的x轴,而没有标题。
在图d中,你创建了一个列表的列表,如果写出你的垃圾代码xaxis = list(xform, title = "x"),它看起来像这样:

xaxis = list(list(categoryorder = "array",
     categoryarray = c("giraffes", 
                       "orangutans", 
                       "monkeys")),
     title = "x")

但你需要这样的结构:

xaxis = list(categoryorder = "array",
     categoryarray = c("giraffes", 
                       "orangutans", 
                       "monkeys"),
     title = "x")

因此,您希望使用appendc函数而不是list函数来追加列表xformlist(title = "x")

相关问题