17
$\begingroup$

We may assume that we have CSV file and we want a very basic line plot with several lines on one plot and a simple legend.

$\endgroup$
  • 2
    $\begingroup$ How do you define 'publication-quality'? Please elaborate on what aspects you like to see covered... e.g. color use, line widths, etc. Should answers focus on font size, instead? $\endgroup$ – Egon Willighagen Jul 20 '10 at 10:51
14
$\begingroup$

The easiest way is to use R

Use read.csv to enter the data into R, then use a combination of the plot and line commands

If you want something really special, then look at the libraries ggplot2 or lattice.

In ggplot2 the following commands should get you started.

require(ggplot2)
#You would use read.csv here
N = 10
d = data.frame(x=1:N,y1=runif(N),y2=rnorm(N), y3 = rnorm(N, 0.5))
p = ggplot(d)

p = p+geom_line(aes(x, y1, colour="Type 1"))
p = p+geom_line(aes(x, y2, colour="Type 2"))
p = p+geom_line(aes(x, y3, colour="Type 3"))
#Add points
p = p+geom_point(aes(x, y3, colour="Type 3"))
print(p)   

This would give you the following plot:

Line plot http://img84.imageshack.us/img84/6393/tmpq.jpg

Saving plots in R

Saving plots in R is straightforward:

#Look at ?jpeg to other different saving options
jpeg("figure.jpg")
print(p)#for ggplot2 graphics
dev.off()

Instead of jpeg's you can also save as a pdf or postscript file:

#This example uses R base graphics
#Just change to print(p) for ggplot2
pdf("figure.pdf")
plot(d$x,y1, type="l")
lines(d$x, y2)
dev.off()
$\endgroup$
  • $\begingroup$ How about saving to file? $\endgroup$ – Łukasz Lew Jul 20 '10 at 10:19
  • 1
    $\begingroup$ Or a little more succinctly with melt and qplot: m <- melt(d, id = "x"); qplot(variable, value, data = m, colour = variable) $\endgroup$ – hadley Jul 21 '10 at 12:39
  • $\begingroup$ Actually, an even easier way is to use R+deducer with ggplot2 (there is a new release of this which is about to come out in the next few months. A beta is currently available) $\endgroup$ – Tal Galili Jul 22 '10 at 3:57
  • 4
    $\begingroup$ Nice example, but the plot is hardly publication quality. Or at least none of the journals I publish in would accept it. $\endgroup$ – Matti Pastell Jul 22 '10 at 11:02
  • 3
    $\begingroup$ "Hardly publication quality"???? I realise that it isn't perfect - the phrase "...should you get you started.." covers that bit. But with a little additional work, i.e. axis labels, I would say it's fine. BTW, what journals do you publish in? $\endgroup$ – csgillespie Jul 26 '10 at 21:20
13
$\begingroup$

It's hard to go past R for graphics. You could do what you want in 3 lines. For example, assuming the csv file has four columns:

x <- read.csv("file.csv")
matplot(x[,1],x[,2:4],type="l",col=1:3)
legend("topleft",legend=c("A","B","C"),lty=1,col=1:3)
$\endgroup$
  • $\begingroup$ How about putting it in publication? $\endgroup$ – Łukasz Lew Jul 20 '10 at 9:59
  • 5
    $\begingroup$ R produces some of the best quality graphics around. As an editor of an international research journal, I would love all our authors to use R. $\endgroup$ – Rob Hyndman Jul 20 '10 at 10:02
  • 1
    $\begingroup$ .. see my comment on the question... how do you define 'publication-quality', or 'best quality'... from a editor perspective? $\endgroup$ – Egon Willighagen Jul 20 '10 at 10:53
  • 19
    $\begingroup$ I like to see vector graphics (no jpegs), graphical design following the principles of Tufte & Cleveland, readable fonts, uncluttered legends, no shaded backgrounds, sensible axis limits and tick intervals, labelled axes, no overlap of text and plotting characters or lines, etc. Most authors use the default settings of their software, so good software has good defaults. This is where Excel fails miserably and R does pretty well. But it is possible to produce lousy graphs in R and good graphs in Excel. It's just easier to produce high quality graphics in R. $\endgroup$ – Rob Hyndman Jul 20 '10 at 11:07
12
$\begingroup$

R is definitely the answer. I would just add to what Rob and Colin already said:

To improve the quality of your plots, you should consider using the Cairo package for the output device. That will greatly improve the quality of the final graphics. You simply call the function before plotting and it redirects to Cairo as the output device.

Cairo(600, 600, file="plot.png", type="png", bg="white")
plot(rnorm(4000),rnorm(4000),col="#ff000018",pch=19,cex=2) # semi-transparent red
dev.off() # creates a file "plot.png" with the above plot

Lastly, in terms of putting it in a publication, that's the role that Sweave plays. It makes combining plots with your paper a trivial operation (and has the added benefit of leaving you with something that is reproducible and understandable). Use cacheSweave if you have long-running computations.

$\endgroup$
  • 2
    $\begingroup$ Probably better than using Cairo to produce a bitmap image is to save graphs as vectors, which are resolution independent, so look better when resized, and always suit print resolution - I like svg, which can easily be edited with inkscape. $\endgroup$ – naught101 Apr 20 '12 at 7:11
10
$\begingroup$

My favorite tool is Python with mathplotlib

The advantages:

  • Immediate export from the environment where I do my experiments in
  • Support for the scipy/numpy data structures
  • Familiar syntax/options (matlab background)
  • Full latex support for labels/legends etc. So same typesetting as in the rest of your document!

Specifically, for different file formats like svg and eps, use the format parameter of savefig

An example: input.csv

"Line 1",0.5,0.8,1.0,0.9,0.9
"Line 2",0.2,0.7,1.2,1.1,1.1

Code:

import csv
import matplotlib.pyplot as plt

legends = []
for row in csv.reader(open('input.csv')):
    legends.append(row[0])
    plt.plot(row[1:])

plt.legend(legends)
plt.savefig("out.svg", format='svg')
$\endgroup$
  • $\begingroup$ Could you put a code snippet for completness? It would be very useful for people in the future finding this page. $\endgroup$ – Łukasz Lew Jul 20 '10 at 10:19
  • $\begingroup$ @Łukasz Hmm, some suggestion how to upload an svg figure? $\endgroup$ – Peter Smit Jul 20 '10 at 10:52
  • 4
    $\begingroup$ You could have mentioned in your answer that matplotlib allows rendering of all typography in the plot with LaTeX so it perfectly integrates visually. $\endgroup$ – Benjamin Bannier Jul 20 '10 at 12:49
  • $\begingroup$ I wonder why matplotlib hasn't been integrated into R yet? $\endgroup$ – naught101 Apr 20 '12 at 7:13
6
$\begingroup$

Take a look at the sample galleries for three popular visualization libraries:

For the first two, you can even view the associated source code -- the simple stuff is simple, not many lines of code. The prefuse case will have the requisite Java boilerplate code. All three support a number of backends/devices/renderers (pdf, ps, png, etc). All three are clearly capable of high quality graphics.

I think it pretty much boils down to which language are you most comfortable working in. Go with that.

$\endgroup$
5
$\begingroup$

Another option is Gnuplot

$\endgroup$
1
$\begingroup$

Easy is relative. No tool is easy until you know how to use it. Some tools may appear more difficult at first, but provide you with much more fine-grained control once you master them.

I have recently started to make my plots in pgfplots. Being a LaTeX package (on top of tikz), it is particularly good at making things look good. Fonts will be consistent with the rest of the document and it's much easier to integrate your plots visually. It's not the easiest option to make plots, but it's a rather easy way to make plots that are certainly publication-quality.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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