15

I am trying to create a circular plot and am stuck at a point:

dat1 <- data.frame (xvar = 1:10, y = 6, ymin = 4, ymax = 4.5)

Using this data, I can produce a circular ribbon plot in ggplot2

require(ggplot2) 
 ggplot(dat1, aes(x=xvar, y=y)) +  geom_ribbon(aes(ymin=ymin, ymax=ymax),
  col = "blue", fill = "blue2") + ylim (c(0,6)) + coord_polar()

However I want more.

I want to fill the segment of the ribbon with different colors and labels using the following data.

 filld <- data.frame (start = c(1, 4, 6, 7.5, 8, 9), end = c(4, 6, 7.5, 8, 9, 10),
                         label = c("A", "B", "C", "A", "C", "D"))
  filld
##    start  end label
## 1   1.0  4.0     A
## 2   4.0  6.0     B
## 3   6.0  7.5     C
## 4   7.5  8.0     A
## 5   8.0  9.0     C
## 6   9.0 10.0     D

The ribbon will be filled with different color by label variable. For example, the segment A will start from 1 and end at 4. Then segment B will start and end at 6 and filled with different color. Segments with same label (such as A and C) will be connected by line.

The resulting plot will look like this:

1
  • Nice graphs :) It's great to add anti-aliasing Commented Jul 25, 2012 at 2:29

3 Answers 3

19

Here is an example:

filld$p <- rowMeans(subset(filld, select = c(start, end)))
ggplot(filld, aes(xmin = start, xmax = end, ymin = 4, ymax = 5, fill = label)) + 
  geom_rect() + 
  geom_segment(data = subset(filld, label %in% label[duplicated(label)]),
               aes(x = p, y = 0, xend = p, yend = 4, colour = label),
               size = 2, show_guide = FALSE) +
  geom_text(aes(x = p, y = 4.5, label = label), colour = "white", size = 10) +
  coord_polar() + 
  scale_y_continuous(limits = c(0, 5))

Updated

I do not recommend but something like this:

filld <- data.frame (start = c(1, 4, 6, 7.5, 8, 9), end = c(4, 6, 7.5, 8, 9, 10),
                     label = c("A", "B", "C", "A", "C", "D"))
filld$p <- rowMeans(subset(filld, select = c(start, end)))
filld <- merge(filld, ddply(filld, .(label), summarize, p2 = mean(p)))

lnd <- subset(filld, label %in% label[duplicated(label)])
lnd <- ddply(lnd, .(label), function(x) {
  x <- seq(x$p[1], x$p[2], length = 100)
  y <- 4.5 + ((x - mean(x))^2 - (x[1]-mean(x))^2) / (x[1]-mean(x))^2 * 3 + sin(x*3*pi) * 0.1
  data.frame(x, y)
})


p <- ggplot(filld, aes(xmin = start, xmax = end, ymin = 4, ymax = 5, colour = label, fill = label)) + 
  geom_line(aes(x, y, xmin = NULL, ymin = NULL, xmax = NULL, ymax = NULL), data = lnd, size = 2) +
  geom_rect() + 
  geom_text(aes(x = p, y = 4.5, label = label), colour = "white", size = 10) +
  coord_polar() + 
  scale_y_continuous(limits = c(0, 5))
p

Perhaps, what you want is beyond the scope of ggplot2.

Sign up to request clarification or add additional context in comments.

5 Comments

@kohske pretty nice ...lines are much better, but I wonder if it is possible make lines that are lower than 180 to making smaller loop that to connect a lines via center to avoid crowding
I was thinking of p + annotation_custom(gridExtra::arcTextGrob()), but it's incompatible with polar coordinates, sadly. Maybe it should only be a warning.
there could be a geom_arc with associated stat, which would work like geom_segment, but never munched, and would connect pairs of observations with a curveGrob
Cool example, reminds me of circos diagrams. My command of geometry isn't strong enough though to give suggestions about how to map such arcs in polar coordinates. Here is another example in d3.js as well. Edit - I see Paolo's answer gives a very similar example as well.
Unfortunately circos is perl and no similar quality r package I could find in R.
8

Take a look at ggbio. This package extends ggplot2 and the grammar of graphics to sequence data (bioinformatics) but they seems to solve your problem (for example see here). Taking a look at the source code for the package may direct you to a more generic solution.

Comments

6

Add the ymin etc to fill d

fd <- data.frame(filld, ymin = 4, y =6, ymax = 4.5)

Then use geom_rect with the column label as the fill and colour aesthetics

 ggplot(fd, aes(x=start,xmin = start, xmax = end, y=y)) +  
   geom_rect(aes(ymin=ymin, ymax=ymax, fill = label )) + 
   ylim (c(0,6)) + 
   coord_polar() 

Add the lines:

## calculate the midpoint for each segment
fd$xmid <- apply(fd[,1:2],1,mean)
## get the replicate id for the labels (what occurence is this)
library(plyr)
library(reshape2)
fd1 <- ddply(fd, .(label), mutate, id = 1:length(xmid))
## reshape to wide, subsetting only with more than one rep
.lines <- na.omit(dcast(fd1, label~id, value.var = 'xmid'))
## add a mid point between the mid points
.lines$mid <- apply(.lines[,2:3],1,mean)
## reshape to long, and add some y values 
ld <- data.frame(arrange(melt(.lines,value.name = 'x'), label, x), y = rep(c(4,3,4),2))

ggplot(fd) +  
  geom_rect(aes(x=start,xmin = start, xmax = end, y=y,ymin=ymin, ymax=ymax, fill = label )) + 
  ylim (c(0,6)) + 
  coord_polar() + geom_path(data = ld, aes(x=x,y=y,colour = label))

The lines are ugly, but there!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.