Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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 在图形上画一条水平线,其中`method=";黄土有最高点_R_Ggplot2 - Fatal编程技术网

R 在图形上画一条水平线,其中`method=";黄土有最高点

R 在图形上画一条水平线,其中`method=";黄土有最高点,r,ggplot2,R,Ggplot2,df 我喜欢在method=“黄土”达到最大值的地方画一条水平线。这里有人对我如何实现这一点有任何意见吗?使用ggplot\u Build()构建绘图,然后提取数据: Date Score Team 1/1/2011 3 A 1/2/2011 5 A 1/3/2011 15 A 1/4/2011 39 B 1/5/2011 23 B 1/6/2011 100 B 1/7/2011 4 C 1/8/2011 25 C 1

df


我喜欢在method=“黄土”达到最大值的地方画一条水平线。这里有人对我如何实现这一点有任何意见吗?

使用
ggplot\u Build()
构建绘图,然后提取数据:

Date     Score  Team
1/1/2011   3    A
1/2/2011   5    A
1/3/2011   15   A
1/4/2011   39   B
1/5/2011   23   B
1/6/2011  100   B
1/7/2011   4    C
1/8/2011  25    C
1/9/2011   30   C

library(ggplot2)
ggplot(df, aes(Date, Score, group=1)) + geom_point() + geom_smooth(method="loess", se=T, size=1) + facet_wrap(~Team)
str(pp$data)
List of 2
 $ :'data.frame':   9 obs. of  4 variables:
  ..$ x    : int [1:9] 1 2 3 4 5 6 7 8 9
  ..$ y    : num [1:9] 3 5 15 39 23 100 4 25 30
  ..$ group: int [1:9] 1 1 1 1 1 1 1 1 1
  ..$ PANEL: int [1:9] 1 1 1 1 1 1 1 1 1
 $ :'data.frame':   9 obs. of  7 variables:
  ..$ x    : int [1:9] 1 2 3 4 5 6 7 8 9
  ..$ y    : num [1:9] 1.29 8.67 19.64 24.54 53.86 ...
  ..$ ymin : num [1:9] -103.1 -60.2 -57.3 -52.4 -23 ...
  ..$ ymax : num [1:9] 105.7 77.6 96.5 101.4 130.8 ...
  ..$ se   : num [1:9] 34.8 23 25.6 25.6 25.6 ...
  ..$ group: int [1:9] 1 1 1 1 1 1 1 1 1
  ..$ PANEL: int [1:9] 1 1 1 1 1 1 1 1 1
请注意,此对象是一个列表,其数据帧对应于每个geom。在我的解决方案中,我只需提取最大值
y
,然后通过它绘制一个
hline


这也适用于多个方面,但您必须做更多的工作来提取数据:

Date     Score  Team
1/1/2011   3    A
1/2/2011   5    A
1/3/2011   15   A
1/4/2011   39   B
1/5/2011   23   B
1/6/2011  100   B
1/7/2011   4    C
1/8/2011  25    C
1/9/2011   30   C

library(ggplot2)
ggplot(df, aes(Date, Score, group=1)) + geom_point() + geom_smooth(method="loess", se=T, size=1) + facet_wrap(~Team)
str(pp$data)
List of 2
 $ :'data.frame':   9 obs. of  4 variables:
  ..$ x    : int [1:9] 1 2 3 4 5 6 7 8 9
  ..$ y    : num [1:9] 3 5 15 39 23 100 4 25 30
  ..$ group: int [1:9] 1 1 1 1 1 1 1 1 1
  ..$ PANEL: int [1:9] 1 1 1 1 1 1 1 1 1
 $ :'data.frame':   9 obs. of  7 variables:
  ..$ x    : int [1:9] 1 2 3 4 5 6 7 8 9
  ..$ y    : num [1:9] 1.29 8.67 19.64 24.54 53.86 ...
  ..$ ymin : num [1:9] -103.1 -60.2 -57.3 -52.4 -23 ...
  ..$ ymax : num [1:9] 105.7 77.6 96.5 101.4 130.8 ...
  ..$ se   : num [1:9] 34.8 23 25.6 25.6 25.6 ...
  ..$ group: int [1:9] 1 1 1 1 1 1 1 1 1
  ..$ PANEL: int [1:9] 1 1 1 1 1 1 1 1 1

p我为没有在第一篇文章中包含所有信息而道歉。您的解决方案适用于单个图形。如果使用facet_wrap(~Team),您将如何解决此问题?@user1471980请不要再这样做。但我已经编辑了我的答案。奇怪的是,这对一些团队有效,而对其他团队无效。我发现它对某些团队非常有效,我发现有些团队没有正确地绘制hline,有些团队甚至没有hline,你知道可能发生了什么吗?@user1471980尽管看起来很奇怪,但你可能不得不自己做一些工作。除非你愿意付钱给我们。。。?不过,说真的,如果你连提供一个可复制的例子所需的最小努力都没有,我们怎么可能知道还有什么错呢?@joran,你是对的。但这很好。我完全可以和你一起工作,找出问题所在。