No bold text from ggplot2 to ggplotly

When I use ggplot2 I have bold text but when I want ggpltly font are not bolded .

#######

l<-ggplot(rp1, aes(x=Pril, y=BUCATI,width=0.7,fill=L)) +
geom_bar(stat="identity")+
geom_text(aes(label=format(BUCATI,big.mark = ",",scientific=TRUE)), position = position_stack(vjust= 0.5),
colour = "white",fontface = "bold", size = 3.5)+
scale_fill_manual(values = val)

l

ggplotly(l)

ggplotly has not been designed to be entrely comprehensive, and is not guaranteed to make a perfect plotly version of your (possibly complex and highly customised plot) its only intended as a low effort conversion that will capture the essence of most normal/simple charts that get made in ggplot.
My advice is that if you are doing anything relatively unusual or using a degree of visual customisation; it would be better to write your chart in the direct plotly way and leave ggplot2 out of it.

Thanks a lot .Please give a litle help:How to get that text annotation to appear in bold?

prepare a field containing text; this can support HTML. example

library(plotly)

data <- mtcars[which(mtcars$wt<2.5 & mtcars$mpg>30),]
data$txt <- ifelse(rownames(data) == 'Fiat 128',"<b>Fiat 128</b>",rownames(data))

fig <- plot_ly(data, x = ~wt, y = ~mpg, text = ~txt,type="scatter",
               mode="markers")
fig <- fig %>% add_text(textposition = "top right")
fig <- fig %>% layout(xaxis = list(range = c(1.3, 2.8)),
                      showlegend = FALSE)

fig
1 Like

I need helps .Please .I want number to be bolded(textdf$STOC)
Rplot01

:library(plotly)

df %>%
plot_ly(x = ~PN, y = ~STOC, color = ~Pril,
colors = mycolors,text =df$STOC, textposition ='inside',textfont = list(color = '#000002', size = 12,family ="Arial",position='midlle center' )) %>%
add_bars() %>%
layout(barmode = "stack")%>%
layout(xaxis = list(title = "PN", tickangle = 45))

in HTML tags are used to label text to make bold; you can see in the example I gave.

df$txt <- "df$STOC"
I tried but does worg

df$txt<-df$STOC doesn work

Where are your HTML tags ?

df1$txt<-df$STOC
Capture
see the capture

You have confused your code with the contents of your variables.

You probably mean to do

df1$txt<- paste0("<b>",df$STOC,"</b>")

Doesn work I want in column txt number bold
Capture
See the results

I thiught you were making a plotly chart .? You could use gt for formatting tables for printing.

i want bold number in plot_ly

Below is an example using the mtcars data set. If the format() label is wrapped in <b> tags via the paste0() function and fontface() commented out, the ggplot output is no longer bold and shows the HTML tags, but the plotly output is bold.

library(tidyverse)
library(plotly)

# sample data
df = mtcars |> 
  group_by(cyl) |>
  summarise(mpg = sum(mpg)) |>
  ungroup()

l = ggplot(df, aes(x = cyl, y = mpg, fill = cyl), width = 0.7) +
  geom_bar(stat = 'identity') +
  geom_text(aes(label = paste0('<b>', format(mpg, big.mark = ',', scientific = T), '</b>')),  # UPDATED
            position = position_stack(vjust = 0.5), 
            colour = 'white', 
            # fontface = 'bold',  # UPDATED
            size = 5)

l  

Here is the ggplot output:

Here is the plotly output (bold):

ggplotly(l)

1 Like

Below are the data frame that I used .

Pril CONF ITEM PART/NO STOC DIAM GROS LUNG DINTER
F2722 2029228 36 482-0278 CHG 01 31 160,00 15,50 894 129,00
F2722 2021517 6 553-2140 CHG 01 80 219,10 22,62 1899 173,86
F2722 2029228 2 448-8160 CHG 01 1 263,53 23,05 1075 217,43
F2723 2029228 45 497-8214 CHG 00 1 203,20 19,81 1142 163,58
F2723 2029228 43 496-4936 CHG 01 53 325,00 29,95 875 265,10
F2725 2021517 2 578-3212 CHG 00 62 177,00 11,15 1784 154,70
F2730 2021517 2 578-3212 CHG 00 4 177,00 11,15 1784 154,70
F2734 2021517 6 553-2140 CHG 01 1 219,10 22,62 1899 173,86
F2735 2029228 45 497-8214 CHG 00 14 203,20 19,81 1142 163,58
F2735 2021517 6 553-2140 CHG 01 54 219,10 22,62 1899 173,86
F2735 2029228 33 367-5119 CHG 00 19 235,00 20,35 1302 194,30
F2735 2029228 28 497-8254 CHG 01 23 263,53 23,06 1980 217,41
F2735 2029228 41 497-8255 CHG 00 10 263,53 23,06 1806 217,41
F2745 2021517 2 578-3212 CHG 00 6 177,00 11,15 1784 154,70
F2745 2029228 40 497-8249 CHG 01 3 202,00 16,88 870 168,24
F2745 2029228 38 312-8827 CHG 01 2 219,10 21,65 844 175,80
F2745 2029228 28 497-8254 CHG 01 1 263,53 23,06 1980 217,41
F2790 2391373 1 451-9087 CHG 01 25 295,00 30,92 1204 233,16
library(ggplot2)
library(patchwork)
library(dplyr)
library(crosstalk)
library(plotly)
#############
CAT <- read.csv("D:/TEMP/cat.csv")

x<-strtrim(CAT$PART.NO,8)
x
y<-strtrim(CAT$DINTER,3)
CAT$PN <- x
CAT$GG<-y
#names(CAT)
CAT
################################
str(CAT)
CAT$DINTER <- as.numeric(gsub(",", ".", CAT$DINTER))

library(dplyr)
CAT <- CAT %>% mutate_at(c('DINTER'), as.numeric)
str(CAT)
library(RColorBrewer)
nb.cols <- 45
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)
df <- crosstalk::SharedData$new(CAT)$data() %>%select(Pril,STOC,PN)
df

df %>%
plot_ly(x = ~PN, y = ~STOC, color = ~Pril,
colors = mycolors,text =df$STOC, textposition ='inside',textfont = list(color = '#000002', size = 12,family ="Arial",position='midlle center' )) %>%
add_bars() %>%
layout(barmode = "stack")%>%
layout(xaxis = list(title = "PN", tickangle = 45))

library(tidyverse)
library(RColorBrewer)
library(plotly)
# library(crosstalk)
CAT_1 <- tibble::tribble(
               ~Pril,    ~CONF, ~ITEM,        ~`PART/NO`, ~STOC,  ~DIAM, ~GROS, ~LUNG, ~DINTER,
             "F2722", 2029228L,   36L, "482-0278 CHG 01",   31L,  16000,  1550,  894L,   12900,
             "F2722", 2021517L,    6L, "553-2140 CHG 01",   80L,  21910,  2262, 1899L,   17386,
             "F2722", 2029228L,    2L, "448-8160 CHG 01",    1L,  26353,  2305, 1075L,   21743,
             "F2723", 2029228L,   45L, "497-8214 CHG 00",    1L,  20320,  1981, 1142L,   16358,
             "F2723", 2029228L,   43L, "496-4936 CHG 01",   53L,  32500,  2995,  875L,   26510,
             "F2725", 2021517L,    2L, "578-3212 CHG 00",   62L,  17700,  1115, 1784L,   15470,
             "F2730", 2021517L,    2L, "578-3212 CHG 00",    4L,  17700,  1115, 1784L,   15470,
             "F2734", 2021517L,    6L, "553-2140 CHG 01",    1L,  21910,  2262, 1899L,   17386,
             "F2735", 2029228L,   45L, "497-8214 CHG 00",   14L,  20320,  1981, 1142L,   16358,
             "F2735", 2021517L,    6L, "553-2140 CHG 01",   54L,  21910,  2262, 1899L,   17386,
             "F2735", 2029228L,   33L, "367-5119 CHG 00",   19L,  23500,  2035, 1302L,   19430,
             "F2735", 2029228L,   28L, "497-8254 CHG 01",   23L,  26353,  2306, 1980L,   21741,
             "F2735", 2029228L,   41L, "497-8255 CHG 00",   10L,  26353,  2306, 1806L,   21741,
             "F2745", 2021517L,    2L, "578-3212 CHG 00",    6L,  17700,  1115, 1784L,   15470,
             "F2745", 2029228L,   40L, "497-8249 CHG 01",    3L,  20200,  1688,  870L,   16824,
             "F2745", 2029228L,   38L, "312-8827 CHG 01",    2L,  21910,  2165,  844L,   17580,
             "F2745", 2029228L,   28L, "497-8254 CHG 01",    1L,  26353,  2306, 1980L,   21741,
             "F2790", 2391373L,    1L, "451-9087 CHG 01",   25L,  29500,  3092, 1204L,   23316) 
  CAT <- CAT_1 |> mutate(PN=strtrim(`PART/NO`,8)) |> 
                           select(PN,STOC,Pril) |>
    mutate(txt=paste0("<b>",STOC,"</b>"))

nb.cols <- 45
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)
# df <- crosstalk::SharedData$new(CAT)$data() |>select(Pril,STOC,PN,txt)
# df
df <- CAT 
df |>
  plot_ly(x = ~PN, y = ~STOC, color = ~Pril,
          colors = mycolors,text= ~txt, textposition ='inside',textfont = list(color = '#000002', size = 12,family ="Arial",position='midlle center' )) |>
  add_bars() |>
  layout(barmode = "stack") |>
  layout(xaxis = list(title = "PN", tickangle = 45))

txt is with bold
image

THanks a lot.Working