2
votes

Let's say, I have data like following example,

dat1 <- data.frame(group = c("a", "a","a", "a", "a", "b", "b", "b","b","b","b","b","c","c","c"),
                   subgroup = c(paste0("R", rep(1:5)),paste0("R", rep(1:7)),paste0("R", rep(1:3))),
                   value = c(5,6,0,8,2,3,4,5,2,4,7,0,3,4,0),
                   pp = c("AT","BT","CT","AA","AT","TT","RT","CC","SE","DN","AA","MM","XT","QQ","HH"))

I want to add some cut off as

dat1 = dat1[dat1$value > 2, ]

My code is

library(ggplot2)
pl <- ggplot(dat1, aes(y = pp, x = subgroup)) + 
        geom_point(aes(size=value)) +  
        facet_grid(cols = vars(group), scales="free", space="free")+ 
        ylab("names") +xlab(" ")
pl 

enter image description here

I want to see all the columns of each panel. For example in the first panel, there should be five subgroups, I see only three columns in the figure, but I just want to see all five columns even if below of cut off or zero. The second panel has 7 scales. After cut off, there are 6 columns, but I want to see all 7 scales even if it has zero.

How can I modify my code or make as this kind of plot?

1
Yes, I have tried. It did not work.Indigofera suffruticosa
What about: facet_grid(cols = vars(group), scales="free_y", space="free")Henry Cyranka
Nope. I have already used scales and space.Indigofera suffruticosa
scales = "fixed" is what you need. You won't see the ones that are excluded from your cut-off - you removed these from the df, so it can't show them.bob1
No, it does not work. When I used scales = "fixed", every panel have six columns, because the second panel has six columns. But I want five columns in the first panel, seven columns in the second panel, three columns in the third panel whatever their value is.Indigofera suffruticosa

1 Answers

0
votes

You need to subset without dropping a level.

dat1 <- data.frame(group = c("a", "a","a", "a", "a", "b", "b", "b","b","b","b","b","c","c","c"),
               subgroup = c(paste0("R", rep(1:5)),paste0("R", rep(1:7)),paste0("R", rep(1:3))),
               value = c(5,6,0,8,2,3,4,5,2,4,7,0,3,4,0),
               pp = c("AT","BT","CT","AA","AT","TT","RT","CC","SE","DN","AA","MM","XT","QQ","HH"))

dat1[dat1$value <= 2, 3] <- NA

pl <- ggplot(dat1, aes(y = pp, x = subgroup)) + 
 geom_point(aes(size=value)) +  
 facet_grid(~group, scales="free_x", space = "free")+ 
 ylab("names") +xlab(" ") +
 scale_size(range = c(3,8))