0
votes
$\begingroup$

I have a table I want to convert into a graph (bar-graph or line-graph)

The first column has fixed values. Twenty different values are simulated for these fixed values and kept in the next columns. I want to plot a graph of the fixed column against all the different twenty simulated columns.

How do I go about it?

Edit

table name:bygrace

V1    V2   V3   V4   V5                 
100    16   11   -6    1                               
120   -17  -12    7   -2                               
140    18   13   -8    3                              
150   -19  -14    9   -4                              
210    20   15  -10   -5

Actually, my table looks like the one above. The first column V1 represents Premiums charged by an insurance company,say last year (for 5 policyholders/policies) After critical analysis of the portfolio, the company decides to increase or decrease the premium amount for some of the customers. However, the company does not know exactly by how much it should increase of decrease the premiums for fear that it would lose customers. For this reason, four different scenarios/simulations are considered which are represented by the next four columns(V2 through V5) respectively.

Now the task is to plot the premium amounts in V1 against these four different scenarios(bar-graph or line-graph; I think bar will be better).

And my question is can this be done on one graph/at once? If yes, how shoul I go about it? Or do I have to plot the premium against each column separately?

In fact, I have spent the whole of yesterday and today on this but I have not been able to get the desired result.

Someone gave me an answer to try.And I am going to do that because it has given me an idea and I am very thankful to the one!

Many thanks to everyone for their help

$\endgroup$
  • 7
    $\begingroup$ Your question is pretty vague and as it stands is more of a request for an introduction to plotting with R and less of a specific problem. If you provide sample data either by making it up or by using dput() to paste the contents of your actual data into the question - people can provide specific tips. Otherwise, this tutorial addresses lines and bar charts well. $\endgroup$ – Chase Apr 26 '11 at 20:24
  • $\begingroup$ This a duplicate of your preceding question. Please consider updating the former one instead (and delete this one afterwards). And don't forget to register your account, as indicated on the FAQ. Finally, I removed the graph-theory tag in your preceding post because it really has nothing to do with graph theory. $\endgroup$ – chl Apr 27 '11 at 19:58
  • $\begingroup$ Some data seems to be in the later almost duplicate question at stats.stackexchange.com/questions/10055/… $\endgroup$ – Henry Apr 27 '11 at 21:48
  • 1
    $\begingroup$ This question has been merged with its duplicate. The new text is now marked by the "Edit". $\endgroup$ – whuber Apr 27 '11 at 22:11
5
votes
$\begingroup$

Here's a few options using the ggplot2 package. If you haven't started learning much about plotting with R, taking the time now to learn ggplot2 or lattice may be worth the effort.

ggplot requires data to be in "long" format, so the first thing we'll do is reformat to long:

require(ggplot2)

dat <- data.frame(V1 = c(100, 120, 140, 150, 210), V2 = c(16, -17, 18, -19, 20)
, V3 = c(11, -12, 13, -14, 15), V4 = c(-6, 7, -8, 9, -10), V5 = c(1, -2, 3, -4, 5))

dat.m <- melt(dat, "V1")

It sounds like V1 represents discrete levels, and should be treated as categorical data. That is why I treat V1 as a factor. If that's not the case, simply use V1 in the x-axis.

#Bar chart
ggplot(dat.m, aes(x = factor(V1), y = value, fill = variable)) + 
geom_bar(position = "dodge")

#Line chart
ggplot(dat.m, aes(x = factor(V1), y = value, group = variable, colour = variable)) + 
geom_line()

enter image description here

$\endgroup$
  • $\begingroup$ +1, could also look at last_plot() + facet_grid(. ~ variable) to isolate each scenario [depending on exactly what the OP is trying to visualize] $\endgroup$ – david w Apr 28 '11 at 7:51
  • $\begingroup$ Hello David, many thanks for your help. I tried it out and it worked. however if I remove the factor(ie if I make it x=V1) it does not work! $\endgroup$ – Son Apr 29 '11 at 16:40
1
vote
$\begingroup$

Try this:

#Generate 100 x values.  Then generate 20 random walk y values for each x value
x <- seq(1, 100, 1)  
y <- matrix(20*100, nrow=100, ncol=20)  
for (i in 1:20) {  
  y[, i] <- cumsum(rnorm(100))  
}  

#Build the table  
df <- data.frame(x=x, y=y)  
head(df)  

#Plot the table  
matplot(df[, 1], df[, 2:21],  type="l", main="Twenty Random Walks", xlab="x", ylab="y")    
grid()  

enter image description here

$\endgroup$

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