Reputation: 1444
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
Upvotes: 2
Views: 663
Reputation: 81683
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
Upvotes: 3