R绘图极坐标图中的勾选标签的注释/文本

问题描述 投票:0回答:1

比方说,我有一个简单的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)

绘制的图表:enter image description here

当我设置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

预期情节:

enter image description here

r ggplot2 r-plotly polar-coordinates
1个回答
1
投票

你可以用angularaxisshowgrid = FALSE中完全删除所有网格线,或者你可以从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)

输出图表:

enter image description here

我注意到你在那里的预期输出,在你的代码中将它们反过来列出了字母。如果这是你还想要改变字母的顺序以匹配角度,如c("A", "H", "G", "F", "E", "D", "C", "B")(从A开始的逆序)

© www.soinside.com 2019 - 2024. All rights reserved.