pwnt.be

Simple Linear Regression with JFreeChart

I’m sure there are numerous ways you can graph a simple linear regression using JFreeChart. Wanting to do so, I turned to Google and came across a few implementations. However, as none of them seemed terribly well written, I took a stab at it myself.

Basically, what I wanted was a scatter plot with the regressed line on top of it. As a bonus, I set out to include support for multiple data sets in one graph. JFreeChart allows you to combine data sets and create overlaid charts, so the whole thing was surprisingly easy. After looking at a few examples, I came up with the following:

Simple linear regression example
Simple linear regression example

Pretty nifty, huh? So, here’s how it’s done …

  • We start by creating a scatter plot from one or more series of data. The ChartFactory.createScatterPlot() method does exactly that, so I won’t elaborate on it here.

  • Next, we add a second data set to the plot. It will be used to render the regressed line and in fact consists of only two points per series: the end points of the line segment that will be drawn.

  • Obviously, JFreeChart already contains the functionality to draw line charts. Hence, all we need to draw the lines is a simple StandardXYItemRenderer. Thus, we create an instance of that class and associate it with the second data set.

  • And finally, some cosmetic surgery. Since JFreeChart does not know the scatter plot and line chart are related, it will paint associated series in a different color. Using setSeriesPaint(), however, we can easily match both renderers’ color schemes.

  • The result is a standard JFreeChart, which can be customized and visualized like any other one.

And that really is all there is to it. The resulting code can be found in RegressionDemo, which I am releasing under a GPL license. Enjoy.

Update: You’ll probably want to get rid of superfluous legend items. To do that, just tell JFreeChart that you want to hide them:

regressionRenderer.setBaseSeriesVisibleInLegend(false);

I took the liberty of updating the screenshot and code to reflect this.

Comments on This Post

  • Says Sascha, roughly 1 year and 6 months ago:

    Hi, I tried to check your RegressionDemo.java, but it seems to be corrupted. Can you check this?

    Regards!

  • Says Tim, roughly 1 year and 6 months ago:

    How so? What error did you get?

  • Says Sascha, roughly 1 year and 6 months ago:

    Hi Tim, no Error I don’t come that far ;) It looks like the .java was transferred in binary mode. I’m able to download the source file, but notepad++ and pspad only show cryptic symbols after opening that file.

  • Says Tim, roughly 1 year and 6 months ago:

    Odd. The only reason I can think of is that text content from this site is gzipped transparently. Your browser should be able to deal with that though. Maybe try a different one?

  • Says Sascha, roughly 1 year and 6 months ago:

    Hey Tim, it seems that our corporate proxy did something with the file. At home it works without any issues. Thanks for your assistence :) Regards

  • Says Nicolas Machado, roughly 5 months and 1 week ago:

    Hi, great, work on first try !! Just what I need !! :D

Post a Comment

This contraption supports Gravatar, as well as Markdown with SmartyPants. If none of that made sense to you, feel free to ignore it and start typing.

  • :)
  • :D
  • ;)
  • :-O
  • :P
  • :@
  • :$
  • :S
  • :(
  • :'(
  • :|
  • :-#
  • 8-|
  • ^o)
  • :-*
  • +o(
  • :^)
  • *-)
  • 8-)
  • |-)
Disorientation
Continuity
Tangentiality
Retributions
Koop eens een Nokia Lumia 800
Samuel Debruyn
Bizar Hairdressing & Beyond
Hanne, Hanne, Ruxi, Wim, Tim, Sarina, Lies, Lynn, erwin, Ano, Frederick, Jacqueline, Wazaaa, Tim, Rebecca, Charlie
Lplayer for the Rest of Us
fieryy-AA, jesus2099, Tim, jesus2099, Tim, jesus2099, Tim, PixelPirate
Automating OpenVPN Connection on Windows XP
blanky, sky, Tim, Geb, 12vpn, Tim, neecom
Simple Linear Regression with JFreeChart
Nicolas Machado, Sascha, Tim, Sascha, Tim, Sascha
Colophonics