0
votes

update: adding a minimum reproducible code for the data.

im trying to convert a ggplot to plotly chart in shiny. The problem is that in ggplot, the stacked bar chart (with stat =identity) stacks up nicely without any spaces in between, whereas when i convert to plotly, there are these spaces in between each item.

I am not producing the entire code for shiny, as it is difficult to follow. However here are the images and a much simplified code (not the shiny version)

t<- 1:50
GIB_Rating = rep(c('2','3','4+','5','5-','7','8','6','6+','5'),5)
t1<-data.frame(t,GIB_Rating)
CapitalChargeType = c('Credit_risk_Capital','NameConcentration','SectorConcentration')
t2<- expand.grid(t=t, CapitalChargeType=CapitalChargeType)
t3<-left_join(t2,t1)
New = rnorm(150, mean=100,sd=250)
t3<- data.frame(t3,New)

t3<- ggplot(t3, aes(x=GIB_Rating, y=New, fill=CapitalChargeType)) + geom_bar(stat='identity') 
t3

this produces this image some what like this, which is exactly what I want. enter image description here

However as it is not interactive, I want a plotly image, which shows the total of capital charge type when cursor hovers over. so, I use the code below

t4<-ggplotly(t3)
t4

the plotly plot now produced has white lines (for each individual item) in between each color class (Capitalchargetype), which i want to avoid, also the tooltip also produces individual items rather than the sum of each CapitalChargeType enter image description here

1
can you provide a reproducible example of your data?yeedle
Done now. Thanks.ashleych

1 Answers

0
votes

The issue is in the way plotly handles stacked bars of factors. Each factor gets wrapped in a border which is white by default. There's a very easy workaround: Just add color = CapitalChargeType to the ggplot object.

library(tidyverse)
df <- data_frame(
  t = 1:50,
  GIB_Rating = rep(c('2','3','4+','5','5-','7','8','6','6+','5'),5)
)

df <- df %>%
  expand(t, CapitalChargeType = 
c('Credit_risk_Capital','NameConcentration','SectorConcentration')) %>%
  left_join(df) %>%
  mutate(New = rnorm(150, mean=100,sd=250))

g <- ggplot(df, aes(x = GIB_Rating, y = New, fill = CapitalChargeType, color = CapitalChargeType)) + 
  geom_bar(stat='identity') 

plotly::ggplotly(g)

plotly output