library(ggplot2)
library(plotly)
## Warning: package 'plotly' was built under R version 4.0.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(shiny)
## Warning: package 'shiny' was built under R version 4.0.3
dfsub<-response[order(response$cat, response$TRT01PN),]
dfsub<-as.data.frame(dfsub)
#dfsub<-dfsub[dfsub$response_percentage != 0,]
dfsub$TRT01PN<-factor(dfsub$TRT01PN, levels=c("1","2","3","4","5","6"))
dfsub$response_percentage[dfsub$response_percentage==0]<-23
#fill used text instead of categorical variable to avoid tooltip issue
test<-ggplot(dfsub,aes( frame=cat))+
geom_tile(mapping=(aes(x=TRT01PN, y=response_percentage/2,height=response_percentage,fill="response percentage",width=0.8)),alpha=1)+
scale_fill_manual(values=c("skyblue"))+
# text in x-axis (requires clip = "off" in coord_*)
# paste(country, " ") is a hack to make pretty spacing, since hjust > 1
# leads to weird artifacts in text spacing.
geom_text(aes(x=TRT01PN, y = response_percentage, label = response_percentage), vjust = 1, hjust =1) +
scale_y_continuous(labels = scales::comma) +
# guides(color = FALSE, fill = FALSE) +
scale_x_discrete("",labels=c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17"))+
guides(color = FALSE, fill = FALSE) +
labs(title='Response percentage based on subgroup', y="Response Percentage") +
theme(plot.title = element_text(hjust = 0.5, size = 12),
#axis.ticks.x = element_blank(), # These relate to the axes post-flip
#axis.text.x = element_blank(), # These relate to the axes post-flip
axis.text.x=element_text(size=6),
plot.margin = margin(1,1,1,4, "cm"))
test
#ggplotly(test)%>%layout(showlegend=FALSE)
div(
ggplotly(test, width=1020, height=620, tooltip=NULL)%>%layout(showlegend=FALSE)%>%animation_opts(1000), align="center")
techincal reference: https://www.rdocumentation.org/packages/plotly/versions/4.9.2.1/topics/animation_opts