我有一系列为R Shiny Jmeter 板制作的条形图,当用户将鼠标悬停在条形图上时,我希望年份(x轴)和成本金额(y轴)显示在条形图上。
我在集成这个方面遇到了麻烦。我一直在尝试使用plotly包。似乎许多实现工具提示的常用方法与ggplot不兼容。我想继续使用ggplot,因为我在这个 Jmeter 板上实现的大多数东西都使用它。下面是其中一个图的工作版本:
output$costs_plot <- renderPlot({
# Subset data based on number of years to display
data_subset <- costShortfallDf()[1:input$num_years, ]
# Create a new column to store the color based on the sign of the cost
data_subset$color <- ifelse(data_subset$cost >= 0, "#d5e8d5", "#f8cdcc")
# Check if both positive and negative values exist
show_dotted_line <- any(data_subset$cost < 0) && any(data_subset$cost > 0)
ggplot(data_subset, aes(x = Year, y = cost, fill = color, text = paste("Year:", Year, "<br>Cost:", cost))) +
labs(title = "Financial Outlook by Year", x = "Year", y = "Revenue (Costs)") +
scale_x_continuous(breaks = seq(min(data_subset$Year), max(data_subset$Year), by = 1)) +
scale_y_continuous(
labels = function(x) paste0(ifelse(x < 0, "-$", "$"), scales::comma_format(scale = 1e-9)(abs(x)), " B")
) +
theme(
panel.background = element_blank(),
panel.grid = element_blank(),
legend.position = "none",
plot.title = element_text(color = "#0D2F4F", size = 24, hjust = 0.5),
axis.title = element_text(size = 16),
axis.text = element_text(size = 16),
axis.line = element_line(color = "black", linewidth = 1)
) +
geom_bar(stat = "identity") +
scale_fill_identity() +
if (show_dotted_line) {
geom_hline(yintercept = 0, color = "black", linetype = "dashed", alpha = 0.5)
}
})
字符串text
属性根本不显示工具提示。
1条答案
按热度按时间bn31dyow1#
ggplot2
创建静态图(类似于png/jpeg文件)。这些图不允许用户交互或动态可视化。plotly
有一个名为ggplotly()
的函数,可以将任何ggplot
转换为动态plotly
图形,以维护ggplot光学。字符串
x1c 0d1x的数据
要在
shiny
应用程序中使用该功能,您只需使用plotly::renderPlotly()
代替shiny::renderPlot()
,并使用plotly::plotlyOutput()
代替shiny::plotOutput()
。型