【问题标题】:Annotation / Text for ticklabels in R plotly polar chartR plotly极坐标图中刻度标签的注释/文本
【发布时间】:2019-04-15 09:10:12
【问题描述】:

比方说,我有一个简单的polar chart

R 代码

library(plotly)

p <- plot_ly(
  type = 'scatterpolar',
  r = c(0,1,2,4),
  theta = c(0,45,90,120),
  size = c(10, 20, 30, 40),
  sizes = c(100, 300),
  mode = 'markers'
) %>%
  layout(
    showlegend = FALSE,
    polar = list(
      angularaxis = list(
        showticklabels = TRUE#,
        #tickmode="array",
        #tickvals = c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5),
        #ticktext = c('A', "B", "C", "D", "E", "F", "G", "H")
    ),
      radialaxis = list(
        tickmode="array",
        tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
        ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
      )
  )
  )
ggplotly(p)

图表绘制

当我设置showticklabels = FALSE时,角度刻度标签消失了,然后我想把A,B,C,D,E,F,G and H放在角度c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5)上。

我无法使用ticktextstickvals 获得以下预期的情节。 有人可以帮我找到解决方案吗,或者add_textadd_annotation 是否可行?

预期情节

【问题讨论】:

    标签: r ggplot2 r-plotly polar-coordinates


    【解决方案1】:

    您可以使用showgrid = FALSE 完全删除angularaxis 中的所有网格线,或者您可以从0 开始每22.5 度有一条线,然后ticktext 将类似于c('', 'A', '', 'B', '', 'C', .......),或者您可以繁琐地添加您希望拥有的线条,然后像这样删除网格线:

    p <- plot_ly() %>% 
      # data points
      add_trace(type = 'scatterpolar',
                r = c(0,1,2,4),
                theta = c(0,45,90,120),
                size = c(10, 20, 30, 40),
                sizes = c(100, 300),
                mode = 'markers') %>%
      # straight line from 0 dg to 180 dg
      add_trace(type = 'scatterpolar',
                r = c(0,4.3,4.3),
                theta = c(0, 180, 360),
                mode = 'lines', 
                line = list(color = 'grey', width = 1)) %>%
      # straight line from 45 dg to 225 dg
      add_trace(type = 'scatterpolar',
                r = c(0,4.3,4.3),
                theta = c(0, 45, 225),
                mode = 'lines', 
                line = list(color = 'grey', width = 1)) %>%
      # straight line from 90 dg to 270 dg
      add_trace(type = 'scatterpolar',
                r = c(0,4.3,4.3),
                theta = c(0, 90, 270),
                mode = 'lines', 
                line = list(color = 'grey', width = 1)) %>%
      # straight line from 135 dg to 315 dg
      add_trace(type = 'scatterpolar',
                r = c(0,4.3,4.3),
                theta = c(0, 135, 315),
                mode = 'lines', 
                line = list(color = 'grey', width = 1)) %>%
      # fill circle of radius 1
      add_trace(type = 'scatterpolar', 
                mode = 'lines', 
                r = 1, 
                theta =seq(0, 360, 0.1),
                line = list(color = 'grey'), 
                fill = 'toself', 
                fillcolor = 'grey', 
                opacity = 0.5) %>%
      layout(
        showlegend = FALSE,
        polar = list(
          angularaxis = list(
            showticklabels = TRUE,
            # remove grid lines and ticks
            showgrid = FALSE,
            ticks = '',
            # if you want the axis to go the other way around
            # direction = 'clockwise',
            tickmode="array",
            tickvals = seq(22.5, 337.5, 45),
            ticktext = LETTERS[1:8]
          ),
          radialaxis = list(
            tickmode="array",
            tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
            ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
          )
        )
      )
    ggplotly(p)
    

    输出图表

    我注意到您在那里的预期输出会以相反的方式列出字母,就像您在代码中使用它们一样。如果这也是您想要的,只需更改字母的顺序以匹配这样的角度c("A", "H", "G", "F", "E", "D", "C", "B")(从A 开始的相反顺序)

    【讨论】:

    • 非常感谢!我从我这边试过但无法修复,如何使用add_area 我可以在半径1 的中心填充灰色?
    • 我更愿意再次使用添加跟踪,但可能还有更有效的方法,看看编辑后的答案
    猜你喜欢
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多