Show every nth row name in ggplot2

2019-07-12 13:34发布

问题:

I have a dataframe in the following long format:

> head(cleanLongPlotData)
  Structure Method Value       Outcome
1      1A00 X1     1    Clustering
2      1A01 X1     1    Clustering
3      1A02 X1     0 No Clustering
4      1A0U X1     1    Clustering
5      1A0Z X1     1    Clustering
6      1A1M X1     0 No Clustering
> tail(cleanLongPlotData)
      Structure     Method Value       Outcome
12931      4PRN       Z        0 No Clustering
12932      4PRP       Z        0 No Clustering
12933      4PXZ       Z       -1         Blank
12934      4PY0       Z       -1         Blank
12935      4Q3H       Z       -1         Blank
12936      6HBW       Z        1    Clustering

Each method has 2,196 observations. I'm plotting it like this:

    p1 <- ggplot(cleanLongPlotData, aes(x=Method, y=Structure,fill=Outcome)) + geom_tile()+
   xlab("Method") +
   ylab("Structure")+
   ggtitle("Cluster Results By Structure")+
      theme(axis.line=element_blank(),
            axis.text.y=element_blank(),axis.ticks=element_blank(),
            panel.background=element_blank(),panel.border=element_blank(),
            panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),plot.background=element_blank())+
      scale_fill_manual(values = c("#F5F5F5","green","blue"))

I've blocked out the rows because they overlap each other and make a mess. Is there a way to show every 100th row name? Or every 200th row name?

回答1:

Here's an example of displaying every other factor level in an axis. Seems like a really bad idea though...

df <- data.frame(Method=rep(LETTERS[1:10], each=10),
                 Structure=rep(LETTERS[17:26]), 
                 Outcome=sample(letters[1:5],100,replace=TRUE))
library(ggplot2)
ggp <- ggplot(df, aes(Method, Structure))+geom_tile(aes(fill=Outcome))+coord_fixed()
ggp

lvls <- levels(df$Structure)
ggp + scale_y_discrete(breaks=lvls[seq(1,length(lvls),by=2)])