[英]R ggplot plotly mouseover tooltip not working. Error: undefined columns selected
我正在尝试在geom_point
对象中绘制和使用ggplotly
进行鼠标悬停。 这是我可重现的 R 脚本:
require(ggplot2)
library(ggrepel)
library(plotly)
# Create the data frame.
sales_data <- data.frame(
emp_name = rep(c("Sam", "Dave", "John", "Harry", "Clark", "Kent", "Kenneth", "Richard", "Clement", "Toby", "Jonathan"), times = 2),
month = as.factor(rep(c("Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Jan"), times = 2)),
dept_name = as.factor(rep(c("Production", "Services", "Support", "Support", "Services", "Production", "Production", "Support", "Support", "Support", "Production"), times = 2)),
revenue = rep(c(100, 200, 300, 400, 500, 600, 500, 400, 300, 200, 500), times = 2),
status = rep(c("Low", "Medium", "Medium", "High", "Very High", "Very High", "Very High", "High", "Medium", "Medium", "Low"), times = 2)
)
sales_data$month <- factor(sales_data$month, levels = c("Jan", "Feb", "Mar"))
month_vector <- levels(sales_data$month)
number_of_enteries <- nrow(sales_data)
sales_data$status <- factor(sales_data$status, levels = c("Low", "Medium", "High", "Very High"))
sales_data$month <- as.integer(sales_data$month)
myChart <- ggplot(sales_data, aes(x = month, y = dept_name)) +
geom_raster(data = expand.grid(sales_data$month, sales_data$dept_name),
aes(x = Var1, y = Var2, width=1, height=1), fill = NA, col = 'gray50', lty = 1) + #default width and height is 1
geom_point(aes(size = status, colour = cut(revenue, c(-Inf, 199, 301, Inf)) ),
shape = 16, position = position_jitter(seed = 0), show.legend = F) +
scale_color_manual(name = "revenue",
values = c("(-Inf,199]" = "red",
"(199,301]" = "#ffbf00", #amber
"(301, Inf]" = "green") ) +
geom_text(aes(label = revenue), size=4, vjust = 1.6, position = position_jitter(seed = 0)) + #try with geom_text
theme_bw() +
theme(
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_blank(),
axis.line = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.text = element_text(colour = "blue", face = "plain", size =11)
) +
scale_x_continuous(limits=c(0.5,3.5), expand = c(0,0), breaks = 1:length(month_vector), labels = month_vector) +
scale_y_discrete(expand = c(0,0)) +
geom_hline(yintercept = as.numeric(sales_data$dept_name) + 0.5) +
geom_vline(xintercept = as.numeric(sales_data$month) - 0.5, color = "grey")
#ggplotly(myChart)
myChart
它给了我错误:
[.data.frame
(g, , c("fill_plotlyDomain", "fill")) 中的错误:
选择了未定义的列
如果我注释ggplotly(myChart)
并取消注释myChart
它会正确myChart
这是输出图表:
任何建议或帮助,我如何在鼠标悬停的工具提示中查看revenue
数据和其他一些细节?
提前致谢!
有ggiraph
( https://davidgohel.github.io/ggiraph/articles/offcran/using_ggiraph.html ) 但由于我不知道鼠标悬停时你想显示什么,我只能提供一个不完整的版本:
myChart <- ggplot(sales_data, aes(x = month, y = dept_name)) +
geom_raster(data = expand.grid(sales_data$month, sales_data$dept_name),
aes(x = Var1, y = Var2, width=1, height=1), fill = NA, col = 'gray50', lty = 1) + #default width and height is 1
geom_point_interactive(aes(tooltip = status, data_id = status, colour = cut(revenue, c(-Inf, 199, 301, Inf)) ),
shape = 16, position = position_jitter(seed = 0), show.legend = F) +
scale_color_manual(name = "revenue",
values = c("(-Inf,199]" = "red",
"(199,301]" = "#ffbf00", #amber
"(301, Inf]" = "green") ) +
geom_text(aes(label = revenue), size=4, vjust = 1.6, position = position_jitter(seed = 0)) + #try with geom_text
theme_bw() +
theme(
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.background = element_blank(),
axis.line = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.text = element_text(colour = "blue", face = "plain", size =11)
) +
scale_x_continuous(limits=c(0.5,3.5), expand = c(0,0), breaks = 1:length(month_vector), labels = month_vector) +
scale_y_discrete(expand = c(0,0)) +
geom_hline(yintercept = as.numeric(sales_data$dept_name) + 0.5) +
geom_vline(xintercept = as.numeric(sales_data$month) - 0.5, color = "grey")
library(ggiraph)
ggiraph(myChart2)
girafe(code = print(myChart) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.