Nick Knauer
Nick Knauer

Reputation: 4243

Plotly Facet_Wrap Bar Graph Not Working

I have a dataset as follows:

DATE <- as.Date(c('2016-11-15','2016-11-15','2016-11-15', '2016-11-15', '2016-11-16', '2016-11-16', '2016-11-16', '2016-11-16'))
TYPE1 <- c('A','A','A', 'A','A','A','A','A')
TYPE2 <- c('uno','uno','dos', 'dos','uno','uno','dos','dos')
TYPE3 <- c('bueno', 'mal','bueno','mal','bueno','mal','bueno','mal')
Revenue <- c(97310.77, 51481.13, 1842.42, 2157.38, 55735.40, 56917.75, 1862.75, 551.23)
df <- data.frame(DATE, TYPE1, TYPE2, TYPE3, Revenue)

df

        DATE TYPE1 TYPE2 TYPE3  Revenue
1 2016-11-15     A   uno bueno 97310.77
2 2016-11-15     A   uno   mal 51481.13
3 2016-11-15     A   dos bueno  1842.42
4 2016-11-15     A   dos   mal  2157.38
5 2016-11-16     A   uno bueno 55735.40
6 2016-11-16     A   uno   mal 56917.75
7 2016-11-16     A   dos bueno  1862.75
8 2016-11-16     A   dos   mal   551.23

My goal here is to create a facet_wrap ggplot that is interactive using plotly: https://plot.ly/ggplot2/getting-started/

library(scales)
graph<-ggplot(df, aes(x=DATE, y= Revenue, fill = DATE)) + geom_bar(stat = "identity") +labs(x = "Date", y= "Revenue") + scale_x_date(labels = date_format("%m-%d"))+facet_wrap(~`TYPE2`+`TYPE3`)

ggplotly(graph)

enter image description here

The quality is really not that great because I want to be able to hover over it which is not working and when I autoscale, the numbers get removed from the side.

Goals to achieve from this post:

1) When hovering over the each bar, display value details

2) Change format of numbers to be $ amounts

3) Remove legend on the side where it shows date, I colored in the bars by date but I don't need the legend.

4) When autoscaling, keep numbers on the side.

I know this is a lot but if anyone is able to answer either of these questions (preferably the 1st goal) that would be a huge help. Thanks!

Upvotes: 1

Views: 1373

Answers (2)

Nick Knauer
Nick Knauer

Reputation: 4243

Figured it out:

library(scales)
hp <- ggplot(df, aes(x=DATE, y=Revenue)) + 
geom_bar(stat="identity", colour = "white")+
facet_grid(`TYPE2`~`TYPE3`, scales = "free", space = "free")

Upvotes: 1

HubertL
HubertL

Reputation: 19544

To enable hovering, you can move the fill argument to geom_bar aesthetics:

graph<-ggplot(df, aes(x=DATE, y= Revenue)) + 
       geom_bar(aes( fill = DATE), stat = "identity") 
ggplotly(graph)

Upvotes: 1

Related Questions