15

Please note I am beginner with R. I have merged two data frames with one common column with merge() method. I have obtained data frame like:

 x   y1   y2
 1   3    5
 2   2    4
 1   2    2
 3   5    5
 ...

etc. i would like to plot such data frame with ggplot. What I have created (using documention of geom_point is

ggplot(data = dat_c, aes(games, variance.x)) + 
     geom_point(aes(x = games, y = variance.x), legend=  TRUE,  xlab="X", ylab="Y", colour=alpha('red', 0.05)) + 
     geom_point(aes(x = games, y = variance.y), legend = TRUE, colour=alpha('blue', 0.05) )

It works, NaNs do not disturb me because I get warning that they are ignored, which is fine. However I have two problems and I am not sure how to fix them:

  1. my actual plot is located at the bottom-left corner, I would like to set max values for X and Y axis (in a dynamic manner, for example with highest value from data + 100 or something like this)
  2. the legend is not displayed
  3. the axis are not described

Here is how it looks hlike: enter image description here

mkk
  • 7,583
  • 7
  • 46
  • 62

2 Answers2

12

See also:

(these are the results of searching [r] ggplot melt, although you might also have gotten there via [r] ggplot legend ...)

If you can, get a copy of the ggplot book and read it from the beginning -- unfortunately the PDF of the draft is no longer available online, but the book is worth the investment.

  1. You actually have some points with x and y values near the extremes of your plot. It's just hard to see them because they're nearly transparent (it will be a little easier to see them on a white background, i.e. try adding +theme_bw() to your ggplot call). You can use xlim and ylim if you want to restrict the range of the plot. (Try summary on your data and check out the Max values ...)

  2. the best way to get the axes drawn is to follow the ggplot idiom of "melting" your data into a long-format data set with one column for the category (y1 vs y2) and another for the value, as follows:


  d <- data.frame(x=c(1,2,1,3),
                y1=c(3,2,2,5),
                y2=c(5,4,2,5))
  library(ggplot2) 
  library(reshape2) ## for melt()
  dm  <- melt(d,id.var=1)
  ggplot(data=dm,aes(x,value,colour=variable))+
  geom_point(alpha=0.2)+
  scale_colour_manual(values=c("red","blue"))+
  labs(x="games",y="variance")

(sorry for the slightly odd formatting) I set the alpha value a little higher because otherwise it would have been hard to see the points in the figure. I think the default colours (reddish and blue-ish) are OK, but I used scale_colour_manual to get them the way you specified. enter image description here

  1. I'm not sure what you mean.
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • thank you very much for your answer. The last point was just about labels for X and Y axis - you have covered it by labs() method, which I did not know (I tried xlab and ylab). – mkk Aug 25 '11 at 16:19
  • OK. It should also work if you do `+xlab("games")+ylab("variance")` (as separate terms in the ggplot object, not within the `geom_point()` call ...) – Ben Bolker Aug 25 '11 at 16:23
  • 1
    @BenBolker does `scale_colour_manual` have the named argument `value`? It seems to me it is `values`, with the final 's'. – Alessandro Jacopson Feb 27 '19 at 11:02
9

You should melt your data into long format and then map the colour aesthetic to the variable column from the melted data.frame. Something like this:

dat <- data.frame(x = c(1,2,1,3), y1 = c(3,2,2,5), y2 = c(5,4,2,5))

dat.m <- melt(dat, id.vars = "x")

ggplot(dat.m, aes(x, value, colour = variable)) +
  geom_point() +
  scale_colour_manual(values = c("red", "blue"))

You can manually set the limits with xlim() and ylim() respectively. It's not clear what you're doing with alpha, so I'll leave that one up to you.

Chase
  • 67,710
  • 18
  • 144
  • 161