I am completely new to ggplot (and to some extent R). I have been blown away with the quality of graphs that can be created using ggplot, and I am trying to learn how to create a simple multi line plot using ggplot.
Unfortunately, I haven’t found any tutorials that help me get close to what I am trying to do:
I have a CSV file that contains the following data:
id,f1,f2,f3,f4,f5,f6
30,0.841933670833,0.842101814883,0.842759547545,1.88961562347,1.99808377527,0.841933670833
40,1.47207692205,1.48713866811,1.48717177671,1.48729643008,1.48743226992,1.48713866811
50,0.823895293045,0.900091982861,0.900710334491,0.901274168324,0.901413662472,0.901413662472
I would like to plot:
- the first column (id) on the X axis
- each subsequent ‘column’ as a line plot, with smoothing between the points of the line to create a nice smooth line
- A legend for f1, f2 ….
- Specify a line colour and add marks (e.g. crosses i.e. ‘+’) to the line plot for column f2 (for example).
I am really new to ggplot, so have really not got beyond reading the file into R.
Any help in getting me create the plot as describe above, will be very educational and help reduce the ggplot learning curve.
from here I would use
melt. Read?melt.data.framefor more info. But in one sentence, this takes data from a “wide” format to a “long” format.then plot however you’d like:
Where
aesdefines the aesthetics such as the x value, y value, color/colour, etc. Then you add “layers”. in the previous example I’ve added a line for what I defined in theggplot()portion withgeom_line()and added a point withgeom_pointwhere I only put them on thef2variable.below, I added a smoothed line with
geom_smooth(). See the documentation for a bit more info on what this is doing,?geom_smooth.or shapes for all. Here I put shape in the aesthetics of
ggplot(). By putting them here they apply to all successive layers rather than having to specify them each time. However, I can overwrite the values supplied inggplot()in any later layer:However, a bit of
ggplotunderstanding just takes time. Work through some of the examples given in the documentation and on theggplot2website. If your experience is anything like mine, after fighting with it for a few days or weeks it will eventually click. Regarding the data, if you assign your data todat, the code will not change.dat <- read.csv(...). I don’t usedataas a variable because it is a built in function.