ggplot使用黄土日期-将轴标签从数字更改为字符

时间:2018-10-20 22:25:41

标签: r ggplot2 loess

我有一个数据集,该数据集包含离散距离和月份(月份被内插为1、1.1、1.2等)和一个z变量。月是数字,此处不是日期。 我使用黄土函数进行插值,并希望使用ggplot,geom_tile对其进行绘制。

dr<-data.frame(
                    Distance = c(0.97, 0.62, 1.55, 0.42, 1.12, 0.8, 0.71, 1.21, 0.47, 1.35,
                                 1.76, 0.18, 1.4, 0.97, 0.34, 1.08, 1.09, 1.64,
                                 1.7, 1.47, 1.2, 0.74, 0.42, 1.41, 0.4, 0.74,
                                 0.99, 0.43, 1.5, 1.54, 1.37, 0.46, 1.1, 1.2, 0.91,
                                 0.72, 1, 0.18, 1.29, 0.62, 1.17, 1.24, 0.84,
                                 0.84, 0.21, 0.17, 0.64, 1.34, 0.47, 1.3),
                   UseMonth2 = c(3.3, 3.5, 3.2, 1.7, 3.6, 4.4, 2, 3.1, 2.4, 3.3, 1.2, 3.1, 4.8,
                                 4, 2.9, 4.8, 1, 1, 2, 1.2, 3.5, 4.8, 4.1, 4.3,
                                 2.7, 2.7, 3.2, 1.1, 3.6, 1.7, 1.1, 5, 4.3, 4,
                                 2.8, 2.5, 4.8, 2.6, 3.7, 1, 4.3, 3.8, 4.9, 3,
                                 4.9, 1.1, 4.5, 2.9, 2.7, 2.6),
                       range = c(377.389988710373, 376.474123485623, 478.534819467677,
                                 602.067091322613, 343.832565335464,
                                 260.55057754737, 574.107524987301, 419.249842350009,
                                 505.762850596818, 417.956813478891, 828.131939674788,
                                 492.934634772338, 270.016441444965,
                                 284.070953766065, 465.236139302851, 225.85027887431,
                                 714.115473334014, 789.071443253749, 750.36957770927,
                                 760.79553484614, 366.144791945304,
                                 258.702510255092, 378.264675422883, 287.371865425501,
                                 472.594793226271, 444.301352980583, 389.16251974121,
                                 700.275256434454, 406.167932696917,
                                 748.397499456663, 743.54873866004, 349.070538178079,
                                 248.098841748563, 290.526095355155, 428.440987262833,
                                 482.536799697771, 226.309801509151,
                                 521.662008526265, 347.009729599152, 709.663188101106,
                                 251.480113101159, 324.543785069108, 242.958802993393,
                                 403.987814761174, 468.008522548405,
                                 714.027652745854, 289.715448154718, 462.343610096765,
                                 463.099485274408, 516.780879302855)
                )

I get this

哪个好,但是我想将月份数字改为单词。我该怎么做呢?

我知道要更改ggplot x轴(带有scale_x_discrete),它必须是一个因素,并且这些是数字。据我所知,为了使绘图有效,它必须是数字。

我可以将其更改为as.Date,这会更改月份中的某天,但我希望Months本身也可以更改。 enter image description here

有什么方法可以仅替换标签中的6月1日,7月2日(等等)而不更改基础数据吗?您不绘制x轴,然后添加自己的标签吗?有可能吗?

我的代码不是很好,但是粘贴在下面。

setwd("C:/Users/vschoe11/Dropbox/Projects/Timberlake/plates/060916")
plates<-read.csv("Data for plates csv.csv", header=T)

plates1.5<-plates[which(plates$Depth==1.5),]

data.loess <- loess(range ~ Distance * UseMonth2, data = plates1.5)
ygrid <-  seq(min(0), max(1.9), 0.01)
xgrid <-  seq(min(1), max(5), 0.1)
data.fit <-  expand.grid(Distance = ygrid, UseMonth2 = xgrid)
mtrx3d <-  predict(data.loess, newdata = data.fit)
contour(x = xgrid, y = ygrid, z = mtrx3d, xlab = "UseMonth2", ylab = "Distance")
library(reshape2)
mtrx.melt <- melt(mtrx3d, id.vars = c("Distance", "UseMonth2"), measure.vars = "range")
names(mtrx.melt) <- c("Distance", "UseMonth2", "range")
library(stringr)
mtrx.melt$Distance <- as.numeric(str_sub(mtrx.melt$Distance, str_locate(mtrx.melt$Distance, "=")[1,1] + 1))
mtrx.melt$UseMonth2 <- as.numeric(str_sub(mtrx.melt$UseMonth2, str_locate(mtrx.melt$UseMonth2, "=")[1,1] + 1))

从这里开始,我使用采样数据(dr)代替mtrx.melt

library(ggplot2)
labels_mon <- c("June", "July", "August", "Sept", "Oct")
plotrange <- ggplot(dr, aes(y = Distance, x = as.Date(UseMonth2, origin="2012-06-01"), z = range)) +
  theme_bw()+
  theme(axis.title=element_text(size=12))+
  stat_contour(geom = "polygon", aes(fill = ..level..))+
  geom_tile(aes(fill = range)) +
  stat_contour(bins=20, color="black")+
  xlab("Month") +
  ylab("Distance") +
  guides(fill = guide_colorbar(title = "range"))+
  scale_fill_gradientn(colours = c("blue", "yellow", "red"))+
  theme(legend.position="bottom")
plotrange

1 个答案:

答案 0 :(得分:1)

快速但仅是近似值:

plotrange <- ggplot(dr, 
                    aes(y = Distance, 
                        x = as.Date(UseMonth2*30, origin="2012-06-01"), 
                        z = range)) +

如果您希望准确无误,建议您使用lubridate::decimal_date或其他基于天的数据(长度都相同)来准备数据,这样就无需使用*30。 / p>