Geom.point in ggplot2, conditional form

I am putting together my first plot with ggplot2. I need to set the shape for values ​​== 0. Here's my dataset and what I've gotten so far:

structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L), .Label = c("MD-1", "MD-2", "MD-3", "MD-4", "ME-1",
"ME-2", "ME-3", "ME-4", "ME-5", "ME-6", "MF-1", "MF-2", "MF-4",
"MF-6", "MF-7", "MF-8"), class = "factor"), Var2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 4L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
14L, 15L, 4L, 3L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 0L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 3L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 13L, 2L, 5L, 7L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 1L, 1L)), .Names = c("Var1",
"Var2", "Freq"), row.names = c(NA, -192L), class = "data.frame")

      

Here's the base of my plot

p <- ggplot(mat.bub, aes(Var1, Var2)) 
p + geom_point(aes(size = Freq))

      

Now, how to set geom.point to a specific shape if Freq == 0? Here's what I've tried so far:

p <- ggplot(mat.bub, aes(Var1, Var2,size=Freq)) 
p + geom_point(aes(Var1[Freq==0], Var2[Freq==0]), colour="black", shape=3, size=5, na.rm = T)

      

Inspired by this answer:

Reshaping a subset of points with ggplot2

But I get "the arguments imply a different number of lines: 162, 192". Of course Var1 and Var2 are not numeric, which is different from the mtcars example.

How could I achieve this conditional formation? What am I missing?

Thanx for any help!

+3


source to share


1 answer


As per my note you linked to, try this:

p <- ggplot(mat.bub, aes(Var1, Var2,size=Freq)) + geom_point()
p + geom_point(data = subset(mat.bub,Freq == 0), colour="black", shape=3, size=5, na.rm = T)

      



enter image description here

By way of explanation, while a subset in variables is possible, I prefer to give each geometta a specific subset of whatever dataframe I'm dealing with. It seems to me to be easier to keep right in my head and seems to be less confusing for ggplot.

+9


source







All Articles