首页 > 解决方案 > 在R中为背靠背条形图悬停

问题描述

样品表

Animals <- read.table( header=TRUE, text='Options_ord Reason Gender Count 1 No Genuine M 24 2 Yes Genuine F -16 3 No Misclassified M 85 4 No Misclassified F 41 5 Yes Taxonomic F -2 6 Yes Taxonomic M 7 7 否 不清楚 F -41 8 是 不清楚 M 117' )

#尝试在R中制作背靠背条形图

>     trace_graph3<- ggplot(Animals, aes(x = (Animals$Options_ord), y = 
          Animals$Count, fill = Gender)) +
          geom_bar(stat = "identity") + 
          facet_share(~Gender, dir = "h", scales = "free", reverse_num = 
           TRUE) + theme_bw(base_size = 9.5)+
          coord_flip() +
           theme_minimal()

打印图表

打印(trace_graph3 + 实验室(y="in %", x = ""))

对于上图,我想创建一个悬停。我尝试了 ggplotly 但不幸的是它抛出了我尝试在 R 中应用非函数的错误

标签: r

解决方案


我建议安装和使用 plotly 。你所需要的只是用 plotly 包装你的普通 ggplot。在交互性方面,Plotly 非常强大而简单。

install.packages('plotly')
library(plotly)

ggplotly(trace_graph3) 

推荐阅读