- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个看起来更像这样的 GGPLOT2 平滑线图
来源:http://www.esrl.noaa.gov/psd/enso/mei/
而不像这样:
来源:https://dl.dropboxusercontent.com/u/16400709/StackOverflow/Rplot02.png
我的数据是available on dropbox .
查看了之前的帖子后,我使用了以下代码:
#MEI Line Graph
d4 <- read.csv("https://dl.dropboxusercontent.com/u/16400709/StackOverflow/Data_MEI.csv")
head(d4,n=20)
MEI<-ggplot(d4,aes(x=d4$Date, y=d4$MEI,group=1))+geom_line()
MEI+stat_smooth(method ="auto",level=0.95)
我认为我需要的是减少平滑量,但我还没有弄清楚如何实现这一点。
d4s<-SMA(d4$MEI,n=8)
plot.ts(d4s)
SMA() 效果很好,但我无法让它与 ggplot 一起使用任何提示将不胜感激!
最佳答案
请注意,MEI 指数的周期为 2 个月,因此它已经内置了一些平滑功能。假设您正在使用 NOAA ESRL 发布的 MEI 数据,您应该能够创建相同的绘图。
首先,您需要设置系统,因为您将使用 timezeones:
# set things up ----
working.dir = file.path('/code/R/StackOverflow/')
setwd(working.dir)
Sys.setenv(TZ='GMT')
现在,下载您的数据并读取它
d.in <- read.csv("MEI.txt")
下一步是正确设置日期格式。
d.in$Date <- as.POSIXct(d.in$Date,
format = "%d/%m/%Y",
tz = "GMT")
并且因为我们需要找出事物与 x 轴交叉的位置,所以我们必须使用十进制日期。使用 Epoch 值:
d <- data.frame(x = as.numeric(format(d.in$Date,
'%s')),
y = d.in$MEI)
现在我们可以找出零交叉点。我们将使用Beroe's example for that .
rx <- do.call("rbind",
sapply(1:(nrow(d)-1), function(i){
f <- lm(x~y, d[i:(i+1),])
if (f$qr$rank < 2) return(NULL)
r <- predict(f, newdata=data.frame(y=0))
if(d[i,]$x < r & r < d[i+1,]$x)
return(data.frame(x=r,y=0))
else return(NULL)
}))
并将其添加到初始数据的末尾:
d2 <- rbind(d,rx)
现在转换回日期:
d2$date <- as.POSIXct(d2$x,
origin = "1960-01-01",
format = "%s",
tz = "GMT")
现在我们可以绘制图了:
require(ggplot2)
ggplot(d2,aes(x = date,
y = y)) +
geom_area(data=subset(d2, y<=0), fill="blue") +
geom_area(data=subset(d2, y>=0), fill="red") +
scale_y_continuous(name = "MEI")
这给了你这个:
现在,你真的需要平滑这个吗?
关于r - ggplot2 geom_line() 和平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367607/
在 geom_line 中获得奇怪的顶点排序阴谋。左图是以 R 为底的;正确的是ggplot。 这是shapefile我正在与。这将重现情节: require(ggplot2); require(ma
我正在融化一个数据框,以便在 ggplot2 中绘制它。然而,geom_line()没有给我水平线。相反,它正在将我的点转换为垂直线。我将附上图片并添加代码以进一步说明: 仅使用 geom_point
我想根据两个变量的相互作用对数据进行group编码,但只能将美观性映射到这些变量之一。 (另一个变量表示理论上应该等效的重复项)。我可以找到实现此目的的优雅方法,但似乎应该有一种更优雅的方法。 例如
有没有办法抖动 geom_line() 中的线条? ?我知道这有点违背了这个情节的目的,但是如果你有一个只有几行的情节并且希望他们都展示它可能会很方便。也许有一些其他解决方案可以解决这个可见性问题。
您好,我有一个关于 geom_line 的问题。 我想要做的是,对于同一条线,根据应用于另一个变量的某些过滤器具有不同的颜色段,即不是用于绘制线的变量。 例如,如果我们有一个带有标题时间、var1 和
我有一个图,其中每个点都有点、线和注释。问题是线条最终会穿过注释文本,我想找到一个解决方案来避免这种情况,例如如何使注释位于“顶部”? 我的伪 ggplot 就像: # some df ggplot(
我有一个图,其中每个点都有点、线和注释。问题是线条最终会穿过注释文本,我想找到一个解决方案来避免这种情况,例如如何使注释位于“顶部”? 我的伪 ggplot 就像: # some df ggplot(
我正在尝试使用 plotly 构建交互式图表,但它无法显示 geom_line(),我不确定为什么。这不是我的确切示例,但即使是 plotly 网页上提供的示例也失败了。在这里: library(pl
我使用 ggplot 和 geom_line 创建了一个绘图,并希望在绘图上显示单个数据值(数字)。 这是我目前的代码: ggplot(errors_prob_3) + geom_point(aes(
我有要绘制的数据。我有几个变量,范围从 1880 年到 2012 年。我每年观察一次。但有时一个变量多年没有观测值。例如,它可能有 1880-1888 年的观测值,但没有 1889-1955 年和 1
这是我一直在尝试寻找修复方法的事情,但基本上我想知道是否有一种快速的方法来“躲避”ggplot2中两个不同数据集的线图。 我的代码目前是: #Example data id <- c("A","A")
这是我要修改的绘图的数据: mdfr con,header=T);close(con) 我使用以下命令创建了一个绘图 ggplot(mdfr, aes(as.Date(value, "%d/%m/%Y
我有以下要绘制的数据框。我想知道是否可以将连接我的结果变量(stackOne$y)的部分线条着色为不同的颜色,具体取决于它是否小于某个值。例如,我希望低于 2.2 的线条部分为红色。 set.seed
我有这个数据框和这个图: df <- data.frame(Groupe = rep(c("A","B"),4), Period = gl(4,2,8,c("t0",
我编写了以下代码: library(ggplot2) data <- structure(list(x = c(1L, 6L, 3L, 4L, 2L, 3L, 6L, 1L, 5L, 2L,
我想用ggplot2建立一个情节。因此,我使用geom_line来可视化线,并使用geom_smooth来显示特定索引的最小-最大范围。 使用了两个数据框,第一行包含日期(例如:2013-02-04)
ggplot(mtcars, aes(wt, mpg)) + geom_line() + geom_point() geom_line() 用一条直线连接点,这是两个点之间的最短路径。我希望这条线首先
我正在尝试做与这篇文章类似的事情 here ,用箭头连接基于分组(在本例中为站点)的点,并根据特定值(年份)指定箭头方向。我无法让箭头的方向正常工作。我想让箭头的方向始终从 2017 年到 2018
我正在尝试创建一个类似于下图的图,其中蓝色遮盖了灰色。然而,在我正在创建的情节中,蓝色并没有掩盖其他颜色。有人可以指导我如何实现吗? 这是我想得到的颜色图: 这是我的情节: ggplot(df,
我正在尝试手动更改 ggplot2 线图中图例的第一项的颜色。 我对一个变量有几个观察结果,我在一个线图中显示,就像这样: ggplot(tmp1, aes(x=factor(month), y=va
我是一名优秀的程序员,十分优秀!