Mimic filled.contour with ggplot - r

I've been trying to mimic the output of filled.contour with ggplot's geom_tile and geom_raster. There seems to be some interpolation or something behind the scenes of filled.contour to fill in the gaps, especially in y-axis. Any idea how to make similar, nice looking plot with ggplot?
Data for example can be downloaded here (ShareCSV link).
Code for example:
library("ggplot2")
library("reshape2")
file <- file.choose()
data <- read.csv(file, stringsAsFactors=FALSE, header=TRUE,
colClasses=c("POSIXct", rep("numeric", 12)),
check.names=FALSE)
palette <- colorRampPalette(c("darkblue", "blue", "lightblue1",
"green", "yellow", "red", "darkred"))
# GGplot version
df <- melt(data, id.vars="Time", measure.vars=names(data)[-1])
names(df)[2:3] <- c("Channel", "Value")
df$Channel <- log10(as.numeric(as.character(df$Channel)))
df$Value <- log10(df$Value)
p <- ggplot(data=df, aes(x=Time, y=Channel, fill=Value)) +
# geom_tile() +
geom_raster() +
scale_y_continuous(expand=c(0, 0)) +
scale_x_datetime(expand=c(0, 0)) +
scale_fill_gradientn(colours=palette(100)) +
theme_bw()
p
# Base version
Channel <- log10(as.numeric(names(data[-1])))
Value <- log10(data.matrix(data[, -1]))
filled.contour(x=data$Time, y=Channel, z=Value,
color.palette=palette, nlevels=100)
I tried the following for df and it got pretty close but it's very compute intensive.
library("akima")
interpolation <- with(df, interp(x=Time, y=Channel, z=Value,
xo=seq(min(Time), max(Time), length.out=nrow(data)),
duplicate="mean"))
df<- interp2xyz(interpolation, data.frame=TRUE)
names(df) <- c("Time", "Channel", "Value")
df$Time <- as.POSIXct(df$Time, origin="1970-01-01")
Outcome:

Related

overlay two plots with different time axis scales using ggplot2 or lattice

Suppose I have the following datasets:
The first one is monthly data:
library (ggplot2)
val <- runif(48, -5,5)
dt <- seq(as.Date("2010/1/1"), by = "month", length.out = 48)
dat <- data.frame(cbind(val, as.Date(dt)))
ggplot(dat, aes(dt, val,fill=val)) + geom_bar(stat = 'identity')
The second is annual data with different y scale:
val2 <- runif(4,50,70)
dt2 <- seq(2010,2014)
dat2 <- data.frame(cbind(val2, dt2))
Is it possible to overlay the 2nd dataset on this plot, having two y axis but one x axis?
You can do it, even with two separate data frames, but the result is likely to be misleading and difficult to interpret.
library(tidyverse)
library(lubridate)
set.seed(2)
val <- runif(48, -5,5)
dt <- seq(as.Date("2010/1/1"), by = "month", length.out = 48)
dat <- data.frame(val, dt=as.Date(dt))
val2 <- runif(5,50,70)
dt2 <- seq(2010,2014)
dat2 <- data.frame(val2, dt2)
ggplot() +
geom_bar(data=dat, aes(dt, val,fill=val), stat = 'identity') +
geom_line(data=dat2 %>% mutate(dt2 = ymd(paste0(dt2,"06-30"))),
aes(dt2, val2/10), colour="red") +
scale_y_continuous(sec.axis=sec_axis(~.*10, name="val2")) +
theme(axis.text.y.right=element_text(colour="red"),
axis.title.y.right=element_text(colour="red"),
axis.text.y=element_text(colour="#346D9D"),
axis.title.y=element_text(colour="#346D9D"),
axis.title.x=element_blank(),
legend.position="bottom")

How to plot a filled.contour plot using ggplot2?

I have some data and I have tried a filled.contour plot which seems nice. However, the legend is hard to control, so I am thinking to use ggplo2. But I have no clue how to plot a filled.contour using ggplot2.
The data contains 840 rows (which stand for the dates), and 12 columns (which stand for 12 time scales). 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.5on the legend and I believe it is much easier to do this with ggplot2. Thanks for any help.
A ggplot2 alternative to filled.contour is 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))

Interactive correlation matrix

I want to plot correlation matrix in such a sense that it should be interactive and show both, correlation values and corresponding p-values in ggplot2 or plot_ly.
I have tried the following code.
library(ggplot2)
library(plotly)
library(reshape)
library(Hmisc)
x <- Seatbelts
y <- as.matrix(x)
rt <- rcorr(y)
mtlr <- melt(rt$r)
mtlp <- melt(rt$P)
gx <- ggplot(mtl, aes(X1, X2, fill = value)) + geom_tile() +
scale_fill_gradient(low = "cyan", high = "red")
ggplotly(gx)
I want to show "mtlp" value in the same plot.
Can anyone help?
You could add your p-values as a label or text which will then appear in your hovertext.
library(ggplot2)
library(plotly)
library(reshape)
library(Hmisc)
x <- Seatbelts
y <- as.matrix(x)
rt <- rcorr(y)
mtlr <- melt(rt$r)
mtlp <- melt(rt$P)
p.value <- mtlp$value
gx <- ggplot(mtlr, aes(X1, X2, fill = value, label=p.value)) + geom_tile() +
scale_fill_gradient(low = "cyan", high = "red")
ggplotly(gx)
The easiest way is to merge the two melted data.frames back together, then use the text aesthetic to add to the tooltip.
I re-did the melt using reshape2 (instead of reshape) and set value names to make the merge easier
mtlr <- melt(rt$r, value.name = "Correlation")
mtlp <- melt(rt$P, value.name = "P-Value")
mtl <-
merge(mtlr, mtlp)
Then, make the plot -- note that you can use anything you want in the text aesthetic, and I often use it to change the layout/presentation of the tooltip (and then only display the text tooltip)
gx <-
ggplot(mtl
, aes(Var1, Var2
, fill = Correlation
, text = paste("P-val = ", round(`P-Value`, 4)))) +
geom_tile() +
scale_fill_gradient(low = "cyan", high = "red")
ggplotly(gx)

alternative for ggplot2 aes: order function?

Does somebody know a alternative method for ordering stacks of a ggplot2 bar graph?
I used to use for example
library(ggplot2)
library(plyr)
a <- cbind(rep("a",5),sample(1:100,5), rep_len(c("1","2","3"),5))
b <- cbind(rep("b",7),sample(1:100,7), rep_len(c("1","2","3"),7))
c <- cbind(rep("c",3),sample(1:100,3), rep_len(c("1","2","3"),3))
d <- cbind(rep("d",10),sample(1:100,10), rep_len(c("1","2","3"),10))
e <- cbind(rep("e",15),sample(1:100,15), rep_len(c("1","2","3"),15))
dat <- rbind(a,b,c,d,e)
colnames(dat) <- c("x","count","example")
dat <- as.data.frame(dat)
dat$x <- as.character(dat$x)
dat$count <- as.numeric(dat$count)
dat$example <- as.character(dat$example)
GP <- ggplot(dat, aes(x= reorder(x, count, sum), y=count, fill = example, order = desc(count)))+
geom_bar(stat="identity", fill= "grey", colour= "black", size = 1)+
coord_flip() +
scale_y_continuous()+
scale_x_discrete('')+
#scale_fill_brewer()+
labs(y="")+
theme_bw()+
theme(axis.text.y=element_text(size=8,face="bold"),
axis.text.x=element_text(size=10,face="bold"),
axis.title.x=element_text(size=16,face="bold"),
axis.title.y=element_text(size=16,face="bold"),
plot.title=element_text(size=16,face="bold"),
strip.text.x = element_text(size=10,face="bold"),
strip.background = element_blank())
print(GP)
to create graphs like
however in version 2.0.0 of ggplot2 order() has been removed. and now the graph will be like:
Does anybody know a alternative?
Tanks

ggplot() lines transparency

How to change the transparency level of lines in ggplot() diagram (i.e. histogram, line plot, etc.)?
For instance consider the code below:
data <- data.frame(a=rnorm(100), b = rnorm(100,.5,1.2))
data <- melt(data)
colnames(data) <- c("Category", "Intensity")
p <- ggplot(data, aes(x=Intensity))
p <- p + geom_density(aes(color=Category), size=2, alpha=.4)
print(p)
I expected the lines would be transparent (as alpha=.4), but they're not.
Simply following #baptiste's directions,
data <- data.frame(a=rnorm(100), b = rnorm(100,.5,1.2))
data <- melt(data)
colnames(data) <- c("Category", "Intensity")
p <- ggplot(data, aes(x=Intensity))
p + geom_line(aes(color=Category), stat="density", size=2, alpha=0.4)

Resources