gpt4 book ai didi

r - 为什么在我尝试使用 ggplot2 显示分面线图时显示 geom_path 警告消息?

转载 作者:行者123 更新时间:2023-12-04 15:46:30 30 4
gpt4 key购买 nike

我正在尝试使用 ggplot、geom_line 和 facet_wrap 创建多线图。图表已成功创建并显示,但是以下消息也随图表一起显示:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

enter image description here

我已经尝试重新启动我的 session ,重新启动 R,通过谷歌搜索搜索答案,在 Stack Overflow 上搜索这里(包括以下问题:ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"),但找不到任何有帮助的东西。

添加:

group=1

...不会消除警告消息。

我使用的RStudio版本是:Version 1.1.463。

该代码位于我正在编织以创建 HTML 文档的 RMarkdown 文件中。

我在这个 Rmarkdown 文件中使用的库是:

library(tidyverse)
library(operators)
library(magrittr)
library(dplyr)
library(knitr)
library(sf)
library(usmap)
library(waffle)

我在图表中使用的数据集的结构是:

'data.frame':   75 obs. of  3 variables:
$ Year : int 1998 1993 1998 1999 2005 2007 2014 2018 1989 1991 ...
$ Age_Bracket : chr "1-12" "13-19" "13-19" "13-19" ...
$ Num_Shootings: num 1 1 1 1 1 2 1 2 1 1 ...

生成警告信息的代码是:

# Create a faceted line graph showing the number of mass shootings per year for each age bracket

faceted_line_graph_year_age <- ggplot(data=shootings_per_year_age, aes(x=Year, y = Num_Shootings, colour = Age_Bracket, group=1)) +
geom_line()+
geom_point()+
geom_smooth(method = "lm", colour = "#666666", linetype = 2, size = 0.5 )+
facet_wrap(~Age_Bracket)


# Display the faceted line graph

faceted_line_graph_year_age

我看到的消息是:

geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?

我希望在没有警告消息的情况下显示图表。我得到的是显示带有警告消息的图表。

最佳答案

感谢用户calum-you答案:

I think the error is caused by geom_line in the first group, because it cannot draw a line with only a single point there. This is also a warning/message (not sure which exactly here) but not an error, which would not have let you produce the graph at all. Warnings/messages are mostly to let you know you might be doing something wrong, not fatal

我删除了代表拳头年龄类别的图(因为该图只有一个数据值),问题已解决。

关于r - 为什么在我尝试使用 ggplot2 显示分面线图时显示 geom_path 警告消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55521659/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com