简体   繁体   中英

R: annotation a barplot in Plotly

I am having quite a bit of difficultly getting annotations to be in the correct spot. I want the first set the "Critical" in the middle of the bars that contain "Critical" and the "Somewhat Important" in the middle of that bar. I have tried changing the alignment I have looked at SO many different examples I am getting confused. I assume that it the add_annotation (x= ) that needs to be changed . The commented lines are things I have changed or tried with no success. Thanks for any input that can point me in the right direction without giving me the answer directly.

library(plotly)

levels <-c("Critical", "Somewhat_Important", "Not_Important")
y<-c("Predictive Maintenance", "Security", "Self organizing networks",
                "Network Management", "Fraud Assurance", "Data Monetization", "CEM")
Predictive_Maintenance <-c(92, 8, 0)
Security <-c(75, 17, 8)
Self_organizing_networks <-c(67, 33, 0)

Network_Management <-c(58, 42, 0)

Fraud_revenue_assurance <-c(58, 42,0)

Data_Monetization <-c(42, 50, 8)

CEM <-c(42, 50, 8)


ML_Use_cases <-data.frame(rbind(
                      Predictive_Maintenance,
                      Security,
                      Self_organizing_networks,
                      Network_Management,
                      Fraud_revenue_assurance,
                      Data_Monetization,
                      CEM
                      )
)

colnames(ML_Use_cases)<-levels
ML_Use_cases$levels <- factor(ML_Use_cases$levels, levels = c("Critical", "Somewhat Important", "Not important")) 
fig <- plot_ly(ML_Use_cases, x = ~Critical, y = ~y, type = 'bar', orientation = 'h', name = "Critical",
               marker = list(color = 'rgba(216, 101, 34, 0.8)',
                             line = list(color = 'rgba(216, 101, 34, 0.8)',
                                         width = 3)))
fig <- fig %>% add_trace(x = ~Somewhat_Important, name = "Somewhat Important",
                         marker = list(color = 'rgba(216, 131, 82, 0.8)',
                                       line = list(color = 'rgba(216, 101, 34, 0.8)',
                                                   width = 3), textposition="auto"))
fig <- fig %>% add_trace(x = ~Not_Important, name = "Not important",
                         marker = list(color = 'rgba(216, 184, 166, 0.85)',
                                       line = list(color = 'rgba(216, 101, 34, 0.8)',
                                                   width = 3), textposition="auto"))


fig <- fig %>% layout(barmode = 'stack',
                      xaxis = list(title = ""),
                      yaxis = list(title =""))


fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
                              # x = Security, y = levels,
                               text = paste(ML_Use_cases[,"Critical"], '%'),
                               textposition ="inside left",
                               font = list(family = 'San Serif', size = 12,
                                           color = 'rgb(248, 248, 255)', align="center"),
                               showarrow = FALSE) 
#fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
                               #x = Self_organizing_networks +Security, y = y,
 #                              text = paste(ML_Use_cases[,"Somewhat_Important"], '%'),
  #                            # textposition ="inside left",
    #                           font = list(family = 'San Serif', size = 12,
   #                                        color = 'rgb(248, 248, 255)', align="left"),
  #                             showarrow = FALSE) 


fig%>% layout(paper_bgcolor='#3F3A38',plot_bgcolor='#3F3A38',
              catagoryorder="ML_Use_Case",
         font = list(
           family = "San Serif",
           color="#ffffff",
           size=16
      
  )

)

I would suggest an approach where you melt the data and it is easy to work with entirely defined variables. Basically you transform your data and have variables containing all groups an an id name for the rows. You can further format the colors as you want. Next the code:

library(plotly)
library(reshape2)
library(RColorBrewer)

#Data
levels <-c("Critical", "Somewhat_Important", "Not_Important")
y<-c("Predictive Maintenance", "Security", "Self organizing networks",
     "Network Management", "Fraud Assurance", "Data Monetization", "CEM")
Predictive_Maintenance <-c(92, 8, 0)
Security <-c(75, 17, 8)
Self_organizing_networks <-c(67, 33, 0)

Network_Management <-c(58, 42, 0)

Fraud_revenue_assurance <-c(58, 42,0)

Data_Monetization <-c(42, 50, 8)

CEM <-c(42, 50, 8)


ML_Use_cases <-data.frame(rbind(
  Predictive_Maintenance,
  Security,
  Self_organizing_networks,
  Network_Management,
  Fraud_revenue_assurance,
  Data_Monetization,
  CEM
)
)

colnames(ML_Use_cases)<-levels
#Assing rownames as ids
ML_Use_cases$id <- rownames(ML_Use_cases)
rownames(ML_Use_cases) <- NULL

Now we format the data by melting and defining the order for your levels:

#Melt data
Melted <- melt(ML_Use_cases,id.vars = 'id')
Melted$variable <- as.character(Melted$variable)
Melted$variable <- factor(Melted$variable,levels = rev(c("Not_Important",
                                                         "Critical",
                                                         "Somewhat_Important")),ordered = T)

Then, we sketch the plot:

f2 <- plot_ly(Melted, x = Melted$value,
              y = Melted$id,
              type = 'bar',
              name = Melted$variable,
              text = paste0(Melted$value,'%'),
              color = Melted$variable,
              colors = brewer.pal(length(unique(Melted$variable)),
                                  "Paired"))%>%
  layout(barmode = 'stack',hoverlabel = list(bgcolor= 'white') ,bargap = 0.5) %>%
  layout(xaxis = list(categoryorder = 'array',
                      categoryarray = Melted$id), showlegend = T)

The output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM