在 ggplotly 中更改悬停 facet_grid
Changing Hover in ggplotly facet_grid
我有一个数据框如下:
DATE<- as.Date(c('2016-11-17','2016-11-17','2016-11-17','2016-11-17', '2016-11-18', '2016-11-18', '2016-11-18','2016-11-18'))
TEST<-c('test', 'test','test','test','test','test','test','test')
TYPE<-c('type1', 'type1', 'type2', 'type2', 'type1', 'type1','type2', 'type2')
CATEGORY<-c('A', 'B','A', 'B','A', 'B','A', 'B' )
Revenue<-c(1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000)
df1<-data.frame(DATE, TEST, TYPE, CATEGORY, Revenue)
df1
DATE TEST TYPE CATEGORY Revenue
1 2016-11-17 test type1 A 1000
2 2016-11-17 test type1 B 2000
3 2016-11-17 test type2 A 3000
4 2016-11-17 test type2 B 4000
5 2016-11-18 test type1 A 5000
6 2016-11-18 test type1 B 6000
7 2016-11-18 test type2 A 7000
8 2016-11-18 test type2 B 8000
然后,当我将 plotly
函数与 ggplot
(ggplotly
) 一起使用以执行 facet_grid 时,它如下所示:
hp <- ggplot(df1, aes(x=DATE, y=Revenue)) +
geom_bar(stat="identity") +
facet_grid(`TYPE`~CATEGORY, scales = "free", space = "free") +
scale_y_continuous(name="Revenue", labels = dollar)
如何更改此悬停部分以将收入显示为美元金额?此外,一个不错的优点是如何更改条形的颜色以表示 DATE
变量。
最重要的是,我需要更改悬停部分,以便将收入表示为美元金额。
任何帮助都会很棒,谢谢!
我只能建议绕过
df1$Revenue2 <- df1$Revenue
df1$Revenue <- paste0(df1$Revenue2, '$')
hp <- ggplot(df1, aes(x=DATE, y=Revenue2, label = Revenue)) +
geom_bar(stat="identity") +
facet_grid(`TYPE`~CATEGORY, scales = "free", space = "free") +
scale_y_continuous(name="Revenue") +
theme(legend.position="none")
ggplotly(hp, tooltip = c("x", "label"))
关于着色,出于某些原因,在我添加 fill
参数后,它会删除悬停文本。真奇怪。
我有一个数据框如下:
DATE<- as.Date(c('2016-11-17','2016-11-17','2016-11-17','2016-11-17', '2016-11-18', '2016-11-18', '2016-11-18','2016-11-18'))
TEST<-c('test', 'test','test','test','test','test','test','test')
TYPE<-c('type1', 'type1', 'type2', 'type2', 'type1', 'type1','type2', 'type2')
CATEGORY<-c('A', 'B','A', 'B','A', 'B','A', 'B' )
Revenue<-c(1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000)
df1<-data.frame(DATE, TEST, TYPE, CATEGORY, Revenue)
df1
DATE TEST TYPE CATEGORY Revenue
1 2016-11-17 test type1 A 1000
2 2016-11-17 test type1 B 2000
3 2016-11-17 test type2 A 3000
4 2016-11-17 test type2 B 4000
5 2016-11-18 test type1 A 5000
6 2016-11-18 test type1 B 6000
7 2016-11-18 test type2 A 7000
8 2016-11-18 test type2 B 8000
然后,当我将 plotly
函数与 ggplot
(ggplotly
) 一起使用以执行 facet_grid 时,它如下所示:
hp <- ggplot(df1, aes(x=DATE, y=Revenue)) +
geom_bar(stat="identity") +
facet_grid(`TYPE`~CATEGORY, scales = "free", space = "free") +
scale_y_continuous(name="Revenue", labels = dollar)
如何更改此悬停部分以将收入显示为美元金额?此外,一个不错的优点是如何更改条形的颜色以表示 DATE
变量。
最重要的是,我需要更改悬停部分,以便将收入表示为美元金额。
任何帮助都会很棒,谢谢!
我只能建议绕过
df1$Revenue2 <- df1$Revenue
df1$Revenue <- paste0(df1$Revenue2, '$')
hp <- ggplot(df1, aes(x=DATE, y=Revenue2, label = Revenue)) +
geom_bar(stat="identity") +
facet_grid(`TYPE`~CATEGORY, scales = "free", space = "free") +
scale_y_continuous(name="Revenue") +
theme(legend.position="none")
ggplotly(hp, tooltip = c("x", "label"))
关于着色,出于某些原因,在我添加 fill
参数后,它会删除悬停文本。真奇怪。