Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting bar graphs in R using ggplot2

Tags:

r

ggplot2

I have a data frame called toplot_noind as shown below.

> toplot_noind

    Chang.1  Chang.2  Chang.3  Chang.4  Chang.5  Chang.6  Chang.7  Chang.8
18    150.3     84.61     31.45     11.08     -0.19    -57.83    -88.63    -98.39

I would want to use this data frame to plot a bar graph using ggplot2.

I do not need the column names: Chang.1, Chang.2, etc., in the graph.

I want these 8 values 150.3, 84.61, ..., -98.39 to appear on the y-axis (18 is not part of the values, it is the name of the row).

Also since there are 8 values, I want 8 bars on the x-axis - each pointing to each of these values.

Therefore, I will want to name these bars as 1(for the 1st bar), 2(for the 2nd bar), 3, ..., 8 in that order, on the x-axis.

Also, I would want to label the y-axis as “Total Expected Profit” and the x-axis as “premium change”.

Shown below is what I have tried to do but it does not work. Actually I have tried reading on the ggplot2 but the material I read could not give me solid understanding but I need to use this bar graph in my assignment. I have very limited time to submit.

library(reshape)
library(ggplot2)

t<-ncol(toplot_noind)

a<-seq(1:t)

ggplot(toplot_noind, aes(x = a, y = toplot_noind, xlab="premium change", ylab="Total Expected Profit")) + 
 geom_bar(position = "dodge")

Many thanks in advance to all those who could be of help to me.

Isaac

like image 942
Son Avatar asked May 30 '11 10:05

Son


1 Answers

Do you mean something like this? (I'm not sure whether you want to add value on top of the graph, so I added them, but you can safely remove the last line if you don't need them.)

tmp <- c(150.3,84.61,31.45,11.08,-0.19,-57.83,-88.63,-98.39)
dd <- data.frame(y=tmp, x=LETTERS[1:8])
ggplot(dd, aes(x=x, y=y)) + geom_bar(fill="darkgrey") + 
  labs(x="Premium change", y="Total Expected Profit") + 
  geom_text(aes(x=x, y=ifelse(y>0, y+5, y-5), 
            label=y), size=4, colour="white")

enter image description here

It would be even nicer to add + coord_flip(), IMO.


What's wrong in your code?

  1. The ggplot() function is expecting a data.frame, from which it can extract named variable, e.g. for the aesthetics parameters x= and y=. So, first of all, you need to convert your object into a proper data.frame and name it, for you can its value through aes():

    toplot_noind <- as.data.frame(toplot_noind)
    names(toplot_noind) <- y
    

    which is better that using the same name as your data.frame. (Note, however, that it will inherit its name with the cast operation.)

  2. Then, the x- and y-labels must be outside the aes() function. I don't use qplot() but I think using xlab= and ylab= works fine there. With ggplot, I prefer the labs() or xlab()/ylab() functions. E.g.

  3. You need to have x represented as a factor.

  4. The dodge aspect does not seem necessary here, because you don't have a second cross-classifying factor (see an example of use in help(position_dodge)).

In sum, your corrected code would look like:

toplot_noind <- as.data.frame(toplot_noind)
ggplot(toplot_noind, aes(x = as.factor(1:8), y = toplot_noind)) + 
  geom_bar() +
  xlab("premium change") + 
  ylab("Total Expected Profit") 
like image 94
chl Avatar answered Oct 31 '22 02:10

chl