0

我在 R 中有一个带有一些嵌套 ggplotly 对象的可反应表。我希望减少间距/填充/行高。

这是代码:

df<-data.frame('Question Category'=letters[1:5), 
Favorable=c(37,36.2,34.8,34.6,34.9),
Neutral=c(19.5,20.4,18.8,18.8,20.9), 
Not_Favorable = c(35.5,35.3,38.4,38.6,36.3),
DNK=c(8,8,8,8,8))  


df2<-data.frame(df[,1])
df2[,2]<-c(1:nrow(df2))
names(df2)<-c('Question Category','Favorability')
plot_list<-list()
for(i in 1:nrow(df2)){
    subset_df <- df[i,3:ncol(df)]
    subset_df<-tibble::rownames_to_column(data.frame(t(subset_df)))
    subset_df[,3]<-df[i,1]
    names(subset_df)<-c('Category','Score','Question_category')
    subset_df2<-subset_df %>% mutate(lab_ypos = cumsum(Score)-0.5*Score)

    g<-ggplotly(
      ggplot(subset_df2, aes(x=Question_category,y=Score,fill=Category))+
      geom_bar(position='stack',stat='identity',width = 0.1)+ 
      coord_flip()+
      geom_text(aes(y=lab_ypos, label=Score),color='white') +
        scale_y_continuous(expand = c(0, 0)) + scale_x_discrete(expand = c(0, 0))+
      theme(
      panel.background = element_blank(),
      panel.grid=element_blank(),
            axis.text = element_blank(),
            axis.title = element_blank(),
            legend.position = 'none',
            axis.ticks = element_blank()
            ),
height=180)
 plot_list[[i]]<-g }
  
p<-reactable(df2, columns = list(
    Favorability = colDef(cell = function(value,index){
        plot_list[[index]]
     })
  ), bordered = TRUE, compact=TRUE)
  p

这是输出

在此处输入图像描述

这是所需的输出

在此处输入图像描述

谢谢您的帮助

4

0 回答 0