2017-06-04 64 views
0

更新:爲數據添加最小可重現代碼。ggplot中的堆疊條形圖轉換爲plotly時不會呈現正確

即時通訊設法將一個ggplot轉換成有光澤的圖表。問題是,在ggplot中,堆疊的條形圖(stat = identity)很好地堆疊起來,兩者之間沒有任何空格,而當我轉換爲plotly時,每個項目之間都有這些空格。

我不生產整個閃亮的代碼,因爲它很難遵循。然而這裏的圖像和更簡化的代碼(而不是閃亮的版本)

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 

這會產生這一形象有些什麼樣的這一點,這就是我想要的東西。 enter image description here

但是,因爲它不是交互式的,所以我需要一個劇情圖像,它顯示了當光標盤旋時總資本費用類型。所以,我使用下面

t4<-ggplotly(t3) 
t4 

現在的代碼所產生的plotly積有各個顏色類(Capitalchargetype),我想避免之間的白線(每個單項的),也工具提示也產生個別項目而不是每個CapitalChargeType的總和 enter image description here

+0

您可以提供數據的重複的例子? – yeedle

+0

現在完成。謝謝。 – ashleych

回答

0

問題的方式是積極處理堆積的因素。每個因子被包裝在一個默認爲白色的邊框中。有一個非常簡單的解決方法:只需將color = CapitalChargeType添加到ggplot對象。

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

+0

但是,此工具提示仍然顯示單個值,而不是每個CapitalChargeType的總和。我意識到解決這個問題的一種方法是更改​​數據並繪製計算出的組合總數,但是有一個我缺少的繪圖功能,通過它可以使工具提示顯示總和?並感謝給我一個更好的方式來創建最小可重現的例子。 – ashleych