簡體   English   中英

R繪圖極坐標圖中的刻度標簽的注釋/文本

[英]Annotation / Text for ticklabels in R plotly polar chart

假設我有一個簡單的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設置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嗎?

預期地塊

在此處輸入圖片說明

您可以使用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倒序)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM