-1

I have the following data in .csv file:

Name  marks1 marks2    
xy      10    30
yz      20    40
zx      30    40
vx      20    20
vt      10    20

How do I draw a graph with both marks1 and marks2 in y-axis and name in x-axis?

y <- cbind(data$marks1,data$marks2)
x <- cbind(data$Name)
matplot(x,y,type="p")

I need a bar plot and due to high amount of x axis data i need to allign them properly and also place the number

For example graph should be like

marks 1 marks 2 in two different colors in same bar and marks written on them

#read csv file
 aau <- read.csv("",head=TRUE,sep=",")

#convert into matrix format
 aaumatrix <- as.matrix(aau)

#create barplot
 barplot(aaumatrix)


#check for more attributes of barplot
 ?barplot
mnel
  • 113,303
  • 27
  • 265
  • 254
Illuminati
  • 555
  • 2
  • 7
  • 34

1 Answers1

1

The description of what you want is a little confusing, but this is my best interpretation.

dat <- read.table(textConnection("Name  marks1 marks2                                                                                  
xy      10    30                                                                                                                       
yz      20    40                                                                                                                       
zx      30    40                                                                                                                       
vx      20    20                                                                                                                       
vt      10    20"), header = TRUE)

library(ggplot2)
library(reshape)

datm <- melt(dat, id.vars = "Name")

datlab <- dat
datlab$y <- datlab$marks1
datlab <- melt(datlab, id.vars = c("Name", "y"))

## set y value to be in the center of the respective bar 
datlab$y <- ifelse(datlab$variable == "marks1", datlab$y / 2, datlab$y + datlab$value / 2)

p <- ggplot() + geom_bar(data = datm, aes(value, x = Name, fill = variable))
p + geom_text(data = datlab, aes(x = Name, y = y, label = value))

Edit: Another option

datlab <- dat
datlab$y <- rowSums(datlab[c("marks1", "marks2")])
datlab <- melt(datlab, id.vars = c("Name", "y"))
## set y value to be in the center of the respective bar                                                                               
datlab$y <- ifelse(datlab$variable == "marks1", datlab$value / datlab$y / 2, 1 - datlab$value / datlab$y / 2)

p <- ggplot() + geom_bar(data = datm, aes(x = Name, fill = variable, y = value), position = "fill")
p + geom_text(data = datlab, aes(x = Name, y = y, label = value))

Option 1:

enter image description here Option 2:

enter image description here

Jake Burkhead
  • 6,435
  • 2
  • 21
  • 32
  • Sorry for the confusion I need bar plots in which one color of the bar should represent marks1 and other color of the bar should represent marks2 – Illuminati Jul 09 '13 at 04:33
  • I have huge x axis data so the names should fit in exactly – Illuminati Jul 09 '13 at 04:33
  • @user2540455 still not getting what you want. do you have an example of what such a plot would look like? would all of this data end up on one bar where color distinguishes between `marks1` and `marks2` and shading (as in the barplot example) represents the different levels of Names? – Jake Burkhead Jul 09 '13 at 04:37
  • http://www.ImageFolks.com/img-51db94d2285ee.html in this graph model there are two colors white and black in same bar i need exactly the same and also i need the x axis labels to be alligned with the bars and also the marks to be displayed on the bars – Illuminati Jul 09 '13 at 04:43
  • http://www.ImageFolks.com/img-51dcd829611a5.html sorry for the delayed reply this is the way i am getting when i am using the code given above any help would help a lot – Illuminati Jul 10 '13 at 03:42
  • @user2540455 its hard to say without seeing your code. you could add a link to your data along with the exact code that you are currently using. The first think I notice is that the data isn't mapped correctly and it looks like its probably due to a mistake with using `melt` but its hard to say. I would recommend reading a tutorial on ggplot. Its a very worthwhile package to understand. These look good: http://www.noamross.net/blog/2012/10/5/ggplot-introduction.html, http://www.ling.upenn.edu/~joseff/rstudy/summer2010_ggplot2_intro.html. Skips right past qplot which, IMO, is a good thing – Jake Burkhead Jul 10 '13 at 04:15
  • @user2540455 there are plenty of other questions posted to help you with changing the axis labels. I suggest searching for and reading those. Here's one I found http://stackoverflow.com/questions/1330989/rotating-and-spacing-axis-labels-in-ggplot2, you can also find really good documentation on ggplot at http://docs.ggplot2.org/current/index.html, `scale_y_continuous` should help you change labels on the y axis – Jake Burkhead Jul 10 '13 at 12:13