ggplot2中geom_label()警告的原因及消除方法咨询
问题描述
在R环境中完成数据转换、计算后,绘制Likert图与条形图的组合可视化图表时,控制台出现如下警告:
Warning message: Removed 20 rows containing missing values or values outside the scale range (`geom_label()`).
请问该警告是否与NA值有关?其产生的具体原因是什么?如何消除该警告?
完整代码如下:
library(tidyverse) library(ggstats) library(patchwork) library(tibble) library(tidyverse) library(ggplot2) library(ggstats) likert_levels = c( "Very \n Dissatisfied", "Dissatisfied", "Neutral", "Satisfied", "Very \n Satisfied" ) custom_colors = c( "Very \n Dissatisfied" = "#ed2e1c", "Dissatisfied" = "#e09c95", "Neutral" = "#85c1e9", "Satisfied" = "#7FF98B", "Very \n Satisfied" = "#04B431" ) var_levels <- c(LETTERS[1:20]) n = 500 likert_levels = c( "Very \n Dissatisfied", "Dissatisfied", "Neutral", "Satisfied", "Very \n Satisfied" ) df <- tibble( var = sample(var_levels, n, replace = TRUE), val1 = sample(likert_levels, n, replace = TRUE), val2 = sample(c(likert_levels, NA),n, replace = TRUE), val3 = sample(likert_levels, n, replace = TRUE) ) df2 = df%>% pivot_longer(!var, names_to = "Categories", values_to = "likert_values")%>% select(-Categories)%>% tidyr::drop_na() df_bar = df%>% select(var)%>% group_by(var)%>% summarise(n=n()) df_likert = df2 %>% group_by(var, likert_values) %>% # Group by `var` and `likert_values` summarise(count = n(), .groups = "drop") %>% # Count the occurrences group_by(var) %>% # Group by `var` mutate(percentage = (count / sum(count)) * 100) %>% # Calculate percentages ungroup() # Ungroup for a clean output df = df_likert%>% left_join(.,df_bar,by = "var")%>% select(-count)%>% pivot_wider(names_from = likert_values, values_from = percentage)%>% dplyr::relocate(var,.before=n)%>% dplyr::relocate(n,.before=`Very \n Dissatisfied`)%>% dplyr::relocate(`Very \n Dissatisfied` ,.after = n)%>% dplyr::relocate( Dissatisfied,.after = `Very \n Dissatisfied`)%>% dplyr::relocate(Neutral,.after =Dissatisfied )%>% dplyr::relocate(Satisfied,.after=Neutral)%>% dplyr::relocate(`Very \n Satisfied`,.after = Satisfied) levels <- names(df)[-c(1:2)] df_long <- df %>% select(-n) %>% pivot_longer(!var, names_to = "Likert", values_to = "Percentage") |> mutate(Likert = factor(Likert, levels)) df_tot <- df_long |> summarise( prop_lower = sum(Percentage[Likert %in% levels[1:2]]), prop_higher = sum(Percentage[Likert %in% levels[4:5]]), .by = var ) |> pivot_longer(-var, names_prefix = "prop_", values_to = "Percentage", names_to = "where" ) var_ordered <- levels(with(df_tot, reorder(var, ifelse(where == "higher", Percentage, NA), na.rm = TRUE )) ) var_ordered = var_ordered[1:10] df_long=df_long%>% filter(var %in% var_ordered) # Likert plot likert_plot <- ggplot(df_long, aes(x = Percentage, y = var, fill = Likert)) + geom_col(position = position_likert(reverse = FALSE)) + geom_text( aes( label = label_percent_abs(hide_below = .01, accuracy = 1, scale = 1)(Percentage) ), position = position_likert(vjust = 0.5, reverse = FALSE), size = 3.5, fontface = "bold" ) + geom_label( data = df_tot, aes( label = label_percent_abs(hide_below = .01, accuracy = 1, scale = 1)(Percentage), x = ifelse(where == "lower", -.8 , .8), fill = NULL ), size = 3.5, fontface = "bold", label.size = 0.2, show.legend = FALSE ) + scale_x_continuous( labels = label_percent_abs() ) + labs( title = "Likert Responses by Category", x = "Category", y = "Percentage", fill = "Likert Scale" ) + theme_bw() + theme( panel.border = element_rect(color = "black"))+ scale_fill_manual(values = custom_colors) + labs(x = NULL, y = NULL, fill = NULL) + coord_cartesian(clip = "off")+ scale_y_discrete(limits = var_ordered) df = df%>% filter(var %in% var_ordered) # Horizontal bar plot bar_plot <- ggplot(df, aes(x = n, y = var)) + geom_bar(stat = "identity", fill = "lightgrey") + geom_label( aes( label = label_number_abs(hide_below = .05, accuracy = 2)(n) ), size = 3.5, position = position_stack(vjust = 0.5), hjust = 1, fill = NA, label.size = 0, color = "black" ) + scale_y_discrete(limits = var_ordered)+ scale_x_continuous( labels = label_percent_abs(), expand = c(0, .15) ) + theme_light() + theme( legend.position = "bottom", panel.grid.major.y = element_blank(), panel.border = element_rect(color = "black") , axis.text.x = element_blank() # Hides x-axis numbers ) + labs(x = NULL, y = NULL, fill = NULL) # Print plots (likert_plot) + (bar_plot) + plot_layout( width = c(4, 1) ) & theme(legend.position = "bottom")
解答
1. 该警告是否与NA值有关?
无关,原始数据中的NA已经通过drop_na()被处理干净,本次警告和NA值没有直接关联。
2. 具体原因
警告来自likert_plot中的geom_label()层,核心问题是数据源不同步:
- 你通过
var_ordered = var_ordered[1:10]筛选出了要展示的10个var,并对df_long、df都做了同步过滤,但geom_label()使用的数据源df_tot仍然保留了全部20个var的数据。 - 绘图时
scale_y_discrete(limits = var_ordered)只显示前10个var,df_tot中剩下的10个var对应的标签数据无法在当前y轴范围内展示,被ggplot自动移除,这就是警告中“20行”的来源(每个未被筛选的var对应df_tot里lower和higher两行数据,10*2=20)。
3. 消除警告的方法
对df_tot同步执行筛选操作,保留和其他数据集一致的var即可:
在定义var_ordered之后,添加一行过滤代码:
df_tot = df_tot %>% filter(var %in% var_ordered)
修改后的关键代码段:
var_ordered = var_ordered[1:10] # 同步过滤df_tot,只保留需要展示的var df_tot = df_tot %>% filter(var %in% var_ordered) df_long=df_long%>% filter(var %in% var_ordered)
这样geom_label()的数据源就只包含要显示的10个var的数据,不会再有超出y轴范围的行,警告会自动消除。
内容的提问来源于stack exchange,提问作者Homer Jay Simpson
相关产品推荐
相关产品推荐

