y-axis scale issues with bar graph

35 views
Skip to first unread message

Mary Ellen

unread,
Feb 27, 2013, 1:14:11 AM2/27/13
to ggp...@googlegroups.com
I'm working on attempting to show a bit of the rank of each student's score within the subject using a bar graph for ggplot.

Everything looks great until I look at the y-axis and see that the legend misplaces where the 10 should go on a scale of 1-10 (puts it right after 1 and before 2)

I've included the datasets, my code, as well as what I currently have for a graph.

Data:
scores
      Subject     Student_A Student_B Student_C  Student_D Student_E
1         Art           10            1                   5               3          2
2     Science         1           10                  5                4          8
3     History          8            5                   7                1          1
4       Sport           2            9                  5                10         4
5     Physics          -            1                  2                 3         1
6     English           2           8                  6                 8         5
7   Chemistry         4            2                  1                1         4
8 Mathematics        10         2                  5                4         2

Code:
scores2<- melt(scores, id=c("Subject"))
scores2
dcast(scores2, formula = Subject ~ variable)

rank.bar <- ggplot(scores2, aes(x = Subject, y = value, fill=variable)) + geom_bar(stat="identity", position=position_dodge(), colour="black")
rank.bar
rank.bar + xlab("Subject") + ylab("Scores") + ggtitle("Rank of Student Scores by Subject")

Visualization:


Brandon Hurr

unread,
Feb 27, 2013, 3:00:53 AM2/27/13
to Mary Ellen, ggplot2
Because you have a character in your vector "-" it is treating it as a factor and putting it in "alphabetical" order. If you are using "-" to represent someone not taking a class, consider recoding this to NA and is necessary, forcing it to use them as numbers with as.numeric(). 

B



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mary Ellen

unread,
Feb 27, 2013, 10:15:01 PM2/27/13
to ggp...@googlegroups.com, Mary Ellen
I did apply as.numeric and it replaced the missing values - however - I am still getting a skewed y-axis and I am not sure what it is about either the data set or the ggplot bar graph. 

Dennis Murphy

unread,
Feb 28, 2013, 2:12:23 AM2/28/13
to Mary Ellen, ggp...@googlegroups.com
Hi:

This works for me:

# Note the na.strings = argument...
scores <- read.table(header = TRUE, na.strings = "-", text = "

      Subject     Student_A Student_B Student_C  Student_D Student_E
1         Art           10            1                   5               3          2
2     Science         1           10                  5                4          8
3     History          8            5                   7                1          1
4       Sport           2            9                  5                10         4
5     Physics          -            1                  2                 3         1
6     English           2           8                  6                 8         5
7   Chemistry         4            2                  1                1         4
8 Mathematics        10         2                  5                4         2
")

library(reshape2)
library(ggplot2)

scores2<- melt(scores, id=c("Subject"))
> str(scores2)
'data.frame':   40 obs. of  3 variables:
 $ Subject : Factor w/ 8 levels "Art","Chemistry",..: 1 7 4 8 6 3 2 5 1 7 ...
 $ variable: Factor w/ 5 levels "Student_A","Student_B",..: 1 1 1 1 1 1 1 1 2 2 ...
 $ value   : int  10 1 8 2 NA 2 4 10 1 10 ...

# Note that scores2$value is integer - my guess is that
# in your version, value is a factor.


ggplot(scores2, aes(x = Subject, y = value, fill=variable)) +
    geom_bar(stat="identity", position=position_dodge(), colour="black") +

    xlab("Subject") + ylab("Scores") +
    ggtitle("Rank of Student Scores by Subject") +
    coord_flip()

I just liked the coord_flip() version better because it avoids labeling issues. YMMV.

Dennis

Mary Ellen

unread,
Feb 28, 2013, 12:41:25 PM2/28/13
to ggp...@googlegroups.com, Mary Ellen

Thanks again Dennis, because there are about four other graphs I used, I ended up making the changes at the read.csv at the very beginning but it worked out well and I was actually able to eliminate some code in the beginning.

This isn't needed for my visualization work, but just a side question: what ggplot2 function would allow me to organize the bars in each subject from smallest to largest?

Thanks- Mary Ellen
Reply all
Reply to author
Forward
0 new messages