2017-06-04 61 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