2

I'd have a data table which is displayed fine. However, if I want to label the points it ends up in chaos since there is too many datapoints. Therefore I'd like to display only some labels. I tried it like this:

p <- ggplot() + 
coord_fixed() +
theme_bw() +
theme(
legend.position="none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
scale_size_area(max_size = 20) +
scale_x_continuous("<= Links - Rechts =>") +
scale_y_continuous("<= Konservativ - Liberal =>") +
layer(
data=werte,
mapping=aes(x=(xy$x + abs(X_Mini) ) * 150 / (X_Maxi-X_Mini), 
y=(xy$y + abs(Y_Mini) ) * 150  / (Y_Maxi-Y_Mini),
            size=werte$avg_gult,
            colour = werte$kern_uml,
            alpha = 1/100 ), 
geom="point",
scale_size_area(max_size = 30)
) +
geom_text(data=subset(werte, avg_gult > 1000),
aes(x=(xy$x + abs(X_Mini) ) * 150 / (X_Maxi-X_Mini),
y=(xy$y + abs(Y_Mini) ) * 150  / (Y_Maxi-Y_Mini))
aes(label=werte$Gemeinde))
p 

But that's what R gives me:

Error in data.frame(x = c(80.0625, 74.3625, 80.9625, 73.3125, 79.875, : arguments imply differing number of rows: 1568, 374

If I do it like that

geom_text(aes(x=(xy$x + abs(X_Mini) ) * 150 / (X_Maxi-X_Mini),
y=(xy$y + abs(Y_Mini) ) * 150  / (Y_Maxi-Y_Mini),
aes(label=werte$Gemeinde)))

it works, but as mentioned, I don't want all of points labelled (only those with a value of avg_gult above 1000). All the help is appreciated, thanks.

werte looks like this:

         x      y   avg_gult        Gemeinde    kern_uml
1   -0.230  1.720   559     Aeugst am Albis     U
2   -0.534  1.018   2595    Affoltern am Albis  U
3   -0.182  1.546   1395    Bonstetten          U
4   -0.590  1.194   1017    Hausen am Albis     0
5   -0.240  1.443   988     Hedingen            U
6   0.810   0.169   312     Kappel am Albis     0
7   -0.430  0.915   526     Knonau              U
8   -0.517  0.484   202     Maschwanden         0
etc.

X_Maxi <- 3.5
X_Mini <- -4.5

Y_Maxi <- 3.5
Y_Mini <- -4.5
Thomas
  • 1,392
  • 3
  • 22
  • 38
  • What is `xy`? Another data frame? – Sven Hohenstein Jan 07 '14 at 13:03
  • @Sven Hohenstein: Exactly. – Thomas Jan 07 '14 at 13:07
  • 1
    I recommend providing `werte` and `xy`. – Sven Hohenstein Jan 07 '14 at 13:36
  • In general, I think `ggplot` is not very happy when fed with both data in the `data` argument (e.g. `data=werte`), and with vectors from another (or the same for that sake) data frame in `aes`, using `$` (e.g. `xy$x`). Perhaps a matter of taste, but I tend to prepare my data (e.g. subsetting, aggregating) _before_ I plug the data in `ggplot`. We still don't know what 'xy' looks like. Please provide [a minimal, reproducible sample](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) of _all_ necessary data. Especially, check `dput`. Thanks. – Henrik Jan 07 '14 at 14:15
  • Sorry, I forgot to mention that I now merged 'xy' into 'werte'. Therefore the columns of 'xy' are now displayed by the columns x and y in 'Werte' – Thomas Jan 07 '14 at 14:25
  • OK. But try to provide data using the `data` argument only. _Not_ "`$-vectors`" in `aes`. – Henrik Jan 07 '14 at 14:38
  • Your example is still not reproducible: `X_Maxi, X_Mini, Y_Maxi, Y_Mini` are missing. – Henrik Jan 07 '14 at 15:52

1 Answers1

3

Since all values all present in werte, you can remove all instances of werte$ and xy$.

Second, you use two aes inside geom_text. Put all into one aes and it will work:


library(ggplot2)
p <- ggplot() + 
  coord_fixed() +
  theme_bw() +
  theme(
    legend.position="none",
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank()) +
  scale_size_area(max_size = 20) +
  scale_x_continuous("<= Links - Rechts =>") +
  scale_y_continuous("<= Konservativ - Liberal =>") +
  layer(
    data=werte,
    mapping=aes(x=(x + abs(X_Mini) ) * 150 / (X_Maxi-X_Mini), 
                y=(y + abs(Y_Mini) ) * 150 / (Y_Maxi-Y_Mini),
                size=avg_gult,
                colour = kern_uml,
                alpha = 1/100 ), 
    geom="point",
    scale_size_area(max_size = 30)
  ) +
  geom_text(data=subset(werte, avg_gult > 1000),
            aes(x=(x + abs(X_Mini) ) * 150 / (X_Maxi-X_Mini),
                y=(y + abs(Y_Mini) ) * 150  / (Y_Maxi-Y_Mini),
               label=Gemeinde))
p 
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Thanks for that first input, unfortunately that does not work either. R gives me: > Error in match(x, table, nomatch = 0L) : attempt to apply non-function – Thomas Jan 07 '14 at 13:21