5

I'm using ggplot in R in order to plot some data. What I am trying to do is have a scatter plot where the background is different in different regions of the chart. There is a very helpful answer here that is getting me most of the way there but not all the way.

Here is a sample of the data

row.names selectionDirector country     Totals    director.rank
1   268   Alfred Hitchcock  Argentina   14        1
2   269   Alfred Hitchcock  Australia   7         3
3   274   Alfred Hitchcock  Canada      10        1
4   286   Alfred Hitchcock  France      18        1
5   288   Alfred Hitchcock  Germany     9         6
6   296   Alfred Hitchcock  Italy       5         3
7   319   Alfred Hitchcock  Spain       21        4
8   320   Alfred Hitchcock  Sweden      4         8
9   325   Alfred Hitchcock  UK          87        1
10  330   Alfred Hitchcock  US          87        1
11  346   Andrei Tarkovsky  Argentina   4         20
12  347   Andrei Tarkovsky  Australia   2         34
13  355   Andrei Tarkovsky  Canada      2         32
14  365   Andrei Tarkovsky  France      2         37

My code is:

rects <- data.frame(xstart = seq(-0.5,8.5,1), xend = seq(0.5,9.5,1), col = letters[1:10])

ggplot() +
  geom_rect(data=rects,aes(ymin=0,ymax=80,xmin=xstart,xmax=xend,fill=col)) +
  geom_point(data=top.votes.by.director.country, aes(x=country, y=director.rank)) +
  coord_flip() +
  facet_wrap(~selectionDirector)

country is a factor with 10 values. director.rank is numeric. They both come from the data frame top.votes.by.director.country. The idea is to have a background that is different for the horizontal area for each country to make it easier to read when I facet. enter image description here

Imagine the picture above except that instead of colored points, there would be a colored band behind a black point for each country. The closest approximation I can find online is the chart below taken from the answer from the link above:

enter image description here

So there would be a colored background for each country in the faceted graph just as the chart above has a colored background for each region.

The problem is that when I run the code above, I get the following error.

Error: Discrete value supplied to continuous scale

When I remove the geom_rect portion, it works fine. If I move the geom_rect above the facet_wrap, I get a chart, although it's messed up. If I do just the geom_rect part, I get the background I want approximately.

I've been messing around with this for a couple of hours now and can't get it to work.

Community
  • 1
  • 1
Alexander Moore
  • 467
  • 2
  • 5
  • 13
  • Can you provide a sample of your data? And also an example of the expected output. – Molx Jul 13 '15 at 13:02
  • I've tried to add some more relevant information into the post. I added a sample of the data and the closest approximation I can find to the output that I want. It would be some kind of combination of the chart that I created and the chart below it where the backgrounds are different colors. – Alexander Moore Jul 13 '15 at 13:27

1 Answers1

5

The error happened you try to plot Discrete value to continuous scale. In your case, you plotted "country" to "-0.5~9.5" in x. You could change your plotting order.

I used "Alfred Hitchcock" selectionDirector to plot by the following code

rects <- data.frame(xstart = seq(0.5,9.5,1), xend = seq(1.5,10.5,1), 
         col = letters[1:10])
ggplot() + geom_point(data=top.votes.by.director.country, 
                      aes(x=country, y=director.rank)) +
           geom_rect(data=rects, aes(ymin=0, ymax=80, xmin=xstart,
                      xmax=xend, fill=col), alpha =0.5) + coord_flip()

and the result is following picture.

enter image description here

Note: I shift the x of rect by "1"(begin at 0.5, not -0.5), and add alpha attribute to rect.

peterchen932
  • 103
  • 1
  • 7
  • Thank you very much! That worked great. I am still a bit fuzzy on what the problem was. I understand that country is discrete and "-0.5~9.5" is not, but I don't quite understand the mechanics of how country is getting assigned to that or why doing it the other way around doesn't result in an error also. – Alexander Moore Jul 14 '15 at 06:56
  • 1
    I did not understand the mechanism detial. In my experience, when the error happened, it could be solved by following way, plotting "continous" scale and then "discrete" or using the scale function (ex: [scale_x_discrete](http://docs.ggplot2.org/current/scale_discrete.html), [scale_x_continous](http://docs.ggplot2.org/current/scale_continuous.html)...) to change the scale. – peterchen932 Jul 15 '15 at 00:20