I have a dataset with discrets distances and Months (The months are interpolated as 1, 1.1, 1.2, etc.) and a z variable. Months are numeric, not dates here. I interpolate using a loess function and want to plot it using 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)
)
Which is good, but I want to change the month numbers to words. How do I do this?
I know that to change a ggplot x axis (with scale_x_discrete) it needs to be a factor and these are numeric. As far as I know it needs to be numeric in order for the plotting to work.
I can change it to as.Date and that changes the day of the month, but I want the Months themselves to change.
Is there any way to just replace the 1 for June, the 2 for July, (etc) in the label without changing the underlying data? Do you not plot the x-axis and then add your own labels? Is that possible?
My code is not elegant, but its pasted below.
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))
From here I use the sampled data (dr) in place of 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
str()
,head()
or screenshot)? You can use thereprex
anddatapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example? – Tung