Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Isotherms as isolines using ggplot2

I am trying to create an an isotherms plot using the ggplot2 package, you can find an example showing the desired result in the comments. I've seen many examples for doing so online, such as:

data <- read.table("http://s000.tinyupload.com/download.php?file_id=01371475872599670620&t=0137147587259967062053048", header=TRUE)

ggplot(data) + aes(x=x, y=y, z=z, fill=z) + stat_contour() + geom_tile()

When I use the exact same code with my data I get the following result:

Image:

graph with isolines

As you can see in the image above, the data seems to work with the stat_contour() part, which looks perfect, but it does not work with geom_tile(), which only seems to create those three horizontal lines. The data seems to be read correctly, since the contour lines work and the legend contains the right values too. I don't know why the files won't be filled though.

I'd really appreciate any help provided, thanks!

like image 236
oepix Avatar asked Jan 08 '23 13:01

oepix


1 Answers

It's probably happening due to the irregular grid. Try interpolating the values first then doing the plot:

library(ggplot2)
library(akima)
library(viridis)
library(ggthemes)

dat <- read.table("http://s000.tinyupload.com/download.php?file_id=01371475872599670620&t=0137147587259967062053048", header=TRUE)

di <- interp(dat$x, dat$y, dat$z,
             xo=seq(min(dat$x), max(dat$x), length=200),
             yo=seq(min(dat$y), max(dat$y), length=200))

dat_interp <- data.frame(expand.grid(x=di$x, y=di$y), z=c(di$z))

ggplot(dat_interp) +
  aes(x=x, y=y, z=z, fill=z) +
  geom_tile() +
  stat_contour(color="white", size=0.25) +
  scale_fill_viridis() +
  theme_tufte(base_family="Helvetica")

enter image description here

like image 104
hrbrmstr Avatar answered Jan 11 '23 20:01

hrbrmstr