Home > Mobile >  Readable names on X-axis
Readable names on X-axis

Time:10-31

I am trying to plot Barchart with around 65 names on X-axis with Plotly. Below you can see the code

library(plotly)
library(data.table)
df1<-structure(list(code = c("01", "02", "03", "10-12", 
                                              "13-15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
                                              "25", "26", "27", "28", "29", "30", "31-32", "33", "35", "36", 
                                              "37-39", "45", "46", "47", "49", "50", "51", "52", "53", "58", 
                                              "59-60", "61", "62-63", "64", "65", "66", "68<U 0410>", "68B", 
                                              "69-70", "71", "72", "73", "74-75", "77", "78", "79", "80-82", 
                                              "84", "85", "86", "87-88", "90-92", "93", "94", "95", "96", "B", 
                                              "F", "I", "T"), rate = c(0.18, 0.18, 0.18, 0.25, 
                                                                                    0.18, 0.12, 0.11, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                                                    0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                                                    0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                                                    0.18, 0.13, 0.13, 0, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                                                    0.18, 0.18, 0.14, 0.15, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                                                    0.18, 0.18, 0.18, 0.15, 0.18)), row.names = c(NA, -64L), class = "data.frame")

df1<-data.frame(df1)

plt <- plot_ly(df1) %>% 
  add_trace(x = ~code, y = ~rate, type = 'bar', name = 'Test') %>% 
    layout(
    xaxis = list(title = ''), 
    yaxis = list(title = ''),
    legend = list(x = 0.9, y = 0.99),
    barmode = 'group'
  )

plt

This code gives me Barchart, which is not fully readable on X-axis, and I can't see the full names of each name on the axis. enter image description here

For example name 10-12 is not readable on the axis and so on.

So can anybody help me how to solve this problem and to see all names on X-axis

CodePudding user response:

Actually this depends upon the two things. First the canvas dimensions (size). You code is returning correct x axis if output is simly rescaled. Here is the screenshot. enter image description here

The second. The font size. If the canvas size is OK adjust the font size of axis labels here:

plt <- plot_ly(df1) %>% 
  add_trace(x = ~code, y = ~rate, type = 'bar', name = 'Test') %>% 
  layout(
    xaxis = list(title = '',
                 tickfont = list(size = 5) # <<<<<< HERE
                 ), 
    yaxis = list(title = ''),
    legend = list(x = 0.9, y = 0.99),
    barmode = 'group'
  )

You can set it as big or small as you wish. You can also read enter image description here

I put the font size on second place as it should be adjusted after the canvas size is known and finally set to the necessary value.

  • Related