Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 geom_line() should point at specified value

Tags:

r

ggplot2

I have written the following code:

library(ggplot2)

data <- structure(list(x = c(1L, 6L, 3L, 4L, 2L, 3L, 6L, 1L, 5L, 2L, 
                    1L, 5L), y = c(1L, 7L, 5L, 6L, 3L, 4L, 6L, 2L, 5L, 6L, 5L, 2L
            ), year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                            2L, 2L), .Label = c("2010", "2011"), class = "factor"), matching = structure(c(1L, 
                            2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("person1", 
                            "person2", "person3", "person4", "person5", "person6"), class = "factor")), .Names = c("x", 
            "y", "year", "matching"), row.names = c(NA, -12L), class = "data.frame")

data$year <- factor(data$year)

colors <- c("#4cb5ee", "#a0d099", "red")

p <- ggplot(data, aes(x=x, y=y)) +
    geom_point(aes(colour=year), shape=16, size=6) +
    geom_line(aes(group=matching), arrow=arrow(length=unit(0.15,"cm")), colour="black", size=1) +
    xlab("x") + ylab("y") +
    scale_colour_manual("year", values=colors) +
    scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
    scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))

print(p)

It gives the following output: plot

But what I want geom_line() to do is: always points at the point where year=2011. I can't figure out why the arrow of the line is point sometimes at a point which refers to year=2010 and sometimes points at a point where year=2011.

What I found out is that arrow takes several arguments:

arrow(angle = 30, length = unit(0.25, "inches"), ends = "last", type = "open") 

So that I could say ends="first". But I can't generalize that ends is always first or always last.

I tried to add a column to my data.frame which has the information if the arrow should end first or last, but it didn't gives me the output I wanted.

Every help is highly appreciated :-)

Thanks in advance!

like image 297
Sarah West Avatar asked Apr 29 '11 12:04

Sarah West


People also ask

What is Geom_line in ggplot?

geom_line() connects them in order of the variable on the x axis. geom_step() creates a stairstep plot, highlighting exactly when changes occur. The group aesthetic determines which cases are connected together.

What does %>% do in ggplot?

%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Introducing magrittr. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.

What does Geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.

How do I change the thickness of a line in ggplot2?

To change line width, just add argument size=2 to geom_line().


2 Answers

geom_path should do the trick:

p <- ggplot(data, aes(x=x, y=y)) +
    geom_point(aes(colour=year), shape=16, size=6) +
    geom_path(aes(group=matching), 
                  arrow=arrow(length=unit(0.15,"cm")),
                  colour="black", size=1) +
    xlab("x") + ylab("y") +
    scale_colour_manual("year", values=colors) +
    scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
    scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))

print(p)

geom_path plot

like image 66
rcs Avatar answered Sep 25 '22 05:09

rcs


There is probably a more efficient way to do this, but one approach is to use geom_segment() instead of geom_line(). This will allow you to specify the beginning and ending points of the line with ease. We have to restructure the data so that we can specify x, y, xend, and yend. I will restructure with merge, though you could probably do this with cast or reshape.

zz <- merge(data[data$year == 2010 ,], data[data$year == 2011 ,]
  , by = "matching", suffixes = 1:2) 

  matching x1 y1 year1 x2 y2 year2
1  person1  1  1  2010  6  6  2011
2  person2  6  7  2010  1  2  2011
3  person3  3  5  2010  5  5  2011
4  person4  4  6  2010  2  6  2011
5  person5  2  3  2010  1  5  2011
6  person6  3  4  2010  5  2  2011

We will then use two datasets in our call to ggplot:

ggplot() +                                                       #Blank call to ggplot
  geom_point(data = data, aes(x=x, y=y, colour=year), shape=16, size=6) +    #Points
  geom_segment(data = zz, aes(x = x1, y = y1, xend = x2, yend = y2),         #Segments
    arrow = arrow(length = unit(0.15, "cm")), colour = "black", size = 1) +
  xlab("x") + ylab("y") +
  scale_colour_manual("year", values=colors) +
  scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
  scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))
like image 40
Chase Avatar answered Sep 25 '22 05:09

Chase