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

ggplot using loess dates- change axis label from numeric to character

我有一个包含离散距离和月份(月份插值为 1、1.1、1.2 等)和 z 变量的数据集。月份是数字,而不是这里的日期。 我使用 loess 函数进行插值,并想使用 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)
                )

这很好,但我想将月份数字更改为单词。我该怎么做呢?

我知道要更改 ggplot x 轴(使用 scale_x_discrete),它需要是一个因子,这些都是数字。据我所知,它需要是数字才能使绘图工作。

我可以将其更改为 as.Date 并更改月份中的日期,但我希望月份本身发生变化。

有什么方法可以在不更改基础数据的情况下仅替换标签中 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

快速但只是近似值:

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

如果您想要精确的数据,我建议您使用 lubridate::decimal_date 或其他以天为单位(长度相同)的数据来准备您的数据,这样就不需要 *30 .