0

I am trying to plot boxplot using ggplot2. sample data is like this.

> sampe

count genotype
71       mt
50       mt
71       mt
95       wt
60       mt
63       mt
75       mt
82       wt
93       wt
87       wt
61       mt
102       wt
60       mt
78       wt
78       wt
87       wt
84       wt
104       wt
81       wt
85       mt


> qplot(factor(genotype),count,data=sampe,geom="boxplot")

The above command produces plot like this: enter image description here

what's wrong here?? why is it plotting like this?? Even this below code produces same output.

ggplot(sampe,aes(x=factor(genotype),y=count))+geom_boxplot()
7
  • Your code works for me. What version are you using:packageDescription("ggplot2")$Version - the latest version is 0.9.3.1 Commented Apr 2, 2013 at 10:14
  • 1
    It works well here. What gives class(sampe$count) ? I guess it's a factor ? Commented Apr 2, 2013 at 10:14
  • 8
    It seems that your count values are stored as factor in your data, that's only way I can reproduce your plot. Commented Apr 2, 2013 at 10:14
  • 1
    @poisonAlien you can answer and accept your own answer. Commented Apr 2, 2013 at 10:58
  • 4
    @poisonAlien, when a numeric value is stored as a factor you should not directly convert to numeric. you should do: as.numeric(as.character(count)). please take care of this! Commented Apr 2, 2013 at 11:02

2 Answers 2

5

Ok.. I 'll answer my own question. As per the suggestion count values were stored as factor. Converting them to numeric did the trick

qplot(factor(genotype),as.numeric(count),data=sampe,geom="boxplot")

Thank you all for the suggestions.

Sign up to request clarification or add additional context in comments.

Comments

0

I think your problem is that your Y-axis is not actually count, R understand count as a variable. Actually you only need to group the data by genotype and than do ggplot2.

df %>% group_by(genotype)
ggplot(df) +
  geom_boxplot(mapping = aes(x=genotype, y = count))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.