Contour plot not fully filled with ggplot


Daniel Valencia C.

I'm trying to do my first filled contour plot using ggplot. Based on my data, I'm selecting something like:

enter image description here

But my result is:

a <- c(1, 1.1, 1, 1.3, 1.2, 2, 2.2, 2, 2.5, 2.1, 3, 3, 3, 3.1, 3.2)
b <- c(rep(c(0, 5, 10, 15, 20), 3))
c <- seq(0, 1000, by = 1000/14)

DF <- data.frame(a, b, c)

ggplot(DF, aes(x = a, y = b, z = c)) +
  geom_raster(aes(fill = c)) +
  geom_contour() + scale_fill_gradientn(colours = rainbow(10))

enter image description here

What am I doing wrong and where can I find more data about the graph?

abuse

Here is an example:

Generate coordinates:

b = c(0, 5, 10, 15, 20)
a = (1:30)/10

Generate all combinations of coordinates

df <- expand.grid(a, b)

Generate c via tcrossprod of a and b + 1 (this is completely arbitrary, but generates a nice pattern)

df$c <- as.vector(a %o% (b+1))

ggplot(df, aes(x = Var1, y = Var2, z = c, fill = c)) +
  geom_raster(interpolate = T) + #interpolate for success 
  scale_fill_gradientn(colours = rainbow(10))

enter image description here

Generally, if you want to plot a matrix of values ​​(z-values) in ggplot, you need to convert it to long format via meltin reshape2or collect in tidyr, and then use it for plotting.

Your data is very sparse, one workaround is to generate missing data. I'll show how to do it with the loess function:

model <- loess(c ~ a + b, data = DF) #make a loess model based on the data provided (data in OP)

z <- predict(model, newdata = expand.grid(a = (10:30)/10, b = (0:200)/10)) #predict on the grid data

df <- data.frame(expand.grid(a = (10:30)/10, b = (0:200)/10), c = as.vector(z)) #append z to grid data

ggplot(df, aes(x = a, y = b, z = c, fill = c)) +
  geom_raster(interpolate = T)+
  scale_fill_gradientn(colours = rainbow(10))

enter image description here

Related


Contour plot not fully filled with ggplot

Daniel Valencia C. I'm trying to do my first filled contour plot using ggplot. Based on my data, I'm selecting something like: But my result is: a <- c(1, 1.1, 1, 1.3, 1.2, 2, 2.2, 2, 2.5, 2.1, 3, 3, 3, 3.1, 3.2) b <- c(rep(c(0, 5, 10, 15, 20), 3)) c <- seq(0,

Contour plot not fully filled with ggplot

Daniel Valencia C. I'm trying to do my first filled contour plot using ggplot. Based on my data, I'm selecting something like: But my result is: a <- c(1, 1.1, 1, 1.3, 1.2, 2, 2.2, 2, 2.5, 2.1, 3, 3, 3, 3.1, 3.2) b <- c(rep(c(0, 5, 10, 15, 20), 3)) c <- seq(0,

Add abline to filled contour plot

Notolina I drew a contour plot and added a diagonal line: library(MASS) library(fields) x <- c(21.06, 28.89, 23.00, 23.61, 23.61, 22.83, 30.44) y <- c(26.56, 24.00, 13.06, 18.61, 18.61, 14.17, 25.33) z <- kde2d(x, y, n=32, lims = c(0,32,0,32)) contour(z, col =

Add abline to filled contour plot

Notolina I drew a contour plot and added a diagonal line: library(MASS) library(fields) x <- c(21.06, 28.89, 23.00, 23.61, 23.61, 22.83, 30.44) y <- c(26.56, 24.00, 13.06, 18.61, 18.61, 14.17, 25.33) z <- kde2d(x, y, n=32, lims = c(0,32,0,32)) contour(z, col =

R: Plot scatter plot on filled contour plot

cure I am very new to R and have made a fill.contour plot using interpolated data (such as the data in plotting contour lines on an irregular grid ) . Using some sample data from plotting contours on an irregular grid , I made fill.contour and a simple scatter

R: Plot scatter plot on filled contour plot

cure I am very new to R and have made a fill.contour plot using interpolated data (such as the data in plotting contour lines on an irregular grid ) . Using some sample data from plotting contours on an irregular grid , I made fill.contour and a simple scatter

R: Plot scatter plot on filled contour plot

cure I'm very new to R and have made a fill.contour plot using interpolated data (such as data in plotting contour lines on an irregular grid ) . Using some sample data from plotting contours on an irregular grid , I made fill.contour and a simple scatter plot

R: Plot scatter plot on filled contour plot

cure I'm very new to R and have made a fill.contour plot using interpolated data (such as data in plotting contour lines on an irregular grid ) . Using some sample data from plotting contours on an irregular grid , I made fill.contour and a simple scatter plot

Empty contour plot in ggplot

side I am trying to create a simple contour plot. Sample code is attached below. The output is an empty graph with labels and warning messages - 1: stat_contour(): Zero contours were generated 2: In min(x) : no non-missing arguments to min; returning Inf 3: I

Empty contour plot in ggplot

side I am trying to create a simple contour plot. Sample code is attached below. The output is an empty graph with labels and warning messages - 1: stat_contour(): Zero contours were generated 2: In min(x) : no non-missing arguments to min; returning Inf 3: I

Empty contour plot in ggplot

side I am trying to create a simple contour plot. Sample code is attached below. The output is an empty graph with labels and warning messages - 1: stat_contour(): Zero contours were generated 2: In min(x) : no non-missing arguments to min; returning Inf 3: I

Empty contour plot in ggplot

side I am trying to create a simple contour plot. Sample code is attached below. The output is an empty graph with labels and warning messages - 1: stat_contour(): Zero contours were generated 2: In min(x) : no non-missing arguments to min; returning Inf 3: I

Empty contour plot in ggplot

side I am trying to create a simple contour plot. Sample code is attached below. The output is an empty graph with labels and warning messages - 1: stat_contour(): Zero contours were generated 2: In min(x) : no non-missing arguments to min; returning Inf 3: I

Empty contour plot in ggplot

side I am trying to create a simple contour plot. Sample code is attached below. The output is an empty graph with labels and warning messages - 1: stat_contour(): Zero contours were generated 2: In min(x) : no non-missing arguments to min; returning Inf 3: I

Create a "smooth" filled plot with ggplot

Hunk Street I have the following dataset: value <- c(0.1,0.2,0.1,0.3,0.3,0.2,0.2,0.4,0.1,0.1) emotie <- c(0,1,2,3,4,0,1,2,3,4) period <- c(1,1,1,1,1,2,2,2,2,2) df_test <- data.frame(value, emotie, period) And - using ggplot - I created the following graph: l

ggplot plot filled with standard deviation

Jonah Davis I'm trying to create a chart where X is a dichotomous categorical variable, Y is a continuous variable, and the "fill" or "grouping" variables are the +1 and -1 SD of the quantitative variable. Here is an example of what I am trying to achieve: Not

R: Filled contour plot does not plot all matrices

learning macros So my colleagues and I used the data matrix in R (using pipeline: Package dplyr, magrittr) and plotted it in the fill.contour plot. But for some reason it doesn't come up with my last two points. Does anyone have a solution for this? Here is my

R: Filled contour plot does not plot all matrices

learning macros So my colleagues and I used the data matrix in R (using pipeline: Package dplyr, magrittr) and plotted it in the fill.contour plot. But for some reason it doesn't come up with my last two points. Does anyone have a solution for this? Here is my