为什么 geom_smooth 没有显示在 ggplotly 中?

Why is geom_smooth not showing in ggplotly?

我正在尝试使用 ggplotly 创建交互式散点图。这将包括一个具有交互性的散点图,以突出显示每个点的国家/地区名称,以及一条最适合的线。但是,当我将文本参数添加到我的 ggplot 时(为了创建交互式标签),它会删除最适合的行。我曾尝试将文本参数隔离到 geom_point 函数,正如之前堆栈溢出 post 中所建议的那样,但这并不能解决问题。

有人能提供解决方案吗?

提前致谢。

我的数据包括一列国家名称,一列国内生产总值和一列世界幸福指数得分,范围为 1 - 8。

就像一个随机生成的例子:

NAME gdp happiness_score
Argentina 10000 6
Canada 600000 8

我的代码如下:

library(ggplot2)
library(plotly)
library(dplyr)
library(readr)
library(lubridate)
library(stringr)
library(ggthemes)
library(extrafont)
library(htmlwidgets)

font = list(
  family = "DM Sans",
  size = 15,
  color = "white"
)

label = list(
  bgcolor = "#232F34",
  bordercolor = "transparent",
  font = font
)

#create base graph
scatter_gdp <- ggplot(scatterdf, aes(x = gdp, y = happiness_score)) + 
  geom_point(colour = '#005a32', alpha = 0.6,  text = NAME) +
  geom_smooth(method = 'lm', se = FALSE, colour = 'red', alpha = 0.6)
  
#format themes
scatter_gdp <- scatter_gdp +  
  theme_classic() +
  labs(title = "Relationship Between a Country's GDP and Happiness") +
  xlab("Gross Domestic Produce (US$)") +
  ylab("World Happiness Index Score") +
  theme(
    text = element_text(family="DM Sans"),
    plot.title = element_text(hjust = 0.5),
    panel.grid.major=element_blank(),
    panel.grid.minor=element_blank()
    ) 

#create ggplotly
graph_gdp.interactive = ggplotly(scatter_gdp, tooltip = 'NAME')%>%
  style(hoverlabel = label) %>%
  layout(font = font, 
         xaxis = list(fixedrange = TRUE), 
         yaxis = list(fixedrange = TRUE)) %>%
  config(displayModeBar = FALSE, showTips = FALSE)

graph_gdp.interactive

您应该将 NAME 分配给 aes 中的 label 命令,并添加 geom_text 以显示地块的名称。您可以使用以下代码:

library(ggplot2)
library(plotly)
library(dplyr)
library(readr)
library(lubridate)
library(stringr)
library(ggthemes)
library(extrafont)
library(htmlwidgets)

font = list(
  family = "DM Sans",
  size = 15,
  color = "white"
)

label = list(
  bgcolor = "#232F34",
  bordercolor = "transparent",
  font = font
)

scatterdf <- data.frame(NAME = c("Argentina", "Canada", "France"),
                        gdp = c(10000, 600000, 1000000),
                        happiness_score = c(6,8, 8))

#create base graph
scatter_gdp <- ggplot(scatterdf, aes(x = gdp, y = happiness_score, label = NAME)) + 
  geom_point(colour = '#005a32', alpha = 0.6) +
  geom_smooth(method = 'lm', se = FALSE, colour = 'red', alpha = 0.6) +
  geom_text(hjust=0, vjust=0)

#format themes
scatter_gdp <- scatter_gdp +  
  theme_classic() +
  labs(title = "Relationship Between a Country's GDP and Happiness") +
  xlab("Gross Domestic Produce (US$)") +
  ylab("World Happiness Index Score") +
  theme(
    text = element_text(family="DM Sans"),
    plot.title = element_text(hjust = 0.5),
    panel.grid.major=element_blank(),
    panel.grid.minor=element_blank()
  ) 

#create ggplotly
graph_gdp.interactive = ggplotly(scatter_gdp, tooltip = 'NAME')%>%
  style(hoverlabel = label) %>%
  layout(font = font, 
         xaxis = list(fixedrange = TRUE), 
         yaxis = list(fixedrange = TRUE)) %>%
  config(displayModeBar = FALSE, showTips = FALSE)

graph_gdp.interactive

输出: