1

My data disappears when I truncate the y-axis to show only the 50 to 90 range. Am I putting the ylim in the wrong place?

The spreadsheet looks like this:

xval yval_LWTW  linenames   SNP
1   61.4835166  MT9513      NN
2   61.93341478 RITA        GG
3   63.31277751 JUDITH      CC
4   63.60466558 CO04W320    GG
5   64.84700514 DECADE      NN

library(ggplot2)
library(xlsx)
data <- read.xlsx("RdataForGraphsofBestHits.xlsx", sheetIndex=4)
ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP,  
                     scale_fill_manual(values=c(GG="blue",CC="red",NN="green")))) + 
                     geom_bar(stat="identity", width=1) + 
                     theme(axis.title.x=element_blank())

enter image description here

This gives me a tall plot because the data go from 61 to 81. I want to make the y-axis start at 50, so the plot will be shorter, so I add + ylim(50.0, 90.0) to the code.

library(ggplot2)
library(xlsx)
data <- read.xlsx("RdataForGraphsofBestHits.xlsx", sheetIndex=4)
ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP, 
        scale_fill_manual(values=c(GG="blue", CC="red", NN="green")))) + 
        geom_bar(stat="identity", width=1) + 
        theme(axis.title.x=element_blank()) + 
        ylim(50.0, 90.0)

Now I get the gray background of the plot with the y-axis nicely limited to the 50 to 90 range, but no data plotted onto it. Just the gray background and the axis labels and the legend. enter image description here

I think I have the code for making the plot horizontal.

ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP, 
          scale_fill_manual(values=c(GG="blue", CC="red", NN="green")))) + 
          geom_bar(stat="identity", width=1) + 
          theme(axis.title.x=element_blank()) + 
          coord_fixed(ratio=1/2)

So I can fix the horizontal problem if I can fix the disappearing data problem.

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
tortoiseshell
  • 33
  • 1
  • 7
  • Try filtering the data to match the output you are after. – Pierre L Jan 04 '16 at 18:36
  • 1
    possibly http://stackoverflow.com/questions/25685185/limit-ggplot2-axes-without-removing-data-outside-limits-zoom . ie use `coord_cartesian(ylim=c(50,90))` – user20650 Jan 04 '16 at 18:40
  • Thank you, Pierre Lafortune. Such a simple but clever solution. Not sure if I can use coord_cartesian and coord_fixed (when I specify the 1/2 ratio) in the same set of instructions, so I may well be subtracting 50 from each data point. Thanks for the idea. – tortoiseshell Jan 05 '16 at 00:16
  • Well, hm, my idea of subtracting 50 wasn't very good, unless I can change the x-axis labels to show 50 to 90 instead of zero to 40. Not sure what you mean by "filtering", although I am trying to figure out how I can make the baseline be 50. – tortoiseshell Jan 05 '16 at 00:42

1 Answers1

1

Was curious so I coded it up:

n <- 250
xval <- 1:n
yval <- 61 + 4*sin(2*pi*(1:n)/n) + 18*(1:n)/n
snp <- sample(c("CC",rep("GG",40),rep("NN",40)),n,replace=T)
data <- data.frame(xval=xval,yval_LWTW=yval,SNP=snp)

ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP)) +  
             scale_fill_manual(values=c(GG="blue",CC="red",NN="green")) + 
             geom_bar(stat="identity", width=1) + 
             theme(axis.title.x=element_blank())

Yields:

enter image description here

And this:

ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP)) +  
             scale_fill_manual(values=c(GG="blue",CC="red",NN="green")) + 
             geom_bar(stat="identity", width=1) + 
             theme(axis.title.x=element_blank()) +
             ylim(50,90)

Yields:

enter image description here

And user20650's coord_cartesian suggestion:

ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP)) +  
             scale_fill_manual(values=c(GG="blue",CC="red",NN="green")) + 
             geom_bar(stat="identity", width=1) + 
             theme(axis.title.x=element_blank()) +
             coord_cartesian(ylim=c(50,90))

yields this:

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • Mike Wise, thank you, thank you, thank you. Love your graphs. – tortoiseshell Jan 05 '16 at 00:09
  • Unfortunately, now I am getting: Error: ggplot2 doesn't know how to deal with data of class function. – tortoiseshell Jan 05 '16 at 00:31
  • Because of the name `data`. If your data frame is not defined (and uses the name `data`), ggplot will use the `data()` function from the `utils` package. R has no type checking. A lot of unintelligible error messages in R come from collisions with functions of the same name. – Mike Wise Jan 05 '16 at 11:07
  • Thanks again, Mike. So many things to know about R. – tortoiseshell Jan 05 '16 at 20:44
  • Unfortunately it is quite hard to avoid these collisions because all the good names are used up by the base R functionality already. `df` for example is the density function for the F function. Collisions everywhere... – Mike Wise Jan 05 '16 at 20:48
  • Thank you for the reminder. I am not entirely new here but I am not versed in the etiquette. Thanks again. – tortoiseshell Jan 08 '16 at 16:14
  • also notice that you have upvote privileges now... – Mike Wise Jan 08 '16 at 16:33