使用 ggplotly 进行缩放后的不同工具提示
Different tooltips after scale shift with ggplotly
我正在尝试使用 ggplot 和 ggplotly 制作组合条形图和点图。我正在对条形图使用比例偏移,以便原点从 100 开始。我成功了,但是条形图和点的工具提示值不同:条形图改变了值,而点保留了原始值。我希望两个工具提示都具有相同的 'correct'(非移位值)。这可能吗?
library(ggplot2)
library(dplyr)
library(ggthemes)
library(plotly)
set.seed(369)
values <- round(100 + rnorm(6) * 10, 2)
df <- data.frame(Year = rep(2014:2019, 2),
value = rep(values, 2),
Indicator = "Indicator1",
Type = rep(c("Bar", "Point"), each = 6))
p <- ggplot(df, aes(value))
bars <- df %>%
filter(Type == "Bar")
points <- df %>%
filter(Type == "Point")
t_shift <- scales::trans_new("shift",
transform = function(x) {x - 100},
inverse = function(x) {x + 100})
pl <- p +
geom_bar(data = bars,
aes(fill = Indicator, group = Indicator,x = Year, y = value), stat = "identity", position = "dodge") +
geom_point(data = points, aes(x = Year, y = value, group = Indicator)) +
scale_fill_manual(values=c("#00A6CE", "#A83D72"))+
theme_tufte() +
theme(legend.title = element_blank()) +
scale_y_continuous(limits = c(80, 120), trans = t_shift)
ggplotly(pl, tooltip = c("value"))
我认为最简单的方法是为两个数据集复制 value
并将其包含在 ggplot
图表的另一个变量中。
这段代码在我的电脑上有效:
bars$Value <- bars$value
points$Value <- points$value
pl <- p +
geom_bar(data = bars,
aes(fill = Indicator, group = Indicator,x = Year, y = value,z=Value), stat = "identity", position = "dodge") +
geom_point(data = points, aes(x = Year, y = value, group = Indicator,z=Value)) +
scale_fill_manual(values=c("#00A6CE", "#A83D72"))+
theme_tufte() +
theme(legend.title = element_blank()) +
scale_y_continuous(limits = c(80, 120), trans = t_shift)
ggplotly(pl, tooltip = c("Year","Value"))
我正在尝试使用 ggplot 和 ggplotly 制作组合条形图和点图。我正在对条形图使用比例偏移,以便原点从 100 开始。我成功了,但是条形图和点的工具提示值不同:条形图改变了值,而点保留了原始值。我希望两个工具提示都具有相同的 'correct'(非移位值)。这可能吗?
library(ggplot2)
library(dplyr)
library(ggthemes)
library(plotly)
set.seed(369)
values <- round(100 + rnorm(6) * 10, 2)
df <- data.frame(Year = rep(2014:2019, 2),
value = rep(values, 2),
Indicator = "Indicator1",
Type = rep(c("Bar", "Point"), each = 6))
p <- ggplot(df, aes(value))
bars <- df %>%
filter(Type == "Bar")
points <- df %>%
filter(Type == "Point")
t_shift <- scales::trans_new("shift",
transform = function(x) {x - 100},
inverse = function(x) {x + 100})
pl <- p +
geom_bar(data = bars,
aes(fill = Indicator, group = Indicator,x = Year, y = value), stat = "identity", position = "dodge") +
geom_point(data = points, aes(x = Year, y = value, group = Indicator)) +
scale_fill_manual(values=c("#00A6CE", "#A83D72"))+
theme_tufte() +
theme(legend.title = element_blank()) +
scale_y_continuous(limits = c(80, 120), trans = t_shift)
ggplotly(pl, tooltip = c("value"))
我认为最简单的方法是为两个数据集复制 value
并将其包含在 ggplot
图表的另一个变量中。
这段代码在我的电脑上有效:
bars$Value <- bars$value
points$Value <- points$value
pl <- p +
geom_bar(data = bars,
aes(fill = Indicator, group = Indicator,x = Year, y = value,z=Value), stat = "identity", position = "dodge") +
geom_point(data = points, aes(x = Year, y = value, group = Indicator,z=Value)) +
scale_fill_manual(values=c("#00A6CE", "#A83D72"))+
theme_tufte() +
theme(legend.title = element_blank()) +
scale_y_continuous(limits = c(80, 120), trans = t_shift)
ggplotly(pl, tooltip = c("Year","Value"))