[Tex/LaTex] Package for drawing RDF graphs

graphstikz-pgf

Is there some package to draw nice RDF
graphs, such as the one below (taken from there)?

Can such a graph (with the same style) be produced in TikZ?

enter image description here

Best Answer

As first think I would start to define the style able to represent the vertices; we basically need:

  1. an ellipse;
  2. some size definitions;
  3. drawing the border thicker and setting the fill color brighter with respect to the border;
  4. making the text in white color;
  5. perhaps reduce a bit the size of the text font.

The style definition in TikZ can be done via \tikzset:

\tikzset{vertex style/.style={
    draw=#1,
    thick,
    fill=#1!70,
    text=white,
    ellipse,
    minimum width=2cm,
    minimum height=0.75cm,
    font=\small,
    outer sep=3pt, % the usage of this option will be clear later on
  },
}

Notice two things: this style receives an argument, the color and sets the fill color brighter by mixing the color with white. Secondly, the ellipse shape requires the library:

\usetikzlibrary{shapes.geometric}

Let's now create the first vertex. I would start with the "Righteous Kill" which has most connections departing from.

\documentclass[dvipsnames,png,border=10pt,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric} % required for the ellipse shape
\tikzset{vertex style/.style={
    draw=#1,
    thick,
    fill=#1!70,
    text=white,
    ellipse,
    minimum width=2cm,
    minimum height=0.75cm,
    font=\small,
    outer sep=3pt,
  },
}
\begin{document}
\begin{tikzpicture}
\node[vertex style=Turquoise] (Rk) {Righteous Kill};
\end{tikzpicture}
\end{document}

Beware of the option dvipsnames: it has been necessary to set this within the class options to prevent a possible option clash error since TikZ loads itself xcolor.

The node uses the style defined above setting its color (definition in the xcolor manual), its name (Rk) and its text {Righteous Kill}.

The result:

enter image description here

From now on, I won't display any time the preamble, but rather just the picture code.

Second step is to locate the other nodes. One might exploit lots of possibilities (using GraphViz, using the object-oriented module of PGF and build some custom class - see: Drawing relationships between elements of a database): here I use the positioning library of TikZ. Each vertex, therefore, will be located starting from the position of other vertices by referring to their names.

Notice: since each vertex needs to be connected to another one, the syntax

\node[options] (a) {text a} ... edge[options] node[options]{text conn} (b);

will connect the vertices a and b setting a label to the connection text conn. Extremely useful in this case. Indeed, let's start to add another node:

\begin{tikzpicture}[node distance=2.75cm,>=stealth']
\node[vertex style=Turquoise] (Rk) {Righteous Kill};

\node[vertex style=BurntOrange, above of=Rk,xshift=2em] (BD) {Bryan Dennehy}
 edge [<-,cyan!60!blue] node[text style,above]{starring} (Rk);
\end{tikzpicture}

The options node distance=2.75cm,>=stealth' concern the basic vertex distance and the type of the arrow tip used for the connection. Due to the arrow tip selected, the library arrows should be loaded.

Notice that the connection text requires a style: text style. Its definition is:

\tikzset{
  text style/.style={
    sloped, % the text will be parallel to the connection 
    text=black,
    font=\footnotesize,
    above
  }
}

The location of the new node is done via the options above of=Rk,xshift=2em: the first one sets the position in relation to the name of the previous node we created before, the second one shifts a bit on the right this position.

Result:

enter image description here

Once understood this mechanism, it is possible to locate all the other nodes:

\begin{tikzpicture}[node distance=2.75cm,>=stealth']
\node[vertex style=Turquoise] (Rk) {Righteous Kill};

\node[vertex style=BurntOrange, above of=Rk,xshift=2em] (BD) {Bryan Dennehy}
 edge [<-,cyan!60!blue] node[text style]{starring} (Rk);

\node[vertex style=BurntOrange, right=1.5cm of Rk,yshift=4ex] (AP) {Al Pacino}
 edge [<-,cyan!60!blue] node[text style]{starring} (Rk); 

\node[vertex style=red, below right of=Rk,xshift=2em] (JA) {John Avnet}
 edge [<-,cyan!60!blue] node[text style]{director} (Rk); 

 \node[vertex style=BurntOrange, right=1.5cm of Rk,yshift=-4ex] (RN) {Robert De Niro}
 edge [<-,cyan!60!blue] node[text style]{starring} (Rk); 

\node[vertex style=MidnightBlue, above right of=Rk,xshift=2em] (Dr) {Drama}
 edge [<-,cyan!60!blue] node[text style]{genre} (Rk); 

\node[vertex style=Maroon, below of=Rk,xshift=-2em] (Skf) {Serial Killer Films}
 edge [<-,cyan!60!blue] node[text style]{subject} (Rk);

\node[vertex style=Maroon, below right of=Skf] (Cf) {Crime Films}
 edge [<-,cyan!60!blue] node[text style]{broader} (Skf);
\end{tikzpicture}

enter image description here

Once this task is done, it is possible to start adding the "liquid" background around some vertices.

To place something in background the library backgrounds is of help, as well as the hobby library by Andrew Stacey to draw the smooth curve. Moreover, often some computation are needed, therefore also the library calc should be loaded.

The path definition should be done as follows: starting from the north of the node (Rk), one circumnavigates all possible anchors of the interested nodes to be highlighted. There are some automatic tools, see Hobby path realization in convex hull approach, but they can not be precise and fine tune things as some manual work can do. And, in this case this allows, to achieve a best result.

\begin{pgfonlayer}{background}
\draw[Maroon,fill=Maroon,dashed,fill opacity=0.1](Rk.north) 
to[closed,curve through={(Rk.north west).. (Rk.west) .. (Rk.south west) 
..($(Rk.south west)!0.5!(Skf.north)$) .. (Skf.north     west).. (Skf.west) 
.. (Skf.south west) .. ($(Skf.south)!0.75!(Cf.west)$) .. (Cf.west) 
.. (Cf.south west) .. (Cf.south) .. (Cf.south east) .. (Cf.east) 
.. ($(Cf.north east)!0.65!(Skf.south east)$) .. (Skf.east) 
.. (Skf.north east).. ($(Skf.north)!0.35!(Rk.south east)$) 
.. (Rk.south east) .. (Rk.east)..(Rk.north east)}](Rk.north);
\end{pgfonlayer}

More or less that's the code... but that's the result:

enter image description here

The mysterious option outer sep=3pt allows to not have the "liquid background" too much close to the border shape. And now it's all!

The complete code for reference:

\documentclass[dvipsnames,png,border=10pt,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric} % required for the ellipse shape
\usetikzlibrary{arrows, backgrounds, calc, hobby, positioning}

% this avoides some problem with the hobby implementation
% egreg's code from:
% http://www.guitex.org/home/it/forum/5-tex-e-latex/83195-la-libreria-hobby-tikz-non-funziona-piu#83203
\ExplSyntaxOn
\cs_if_exist:NF \prg_stepwise_function:nnnN { \cs_gset_eq:NN \prg_stepwise_function:nnnN \int_step_function:nnnN }
\cs_if_exist:NF \prg_stepwise_inline:nnnn { \cs_gset_eq:NN \prg_stepwise_inline:nnnn \int_step_inline:nnnn }
\ExplSyntaxOff

\tikzset{vertex style/.style={
    draw=#1,
    thick,
    fill=#1!70,
    text=white,
    ellipse,
    minimum width=2cm,
    minimum height=0.75cm,
    font=\small,
    outer sep=3pt,
  },
  text style/.style={
    sloped,
    text=black,
    font=\footnotesize,
    above
  }
}

\begin{document}
\begin{tikzpicture}[node distance=2.75cm,>=stealth']
\node[vertex style=Turquoise] (Rk) {Righteous Kill};

\node[vertex style=BurntOrange, above of=Rk,xshift=2em] (BD) {Bryan Dennehy}
 edge [<-,cyan!60!blue] node[text style]{starring} (Rk);

\node[vertex style=BurntOrange, right=1.5cm of Rk,yshift=4ex] (AP) {Al Pacino}
 edge [<-,cyan!60!blue] node[text style]{starring} (Rk); 

\node[vertex style=red, below right of=Rk,xshift=2em] (JA) {John Avnet}
 edge [<-,cyan!60!blue] node[text style]{director} (Rk); 

 \node[vertex style=BurntOrange, right=1.5cm of Rk,yshift=-4ex] (RN) {Robert De Niro}
 edge [<-,cyan!60!blue] node[text style]{starring} (Rk); 

\node[vertex style=MidnightBlue, above right of=Rk,xshift=2em] (Dr) {Drama}
 edge [<-,cyan!60!blue] node[text style]{genre} (Rk); 

\node[vertex style=Maroon, below of=Rk,xshift=-2em] (Skf) {Serial Killer Films}
 edge [<-,cyan!60!blue] node[text style]{subject} (Rk);

\node[vertex style=Maroon, below right of=Skf] (Cf) {Crime Films}
 edge [<-,cyan!60!blue] node[text style]{broader} (Skf);

\begin{pgfonlayer}{background}
\draw[Maroon,fill=Maroon,dashed,fill opacity=0.1](Rk.north) 
to[closed,curve through={(Rk.north west).. (Rk.west) .. (Rk.south west) 
..($(Rk.south west)!0.5!(Skf.north)$) .. (Skf.north     west).. (Skf.west) 
.. (Skf.south west) .. ($(Skf.south)!0.75!(Cf.west)$) .. (Cf.west) 
.. (Cf.south west) .. (Cf.south) .. (Cf.south east) .. (Cf.east) 
.. ($(Cf.north east)!0.65!(Skf.south east)$) .. (Skf.east) 
.. (Skf.north east).. ($(Skf.north)!0.35!(Rk.south east)$) 
.. (Rk.south east) .. (Rk.east)..(Rk.north east)}](Rk.north);
\end{pgfonlayer}

\end{tikzpicture}
\end{document}