[英]How to plot a filled.contour plot using ggplot2?
I have some data and I have tried a filled.contour
plot which seems nice. 我有一些数据,并且尝试了
filled.contour
图,这看起来不错。 However, the legend is hard to control, so I am thinking to use ggplo2
. 但是,图例很难控制,因此我正在考虑使用
ggplo2
。 But I have no clue how to plot a filled.contour
using ggplot2
. 但我不知道如何绘制
filled.contour
使用ggplot2
。
The data contains 840 rows (which stand for the dates), and 12 columns (which stand for 12 time scales). 数据包含840行(代表日期)和12列(代表12个时标)。 Here is an example
这是一个例子
set.seed(66)
Mydata <- sample(x=(-3:3),size = 840*12,replace = T)
Mydata <- matrix(data=Mydata,nrow=840,ncol=12)
Dates <- seq(from=1948+1/24, to= 2018,by=1/12)
data.breaks <- c(-3.5,-2.5,-1.5,0,1.5,2.5,3.5)
filled.contour(Dates,seq(1:12),Mydata,col=cols(11),xlab="",ylab="time-scale",levels=data.breaks)
As we can see, the legend intervals are not what I want. 如我们所见,图例间隔不是我想要的。 I want to show
-3.5,-2.5,-1.5,0,1.5,2.5,3.5
on the legend and I believe it is much easier to do this with ggplot2
. 我想在图例上显示
-3.5,-2.5,-1.5,0,1.5,2.5,3.5
,我相信使用ggplot2
轻松ggplot2
。 Thanks for any help. 谢谢你的帮助。
A ggplot2
alternative to filled.contour
is stat_contour
. ggplot2
替代filled.contour
是stat_contour
。
library(ggplot2)
library(reshape2)
set.seed(66)
Mydata <- sample(x=(-3:3),size = 840*12,replace = T)
Mydata <- matrix(data=Mydata,nrow=840,ncol=12)
Dates <- seq(from=1948+1/24, to= 2018,by=1/12)
data.breaks <- c(-3.5,-2.5,-1.5,0,1.5,2.5,3.5)
rownames(Mydata) <- Dates
d <- melt(Mydata)
colfunc = colorRampPalette(c("brown", "red", "yellow", "white"))
ggplot(d, aes(Var1, Var2, z=value, fill = value)) +
stat_contour(geom="polygon", aes(fill=..level..)) +
scale_fill_gradientn(colours = colfunc(7), breaks=data.breaks, limits=c(-4,4),
values=scales::rescale(data.breaks))+
theme_bw() +
scale_x_continuous(name="", breaks=seq(1950,2010,20), expand=c(0,0)) +
scale_y_continuous(name="time-scale", expand=c(0,0))+
guides(fill = guide_colorbar(barwidth = 2, barheight = 15))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.