ggplot2 – much easier with JGR and Deducer

In the last R-User meeting in Cologne, we had a discussion about using ggplot2 – and I gave a short introduction of how to use ggplot2 with JGR and Deducer.

Basically, JGR is a Graphical User Interface for R, and Deducer is a kind of “data analysis plugin”, that also comes with a so-called “plot builder” running ggplot2.

This feature allows users to access the power of ggplot2 through a simple user interface, which makes the start with ggplot2 much easier.

The attached slide pack gives a short introduction (unfortunately only in german), but in order to see Deducer in action, I recommend to have a look at excellent tutorials like

Introductory tour of Deducer

Deducer’s plot builder – Part 1

The slides below


As well as the source code of the slides, in .Rmd format.

Displaying german stock performance with R using ggplot2

I cannot follow stock market developments daily, so I was looking for a quick overview of what had happened in the last week. What would be of interest for me is  “How did German stocks perform over the last 5 days, compared to the last 20 trading days and the last 250 trading days”.

R in combination with the right packages delivers a quick answer.

The result is a picture like this:

Performance Plot

Plot of German stock performance

Values in the “upper right” quadrant stand for shares, that did show positive performance during the last 5 and 20 trading days. The color displays the performance over the last 250 trading days.

We can see that the last week was rather positive, all of the shares except one have positive performance.  Also the last 20 days were profitable for most shares.

However, it can be seen that a lot of stocks did suffer during the last year (i.e. 250 trading days), which van bee seen from the colors – most of them are red. Most notably CBK.DE (Commerzbank) did nearly loose all of its value…

You can find the R code here:


#get list of Symbols for DAX-values
getSymbols(l, from="2010-09-01")
l[1] <- "GDAXI"

# Function to extract "adjusted prices" and build dataframe: Thanks to Zach Mayer of
symbolFrame <- function(symbolList) {
Data <- data.frame(NULL)
for (S in symbolList) {
Data <- cbind(Data,Ad(get(S)))
colnames(Data) <- symbolList


Data <- symbolFrame(l[-1]) # build a dataframe without DAX istelf
Data <- cbind(Ad(GDAXI), Data) # add DAX
colnames(Data)[1] <- "DAX"
tail(Data,2) #just to check - often Yahoo is not up to date and there are NAs in the last row
#Data <- window(Data, start=start(Data), end=end(Data)-1) # code to delete last row...

Return.calculate(Data, method="simple") -> Data.r #calculates the returns (simple)
Data.r[] <- 0 

#builds frames for the respective perfromances on short, mid and long term
mid.perf <-,20)),1)))
short.perf <-,5)),1)))
long.perf <-,250)),1)))

per.df <- data.frame(cbind(t(short.perf), t(mid.perf), t(long.perf)))

colnames(per.df) <- c("short", "mid", "long")
row.names(per.df)[1] <- "DAX"
chart_title <- paste("Performance Comparison DAX values\n(latest data close of ",end(Data),")")
ggplot(data=per.df, aes(short, mid, label=rownames(per.df))) + geom_point(aes(color=long), size=4) +
  geom_text(hjust=0, vjust=0,size=4) + geom_vline(xintercept=0) + geom_hline(yintercept=0) +
  scale_colour_gradient2(low="red", high="green", "250days\nPerformance") +
  scale_y_continuous("Mid Performance: 20days", formatter="percent") +
  scale_x_continuous("Short Performance: 5days", formatter="percent") +
  opts(title = chart_title)

Created by Pretty R at