Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot with data frame columns

I am totally lost with using ggplot. I've tried with various solutions, but none were successful. Using numbers below, I want to create a line graph where the three lines, each representing df$c, df$d, and df$e, the x-axis representing df$a, and the y-axis representing the cumulative probability where 95=100%.

          a     b                    c                    d             e
1         0    18          0.047368421          0.036842105   0.005263158
2         1    20          0.047368421          0.036842105   0.010526316
13        2    26          0.052631579          0.031578947   0.026315789
20        3    35          0.084210526          0.036842105   0.031578947
22        4    41          0.068421053          0.052631579   0.047368421
24        5    88          0.131578947          0.068421053   0.131578947
26        7    90          0.131578947          0.068421053   0.136842105
27        8    93          0.126315789          0.068421053   0.147368421
28        9    96          0.126315789          0.073684211   0.152631579
3        10   115          0.105263158          0.078947368   0.210526316
4        11   116          0.105263158          0.084210526   0.210526316
5        12   120          0.094736842          0.084210526   0.226315789
6        13   128          0.105263158          0.073684211   0.247368421
7        14   129          0.100000000          0.073684211   0.252631579
8        15   154          0.031578947          0.042105263   0.368421053
9        16   155          0.031578947          0.036842105   0.373684211
10       17   158          0.036842105          0.036842105   0.378947368
11       18   161          0.036842105          0.031578947   0.389473684
12       19   163          0.026315789          0.031578947   0.400000000
14       20   169          0.026315789          0.021052632   0.421052632
15       21   171          0.015789474          0.021052632   0.431578947
16       22   174          0.010526316          0.021052632   0.442105263
17       24   176          0.010526316          0.021052632   0.447368421
18       25   186          0.005263158          0.005263158   0.484210526
19       26   187          0.005263158          0.000000000   0.489473684
21       35   188          0.005263158          0.005263158   0.489473684
23       40   189          0.005263158          0.000000000   0.494736842
25       60   190          0.000000000          0.000000000   0.500000000

I was somewhat successful with using R base coding

plot(df$a, df$c, type="l",col="red")
lines(df$a, df$d, col="green")
lines(df$a, df$e, col="blue")
like image 634
Provisional.Modulation Avatar asked Jan 17 '15 09:01

Provisional.Modulation


1 Answers

You first need to melt your data so that you have one column that designates from which variables the data comes from (call it variable) and another column that lists actual value (call it value). Study the example below to fully understand what happens to the variables from the original data.frame you want to keep constant.

library(reshape2)
xymelt <- melt(xy, id.vars = "a")

library(ggplot2)
ggplot(xymelt, aes(x = a, y = value, color = variable)) +
  theme_bw() +
  geom_line()

ggplot(xymelt, aes(x = a, y = value)) +
  theme_bw() +
  geom_line() +
  facet_wrap(~ variable)

This code is also drawing column from your data called "d". You can remove it prior to melting, after melting, prior to plotting... or plot it.

enter image description here

like image 126
Roman Luštrik Avatar answered Sep 25 '22 03:09

Roman Luštrik