R绘图极地图简单定制

时间:2019-04-12 13:14:08

标签: r plotly scatter-plot

R和密谋专家,

我最近正在尝试着plotly

这是我简单的最小设计代码

library(plotly)

df <- data.frame(
  r = c(0,1,2,2,1.5,3, 1, 2, 3, 4, 5, 6),
  theta = c(0,45,90,315, 180, 270, 10, 40, 70, 100, 130, 160),
  group = c('R', 'B', 'G', 'R', 'G', 'B', 'R', 'B', 'G', 'R', 'G', 'B'),
  size = c(1, 2, 3, 10, 20, 9, 2, 4, 6, 8, 10, 12)
)

colors_map <- c("blue", "green", "red") 

p <- plot_ly(
  df,
  type = 'scatterpolar',
  r = ~r,
  theta = ~theta,
  color = ~group, 
  colors = colors_map, #Provide a color mapped vector
  size = ~size, 
  sizes = c(100,200), #Provide a range of size.
  text = ~paste('<b>DETAILS </b><br>---------------<br>Radius:', r, '<br>Theta:', theta, '<br>Group:', group, '<br>Size:', size), 
  mode = 'markers',
  hoverinfo="text",
  marker = list(symbol = 'circle', line = list(width = 2, color = 'lightgrey'))
)%>%
  layout(title = 'SAMPLE TITLE',
         showlegend = TRUE,
    polar = list(
      #hole = 0.5,
      radialaxis = list(
        tickfont = list(
          size = 20,
          color = "blue"
        ),
        visible = TRUE,
        tickcolor = toRGB("red"),
        ticks = "outside",
        #ticklen = 15,
        #tickwidth = 20,

        range = c(0, max(df$r)),
        rangemode = "tozero",
        angle = 90,
        tickangle = 90,
        #gridcolor = '#FFF', #color of grid lines
        categoryorder = "array",
        categoryarray = c("GroupA", "GroupB", "GroupC", "GroupD") #Not working
      ),
      angularaxis = list(
        tickfont = list(
          size = 12
        ),
        rotation = 90,
        direction = 'clockwise' ,
        categoryarray = c("A", "B", "C", "D") #Not working
      )
    )
  )
ggplotly(p)

绘制的图表为:

enter image description here

我已经尝试过基于文献的方法,但无法实现。 这是我无法渲染的项目[解决了其中的几个问题并在答案中发布了这些问题]:

  1. [已解决]根据点所属的组显示点的颜色。如果它属于R组,那么颜色应该是红色,如果B然后是蓝色,如果G然后是绿色。

  2. 极坐标图中显示的
  3. [已解决]点的大小非常小,是否可以放大或指定大小范围?

  4. [已解决] ,在每个点的中间,我要显示文本/数字,假设我要显示size < strong>每个点中心的值。我该怎么做?

  5. 删除基于角度的角轴标签,并想将我的自定义标签放入AB,{{1 }},C分别位于45度,135度,225度和315度。

  6. [已解决] 在极坐标图外部的垂直线上显示径向轴标签,而不是在图表内部的显示方式。它应该以垂直规则形式位于图表之外,并带有我的自定义标签,例如DGroupAGroupBGroupC

  7. [已解决] ,使鼠标悬停工具提示放大并在其中显示更多信息。例如,现在,GroupD信息显示在工具提示的外部,应该在行的内部。从工具提示中删除group

  8. 如何用灰色填充同心圆并在上面写上文字?使用theta时,我先在图表底部使用ggplot,然后使用geom_rect来实现它,但是这种方法在coord_polarpolar chart中不起作用。还有其他方法吗?

我了解这对于plotly专家来说可能是基本的事情,但是自最近4个小时以来,我一直在努力使事情变得混乱和复杂。

我想要类似以下内容enter image description here 我相信,由于没有在线帮助,因此这个问题将在帮助初学者自定义绘图极坐标图视觉方面大有帮助,因此可以解决这个问题。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

到目前为止,我已经能够解决已解决制成的商品。

R情节代码:

library(plotly)

df <- data.frame(
  r = c(0,1,2,2,1.5,3, 1, 2, 3, 4, 5, 6),
  theta = c(0,45,90,315, 180, 270, 10, 40, 70, 100, 130, 160),
  group = c('R', 'B', 'G', 'R', 'G', 'B', 'R', 'B', 'G', 'R', 'G', 'B'),
  size = c(1, 2, 3, 10, 20, 9, 2, 4, 6, 8, 10, 12)
)

colors_map <- c("blue", "green", "red") 

p <- plot_ly(
  df,
  type = 'scatterpolar',
  r = ~r,
  theta = ~theta,
  color = ~group, 
  colors = colors_map, #Provide a color mapped vector
  size = ~size, 
  sizes = c(100,200), #Provide a range of size.
  text = ~paste('<b>DETAILS </b><br>---------------<br>Radius:', r, '<br>Theta:', theta, '<br>Group:', group, '<br>Size:', size), 
  mode = 'markers',
  hoverinfo="text",
  marker = list(symbol = 'circle', line = list(width = 2, color = 'lightgrey'))
)%>%
  layout(title = 'SAMPLE TITLE',
         showlegend = TRUE,
         polar = list(
           #hole = 0.5,
           radialaxis = list(
             tickfont = list(
               size = 20,
               color = "blue"
             ),
             visible = TRUE,
             tickcolor = toRGB("red"),
             ticks = "outside",
             #ticklen = 15,
             #tickwidth = 20,

             range = c(0, max(df$r)),
             rangemode = "tozero",
             angle = 90,
             tickangle = 90,
             tickmode="array",
             tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
             ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
             #gridcolor = '#FFF', #color of grid lines
             #categoryorder = "array",
             #categoryarray = c("GroupA", "GroupB", "GroupC", "GroupD") #Not working
           ),
           angularaxis = list(
             tickfont = list(
               size = 12
             ),
             rotation = 90,
             direction = 'clockwise' ,
             categoryarray = c("A", "B", "C", "D") #Not working
           )
         )
  )%>%
  add_trace(
    r = ~r,
    theta = ~theta,
    mode = "text",
    text = ~size,
    textfont = list(color = '#000000', size = 12)
  )  %>%
  add_trace(
    r = ~r, 
    theta = ~theta,
    mode = "marker",
    opacity = 0.5,
    text = ~paste('<b>DETAILS </b><br>---------------<br>Radius:', r, '<br>Theta:', theta, '<br>Group:', group, '<br>Size:', size), 
    hoverinfo = "text"

  ) 

ggplotly(p)

图表: enter image description here