[R] ggplot pale colors

Ista Zahn izahn at psych.rochester.edu
Sat May 28 19:56:43 CEST 2011


Hi Victor,
The problem is that you have not grasped the difference between
setting an aesthetic to a fixed value and mapping it to a variable.
See below for details.

On Sat, May 28, 2011 at 6:16 AM, Victor Gabillon
<victor.gabillon at inria.fr> wrote:
> Hello i am new to ggplot and i observed a strange behavior.
>
> I want to display two groups of points, each group with a different color.
> But i encountered a problem with the colors.
>
> Here is a first example:
>
> dataset <- data.frame(Main = c("A", "A", "B", "B"), Detail = c( "b", "c", "1", "2"), resp = runif(4, min = 0.5, max = 1))
>
> ggplot(dataset, aes(x = Detail, y = resp)) +
> facet_grid(.~Main, scales = "free_x")+ geom_point(aes( size=6,shape = c(16,16,15,15) ),colour="blue")+geom_hline(aes(yintercept=0.25),colour='blue', size=2)
>
> with this code all the point are blue (like the line below)
>
>
> But if i try the following code, where my goal is to have the point on the left blue and the one on the right red, a problem appears:
>
> dataset <- data.frame(Main = c("A", "A", "B", "B"), Detail = c( "b", "c", "1", "2"), resp = runif(4, min = 0.5, max = 1))
>
> ggplot(dataset, aes(x = Detail, y = resp)) +
> facet_grid(.~Main, scales = "free_x")+ geom_point(aes( size=6,shape = c(16,16,15,15) ,colour=c("blue","blue","red","red")))+geom_hline(aes(yintercept=0.25),colour='blue', size=2)
>

The colors are not coming from c("blue", "blue", "red", "red"). Try this to see

p <- ggplot(dataset, aes(x = Detail, y = resp)) +
facet_grid(.~Main, scales = "free_x") +
  geom_point(aes(shape = c(16,16,15,15)
,colour=c("purple","purple","foo","foo")), size = 6) +
  geom_hline(aes(yintercept=0.25),colour='blue', size=2)
p

So now you see that you are not setting the colors to be red and blue,
you are mapping the color to a variable that just happens to have
levels "red" and "blue". The actual colors that are mapped to that
variable are determined by scale_colour_discrete, as you can see from

p + scale_color_manual(value=c("red", "blue"))
p + scale_color_manual(value=c("green", "black"))

As a general rule, if you want an aesthetic (e.g., color, size, shape
etc.) to vary (i.e., to have more than one value) you should put it as
an argument to aes(). If you just want to set it to a fixed value you
should set it as an argument to geom_* or as an argument to ggplot()
itself.
> The points have different colors but those colors are pale (dull). You can see it by comparing the blue of the points to the blue of the line.
> I guessing i am duing it wrong but i'm stucked with it.
>
> Do you have suggestions?
>
> An additional question is that i want to add text along the blue line (which is a reference) but i did not understand what geom_text was expecting.

see http://had.co.nz/ggplot2/geom_text.html

Best,
Ista
>
> Thanks for your help!
>
> Victor
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list