Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2折线图给出;geom_路径:每组仅包含一个观测值。您是否需要调整团队美学?”;_R_Ggplot2 - Fatal编程技术网

R ggplot2折线图给出;geom_路径:每组仅包含一个观测值。您是否需要调整团队美学?”;

R ggplot2折线图给出;geom_路径:每组仅包含一个观测值。您是否需要调整团队美学?”;,r,ggplot2,R,Ggplot2,使用此数据帧(“df”): 我尝试创建如下折线图: plot5 <- ggplot(df, aes(year, pollution)) + geom_point() + geom_line() + labs(x = "Year", y = "Particulate matter emissions (tons)", title = "Motor vehicle emissions in Baltimore") 出现此错

使用此数据帧(“df”):

我尝试创建如下折线图:

  plot5 <- ggplot(df, aes(year, pollution)) +
           geom_point() +
           geom_line() +
           labs(x = "Year", y = "Particulate matter emissions (tons)", title = "Motor vehicle emissions in Baltimore")

出现此错误是因为其中一个变量实际上是因子变量 . 执行

str(df) 
来检查一下。 然后执行此双变量更改以保留年份编号,而不是转换为“1,2,3,4”级别编号:

df$year <- as.numeric(as.character(df$year))

df$year在新会话中启动R并将其粘贴到:

library(ggplot2)

df <- structure(list(year = c(1, 2, 3, 4), pollution = structure(c(346.82, 
134.308821199349, 130.430379885892, 88.275457392443), .Dim = 4L, .Dimnames = list(
    c("1999", "2002", "2005", "2008")))), .Names = c("year", 
"pollution"), row.names = c(NA, -4L), class = "data.frame")

df[] <- lapply(df, as.numeric) # make all columns numeric

ggplot(df, aes(year, pollution)) +
           geom_point() +
           geom_line() +
           labs(x = "Year", 
                y = "Particulate matter emissions (tons)", 
                title = "Motor vehicle emissions in Baltimore")
库(ggplot2)

df您只需将
group=1
添加到ggplot或geom_line aes()中即可

对于折线图,必须对数据点进行分组,以便它知道要连接哪些点。在这种情况下,它很简单——所有点都应该连接,所以group=1。当使用更多变量并绘制多条直线时,直线的分组通常由变量完成

参考:

试试这个:

plot5 <- ggplot(df, aes(year, pollution, group = 1)) +
         geom_point() +
         geom_line() +
         labs(x = "Year", y = "Particulate matter emissions (tons)", 
              title = "Motor vehicle emissions in Baltimore")

plot5我对数据框也有类似的问题:

group time weight.loss
1 Control  wl1    4.500000
2    Diet  wl1    5.333333
3  DietEx  wl1    6.200000
4 Control  wl2    3.333333
5    Diet  wl2    3.916667
6  DietEx  wl2    6.100000
7 Control  wl3    2.083333
8    Diet  wl3    2.250000
9  DietEx  wl3    2.200000
我认为x轴的变量应该是数字的,这样geom_line就知道如何连接点来画线

将第2列更改为数字后:

 group time weight.loss
1 Control    1    4.500000
2    Diet    1    5.333333
3  DietEx    1    6.200000
4 Control    2    3.333333
5    Diet    2    3.916667
6  DietEx    2    6.100000
7 Control    3    2.083333
8    Diet    3    2.250000
9  DietEx    3    2.200000

然后它就工作了。

我得到了类似的提示。这是因为我指定了x轴的百分比(例如:10%A,20%B,…)。
因此,另一种方法是将这些值相乘,并以最简单的形式写入。我发现,如果绘制的大多数数据超出轴的限制,也会出现这种情况。在这种情况下,相应地调整轴刻度。

运行时不会出现错误。很可能
df
不是您所认为的那样。请以可复制的形式陈述您的问题,即显示
dput(df)
的输出。可能您的变量是因子,那么您需要将它们转换为numeric@G.Grothendieck我把你说的话发出去了。我也转换成了数字,但仍然有问题。你真的应该以可复制的形式陈述问题。如果我们无法重新创建错误,则很难帮助您。是否可以按“污染”的降序排列线条点?在新会话中启动R,并将我帖子中的代码粘贴到其中。您解决了这个问题吗。我和你有同样的问题,每个x值只有一个值。等待您的回复。谢谢。你能解释一下为什么把所有东西都转换成数字可以解决这个问题吗?我的有序因子变量是一个字符变量,所以我不能用数字来代替它。
pollution
是一个一维数组,而不是一个普通向量。请看
str(df)
这对我来说是一个方便的答案,因为它修复了根目录中的问题。这是防止此警告的好答案!需要注意的是,必须使用
group
参数进行分组。仅按
颜色进行分组是不够的。我遇到了这个问题,希望这能帮助遇到相同问题的人。这个答案仍然有效吗?在美学中添加group=1似乎不再有效了。@Giacomo——在Mac上的3.6.2上对我有效。正在收到可怕的警告,但添加group=1修复了该问题。ggplot(湖泊数据,地图=aes(x=湖泊,y=面积,组=1))+geom_线(大小=2,颜色=蓝色)是否可以按“污染”的降序排列该点?
plot5 <- ggplot(df, aes(year, pollution, group = 1)) +
         geom_point() +
         geom_line() +
         labs(x = "Year", y = "Particulate matter emissions (tons)", 
              title = "Motor vehicle emissions in Baltimore")
group time weight.loss
1 Control  wl1    4.500000
2    Diet  wl1    5.333333
3  DietEx  wl1    6.200000
4 Control  wl2    3.333333
5    Diet  wl2    3.916667
6  DietEx  wl2    6.100000
7 Control  wl3    2.083333
8    Diet  wl3    2.250000
9  DietEx  wl3    2.200000
 group time weight.loss
1 Control    1    4.500000
2    Diet    1    5.333333
3  DietEx    1    6.200000
4 Control    2    3.333333
5    Diet    2    3.916667
6  DietEx    2    6.100000
7 Control    3    2.083333
8    Diet    3    2.250000
9  DietEx    3    2.200000