1
$\begingroup$

everyone! I'm trying to plot pvalues of a test by categories (in this case, genetic loci). So, the x-axis contains gene names. I'm getting the plot to work, but the x-axis was very busy, so I am thinking of splitting the screen into several plots, each containing only a few loci (i.e., a few x values). When I attempted to plot a subset of my data with the script below, I got a plot that still contained far too many x-values on the x-axis. In other words, the bars that were plotted were all crowded onto the left-hand side of the plot, and many x-values had nothing. Any idea what I'm doing wrong? It's hard to read the loci names on the x-axis, so I can't tell whether it's repeating the same locus names (i.e., maybe a problem having to do with melt()) or whether it's plotting all of the Loci values from data instead of from split1_data. Alternatively, any different recommendations for how to make a plot with hundreds of bars easy to see? Thank you so much!

library(reshape)
library(ggplot2)
require(ggplot2)

setwd("/Users/markfisher/Desktop")

sink(file="/Users/markfisher/Desktop/Pvalue_HWE_output.txt")
data=read.csv("Pvalues_of_all.csv", header=TRUE)
attach(data)
print(data$Loci[1:10]) 

split1_data<-subset(data,data$Loci %in% data$Loci[1:10])

split1_datam<-melt(split1_data,id="Loci")
print("split1_data$Loci")
    print(split1_data$Loci) 
sink()

pdf('/Users/markfisher/Desktop/pvalue_sensitivity.pdf', bg = "white")
p <- ggplot(split1_datam, aes(x =Loci, y = value, color = variable, width=.15))
p + geom_bar(position="dodge") + ylab("P-value")+ geom_hline(yintercept=0.05)
dev.off()

Update: I added made the suggested change of using droplevels(), and got an error about an unexpected numeric constant. This is the output from my R console:

> library(reshape)
> library(ggplot2)
> require(ggplot2)
> setwd("/Users/markfisher/Desktop")
> sink(file="/Users/markfisher/Desktop/Pvalue_HWE_output.txt")
> data=read.csv("Pvalues_of_all.csv", header=TRUE)
> attach(data)
> print(data$Loci[1:10]) 
    > split1_data<-droplevels(subset(data,data$Loci %in% data$Loci[1:10]))
    > split2_data<-subset(data,data$Loci %in% data$Loci[11:20])
    > split3_data<-subset(data,data$Loci %in% data$Loci[21:30])
    > split4_data<-subset(data,data$Loci %in% data$Loci[31:40])
    > split5_data<-subset(data,data$Loci %in% data$Loci[41:51])
    > split6_data<-subset(data,data$Loci %in% data$Loci[52:62])
    > 
    > split1_datam<-melt(split1_data,id="Loci")
    > print("split1_data$Loci")
> print(split1_data$Loci)   
> 
> 
> sink()
> pdf('/Users/markfisher/Desktop/pvalue_sensitivity.pdf', bg = "white")
> 
> p <- ggplot(split1_datam, aes(x =Loci, y = value, color = variable, width=.15))1
Error: unexpected numeric constant in "p <- ggplot(split1_datam, aes(x =Loci, y = value, color = variable, width=.15))1"
> p + geom_bar(position="dodge") + ylab("P-value")+ geom_hline(yintercept=0.05)
Error: object 'p' not found
> dev.off()
null device 
1 
>

If anyone needs to see my output file, it looks like this:

The following object(s) are masked from 'data (position 3)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 4)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 5)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 6)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 7)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 8)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 9)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 10)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 11)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 12)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 13)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
The following object(s) are masked from 'data (position 14)':
    All, Loci, X1_only, X1_removed, X2_only, X2_removed, X3_only, X3_removed,
    X4_only, X4_removed, X5_only, X5_removed, X7_only, X7_removed, X78_only,
    X8_only, X8_removed
 [1] Baez       Blue       C147       C204       C21        C278_PT    C294       C316      
 [9] C485       C487_PigTa
62 Levels: Baez Blue C147 C204 C21 C278_PT C294 C316 C485 C487_PigTa C536 Carey ... Yellow
[1] "split1_data$Loci"
 [1] Baez       Blue       C147       C204       C21        C278_PT    C294       C316      
 [9] C485       C487_PigTa
Levels: Baez Blue C147 C204 C21 C278_PT C294 C316 C485 C487_PigTa

I would also show what my plot looks like, but alas I do not yet have enough cool points on CrossValidated to do so. Thanks again for all of your help with this. I hope my updates clarify things a little bit...

$\endgroup$
2
  • $\begingroup$ All of those "the following objects are masked" warnings are because you use attach(data). attach() is generally deprecated because you have the problem that it becomes difficult to tell which version of the data you are referring to. From eyeballing your code I don't think you need it at all. Removing that would at least remove some of the cluttering problems. $\endgroup$ Commented Jun 8, 2012 at 23:43
  • $\begingroup$ ok, and your other problem is a rogue "1" that has appeared out of nowhere: "width=.15))1" $\endgroup$ Commented Jun 8, 2012 at 23:45

2 Answers 2

3
$\begingroup$

I'm going to put on my mind reading hat and suggest that you simply add droplevels when you subset:

split1_data <- droplevels(subset(data,data$Loci %in% data$Loci[1:10]))

The likely cause of the "problem" is that Loci is a factor. Subsetting a factor may reduce the levels that are present, but it doesn't change the set of levels as an attribute of the factor. If this behavior of factors disturbs you, you can avoid it by using character vectors instead by default by setting options(stringsAsFactors = FALSE).

(But in the future, please note that it is in general impossible to diagnose problems like this without more detailed information about your data, say the output from str or dput. Please include such things in future questions.)

$\endgroup$
3
  • $\begingroup$ Hey, Joran! Thanks for your response and apologies for the format. You have admirable mind-reading abilities. I attempted to use droplevels, but it produced another error (see above). Any suggestions for this one? $\endgroup$
    – Atticus29
    Commented Jun 8, 2012 at 22:24
  • $\begingroup$ @Atticus29 (a) It looks like you typed a "1" where you shouldn't have, (b) Note that I specifically suggested using str and dput, which you failed to do. Most of what you added wasn't useful. Typing str(split1_datam) will confirm that the variable is a factor. Typing dput(split1_datam) will yield a version of your data that we can copy+paste into our R sessions. See here for examples. $\endgroup$
    – joran
    Commented Jun 8, 2012 at 22:51
  • $\begingroup$ thanks for that. I had no idea what you were talking about with respect to str and dput- I was going to look them up later. Many apologies. It worked quite nicely this time without the typo. Thanks so much for your help! $\endgroup$
    – Atticus29
    Commented Jun 8, 2012 at 23:38
3
$\begingroup$

I imagine that Loci is a factor, and as such contains information about all possible values that Loci could take on (not just the ones that it happens to take on in your particular subset of data). Two approaches to getting around this are to use droplevels on split1_datum

p <- ggplot(droplevels(split1_datam), aes(x =Loci, y = value, color = variable, width=.15))

or to convert Loci to a character variable

p <- ggplot(split1_datam, aes(x =as.character(Loci), y = value, color = variable, width=.15))
$\endgroup$
1
  • $\begingroup$ Hey, Brian! Thanks for your response and apologies for the format. As I mentioned to Joran above, I attempted to use droplevels, but it produced another error (see above). Any suggestions for this one? Thanks! $\endgroup$
    – Atticus29
    Commented Jun 8, 2012 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.