ggplot2 fill problem in colour shading

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

ggplot2 fill problem in colour shading

tring
Dear friends - I have 2  problems with ggplot2

Here is the code for illustration

 

x <- seq(1,10,length=10000)

y <- x^2

fill <- rep(0,length(x))

fill[(5<x)&(x<7.5)] <- "red"

ddf <- data.frame(x,y,fill)

ggplot(data=ddf,aes(x=x,y=y)) + geom_area(aes(fill=fill)) + geom_line()

 

First the embarrassing one: I would really like no fill except from x from 5
to 7.5 or transparency so the background is still visible. Have tried NULL
and NA with no effect.

The other thing is that it appears that above the blue area some of the
other color spills true - reddish on my system.

 

Windows 10

R version 3.6.1

 

All best wishes

Troels Ring

Aalborg, Denmark


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2 fill problem in colour shading

Rui Barradas
Hello,

You must put the NA in scale_fill_manual. Like this:

ggplot(data = ddf, aes(x = x, y = y)) +
   geom_area(aes(fill = fill)) +
   geom_line() +
   scale_fill_manual(values = c(NA, "red"))


Note also that 'fill' is a factor, were it numeric you would need fill =
factor(fill).


ddf2 <- data.frame(x,y,fill = as.integer(fill == "red"))

ggplot(data = ddf2, aes(x = x, y = y)) +
   geom_area(aes(fill = fill)) +
   geom_line() +
   scale_fill_manual(values = c(NA, "red"))
#Error: Continuous value supplied to discrete scale


Just change to fill = factor(fill) and the graph is exactly the same.
Which leads me to propose an easier way of creating the vector 'fill',
in the first place.

fill <- as.integer((5 < x) & (x < 7.5))

Will give the same result, coerce to factor if you like.


Hope this helps,

Rui Barradas

Às 17:13 de 11/08/19, Troels Ring escreveu:

> Dear friends - I have 2  problems with ggplot2
>
> Here is the code for illustration
>
>  
>
> x <- seq(1,10,length=10000)
>
> y <- x^2
>
> fill <- rep(0,length(x))
>
> fill[(5<x)&(x<7.5)] <- "red"
>
> ddf <- data.frame(x,y,fill)
>
> ggplot(data=ddf,aes(x=x,y=y)) + geom_area(aes(fill=fill)) + geom_line()
>
>  
>
> First the embarrassing one: I would really like no fill except from x from 5
> to 7.5 or transparency so the background is still visible. Have tried NULL
> and NA with no effect.
>
> The other thing is that it appears that above the blue area some of the
> other color spills true - reddish on my system.
>
>  
>
> Windows 10
>
> R version 3.6.1
>
>  
>
> All best wishes
>
> Troels Ring
>
> Aalborg, Denmark
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot2 fill problem in colour shading

David Winsemius
In reply to this post by tring

> On Aug 11, 2019, at 9:13 AM, Troels Ring <[hidden email]> wrote:
>
> Dear friends - I have 2  problems with ggplot2
>
> Here is the code for illustration
>
>
>
> x <- seq(1,10,length=10000)
>
> y <- x^2
>
> fill <- rep(0,length(x))
>
> fill[(5<x)&(x<7.5)] <- "red"
>
> ddf <- data.frame(x,y,fill)
>
> ggplot(data=ddf,aes(x=x,y=y)) + geom_area(aes(fill=fill)) + geom_line()
>
>
>
> First the embarrassing one: I would really like no fill except from x from 5
> to 7.5 or transparency so the background is still visible. Have tried NULL
> and NA with no effect.
>
> The other thing is that it appears that above the blue area some of the
> other color spills true - reddish on my system.
>

I'm not exactly clear on the request, but perhaps this will help clarify. Need to extract the desired portion of the ddf object to give to the geom_area function:

ggplot(data=ddf,aes(x=x,y=y)) + geom_line(color="blue") + geom_area(data=ddf[(5<ddf$x)&(ddf$x<7.5),], inherit.aes=FALSE, aes(x=x,y=y,fill=fill) )

--
David.

>
>
> Windows 10
>
> R version 3.6.1
>
>
>
> All best wishes
>
> Troels Ring
>
> Aalborg, Denmark
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.