Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stat_contour fill - 2 issues #1066

Closed
atsikerdekis opened this issue Mar 11, 2015 · 5 comments
Closed

stat_contour fill - 2 issues #1066

atsikerdekis opened this issue Mar 11, 2015 · 5 comments

Comments

@atsikerdekis
Copy link

Hello everyone,

I have been using ggplot2 for quite sometime now it's a great library! But I am stuck on some (known from what I have searched) issues of stat_contour.

  1. When I set geom="polygon" the levels that are not fully closed are not filled correctly.
  2. Higher levels cover/hide the lower levels.

Straight to a simple example to show you what I mean:

Creating the data

library(ggplot2)
x = seq(-1, 1, length.out = 100)
y = x
R = expand.grid(x, y)
dim(R)
names(R) = c("x", "y")
R$z = R$x^2 + R$y^2
head(R)

Ploting contour

ggplot(R, aes(x = x, y = y, z = z)) + stat_contour()
stat_contour1

Ploting filled contour by setting geom="polygon"

ggplot(R, aes(x = x, y = y, z = z)) + stat_contour(geom="polygon")
stat_contour2

In order to see what is filled correctly and what is not

ggplot(R, aes(x = x, y = y, z = z, fill=..level..)) +
stat_contour(geom="polygon") +
stat_contour() +
scale_fill_gradient(low = "blue", high = "red") +
coord_map(xlim=c(-1,1),ylim=c(-1,1))
stat_contour3

And if we reverse the values of R$z we can display the inner/hidden levels (multiply *-1)

ggplot(R*-1, aes(x = x, y = y, z = z, fill=..level..)) +
stat_contour(geom="polygon") +
stat_contour() +
scale_fill_gradient(low = "blue", high = "red") +
coord_map(xlim=c(-1,1),ylim=c(-1,1))
stat_contour4

The first problem is somehow fixed (not completely) by expanding my grid with the same values that has in each side. Something like this:

R_fill <- subset(R, x==1 | x==-1 | y==1 | y==-1)
R_fill[R_fill$x==1,]$x=5
R_fill[R_fill$x==-1,]$x=-5
R_fill[R_fill$y==1,]$y=5
R_fill[R_fill$y==-1,]$y=-5
R_fill <- rbind(R_fill,R)
ggplot(R_fill*-1, aes(x = x, y = y, z = z, fill=..level..)) +
stat_contour(geom="polygon") +
stat_contour() +
scale_fill_gradient(low = "blue", high = "red") +
coord_map(xlim=c(-1,1),ylim=c(-1,1))
stat_contour5

To sum up, (1) every level that is higher than 1 is not filled correctly since the level line is not closed on the edges. So whenever the level is not closed, ggplot is forcing a closure by connecting the two edges of the semi-circles. Maybe the only way to correct the above problem is to handle and plot the levels in stat_contour as polygons with certain coordination. Can I somehow obtain the exact point position that belong to each contour line? If I could then I would be able to plot them as polygons and set a certain box around the region I want to plot and "close" the "un-closed" levels in order to be filled correctly.

And (2) since ggplot (to my understanding) is plotting lower to higher levels, the lower levels are obscured. I have no idea how this could be fixed, since some datasets contain regions of lower values around higher levels.

I don't know how hard is to tackle and correct these problems since I have no experience on the ggplot code. It would be really useful if stat_contour acted as filled.contour, since ggplot2 has some many possibilities. So I would like to hear your thoughts and your suggestions.

Thank you in advance,
Thanos

@chengvt
Copy link

chengvt commented May 7, 2015

I am also hoping that stat_contour(geom="polygon") can act as filled.contour too. Hopefully this problem will be considered.

@hadley
Copy link
Member

hadley commented May 7, 2015

Fixing this is complicated because ggplot2 uses contourLines(). contourLines() doesn't complete contour lines to the edges of the plot, so someone either needs to rewrite the contouring algorithm from scratch to do this (hard), or look at the output from contourLines() and patch it up (somewhat less hard).

@atsikerdekis
Copy link
Author

Yeah it demands some serious coding to make it work without flaws. For the time being I did some easy changes in the filled.contour and I am using that. But as I said ggplot2 is more powerful. I think it would be good to have this tread in case someone in the future wants to work on that update. Thanks for the feedback, Cheers!

@hadley hadley closed this as completed Jun 10, 2015
@LukeDetwiler
Copy link

raidsteel,

I'd be very interested in your altered filled.contour code. I'm having similar issues with blank polygons in stat_contour and the current functionality for filled.contour doesn't provide the necessary functionality for me.

@chengvt
Copy link

chengvt commented Jun 16, 2017

@LukeDetwiler not sure if this is what you are looking for but you may find this hack useful. https://stackoverflow.com/questions/28469829/how-to-fill-in-the-contour-fully-using-stat-contour

@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants