Setting ranges for a bar chart in R and displaying count of items

丶灬走出姿态 提交于 2019-12-06 15:21:41
iris$sepal_gp <- cut(iris$Sepal.Length, breaks=3, include.lowest=TRUE)
ggplot(iris, aes(sepal_gp, fill=Species)) + geom_bar()

Edit:

ggplot(iris, aes(sepal_gp, fill=sepal_gp)) + geom_bar()

To remove legend and put in custom hover text in ggplotly:

(i) calculate the count for each group

library(dplyr)
iris <- iris %>% 
     group_by(sepal_gp) %>% 
     mutate(sepal_gp_count = n())

(ii) insert custom hover text in ggplot:

 p <- ggplot(iris, aes(sepal_gp, fill=sepal_gp, text=paste("Case: <br>", sepal_gp, "<br> Count: <br>", sepal_gp_count))) + 
      geom_bar() 

(iii) plot with ggplotly:

ggplotly(p, tooltip="text") %>%
     layout(showlegend=FALSE)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!