r - ploting a line graph in using ggplot or dygraph having matrix as input -


i have matrix of 6 columns , 5 rows. first column week index , rest percentage changes. may this:

i want create aesthetically pleasing line graphs in r using either ggplot or dygraph labelled axis , colored lines (for 2nd 6th column)

any extended appreciated.

your request of "aesthetically pleasing" graph little vague, here how produce labelled , colourful plot using ggplot2.

first simulate data fit format describe:

set.seed(2015) df = data.frame(week = 1:5,                  pct1 = sample(1:100, 5),                  pct2 = sample(1:100, 5),                  pct3 = sample(1:100, 5),                  pct4 = sample(1:100, 5),                  pct5 = sample(1:100, 5))  df   week pct1 pct2 pct3 pct4 pct5 1    1    7   36   71   89   70 2    2   84   50   39   27   41 3    3   30    8    4    8   21 4    4    4   64   40   79   65 5    5   14   99   72   37   71 

to produce desired plot ggplot2, should convert data "long" format. use function gather package tidyr (you use equivalent melt function package reshape2).

library(tidyr) library(ggplot2)  # gather data.frame long format df_gathered = gather(df, pct_type, pct_values, pct1:pct5)  head(df_gathered)   week pct_type pct_values 1    1     pct1          7 2    2     pct1         84 3    3     pct1         30 4    4     pct1          4 5    5     pct1         14 6    1     pct2         36 

now can produce plot colouring using pct_type variable.

ggplot(data = df_gathered, aes(x = week, y = pct_values, colour = pct_type)) +    geom_point() + geom_line(size = 1) + xlab("week index") + ylab("whatever (%)") 

ggplot2_example

note: in case variable week factor (i assumed number, referred "week index"), need tell ggplot group data pct_type, so:

ggplot(data = df_gathered, aes(x = week, y = pct_values,                                 colour = pct_type, group = pct_type)) +    geom_point() + geom_line(size = 1) + xlab("week index") + ylab("whatever (%)") 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -