Search code examples
rplotlyscatter-plot

R plotly polar chart simple customizing


Hi R and plotly experts,

I am trying my hands on plotly lately.

Here is my simple minimal design code:

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)

The chart ploted is:

enter image description here

I have tried something based on documentaion but couldn't achieve. Here are the items I failed to render[Few of them are solved and posted in answer]:

  1. [SOLVED] Show color of points based on which group it belongs. If it belongs to R group, then color should be red, if B then blue and if G then green.

  2. [SOLVED] Size of points shown in polar chart is quite small, is there a way I can enlarge it or give a range of size ?

  3. [SOLVED] In middle of each point, I want to show a text/number, let's say I want to show size value on the center of each point. How I can do this ?

  4. remove the angle based angular axis labels and want to put my custom label as A, B, C, D at 45deg, 135deg, 225deg and 315deg respectively.

  5. [SOLVED] show radial axis label in vertical line outside polar chart, instead, of how it is present inside the chart. It should be outside the chart in vertical rule form, with my custom labels let's say GroupA, GroupB, GroupC, GroupD.

  6. [SOLVED] Make mouseover tooltip to be enlarged and show more information inside it. For example, Right now, the group information is shown outside the tooltip, it should be inside the row. Removing theta from tooltip.

  7. How to have concentric circle with grey color filled and a text written on it.? With ggplot I implemented it using a geom_rect at the bottom of chart and then coord_polar, but that approach will not work in polar chart with plotly. Any other approach ?

I understand this could be basic things for plotly experts, but I am struggling and complicating things out of order since last 4 hours.

I want something similar as below: enter image description here I am sure, this question will go long way in helping beginners in customizing plotly polar chart visuals as no online help is available and SO question addressing it. Any help is appreciated.


Solution

  • So far, I am able to solve SOLVED maked items.

    R plotly code:

    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)
    

    Chart: enter image description here