Reputation: 15
Hej!!
I have a dataset with three variables. The third variable contains NA. Example:
A <- c(1,2,5,6,8,9,2,5)
B <- c(9,4,6,2,8,4,6,9)
C <- c(1,2,NA,3,2,NA,NA,1)
df <- data.frame(A,B,C)
Now I would like to make a scatterplot from A and B with dots, and have the dots the size of C.
p <- ggplot(df, aes(y=B, x=A)) + geom_point(shape=1, size=C)
p
However now I get the warning message that 3 rows are removed because of missing values in C. This makes sense, but I would like to have these points also in the scatterplot, but then as a X. So I would like to have it look like:
Anyone who can help me how to do this in R?
Upvotes: 0
Views: 373
Reputation: 2485
Try adding a column to your dataframe that controls the shape:
A <- c(1,2,5,6,8,9,2,5)
B <- c(9,4,6,2,8,4,6,9)
C <- c(1,2,NA,3,2,NA,NA,1)
D <- c(1,1,4,1,1,4,4,1)
df <- data.frame(A,B,C,D)
# replace NA with the value you want for the size of missing points
df[is.na(df)] <- 2
ggplot(df) +
geom_point(aes(y = B, x = A, size = C, shape = D)) +
scale_shape_identity()
In vector D
the value 4
is not random, but is the value corresponding to the symbol x
in shape.
Upvotes: 1