geom_segment and geom_point

161 views
Skip to first unread message

Alessandra Carioli

unread,
Nov 17, 2015, 11:43:39 AM11/17/15
to ggplot2

Dear All,

I am using geom segment to underline a change in a variable (sort of time=0 and time=1). I would like to have this distinction through 2 different colors in geom_point (starting point and end point) and have it appear in the legend, but I am at loss on how it can be done…

here is my code:

ggplot(dt,aes(O_TFR,TFR))+
geom_point(col="green")+
geom_segment(aes(x=O_TFR,y=TFR,xend=TFR_1.1,yend=TFR_1))+
geom_point(aes(TFR_1.1,TFR_1), col="red")

and the data:

dt <-structure(list(Country = structure(c(1L, 7L, 6L, 8L, 19L, 10L,
13L, 15L, 17L, 18L, 2L, 3L, 5L, 4L, 9L, 11L, 12L, 14L, 16L), .Label = c("Austria",
"Bulgaria", "Czech Rep.", "East Germany", "Estonia", "Finland",
"Flanders", "France", "Hungary", "Italy", "Latvia", "Lithuania",
"Norway", "Poland", "Portugal", "Slovenia", "Spain", "Sweden",
"West Germany"), class = "factor"), FFS_DFS = c(2, 2.01, 2.42,
2.27, 1.88, 2.07, 2.36, 2.1, 2.2, 2.42, 1.9, 2, 2.39, 1.88, 2.1,
2.1, 2.1, 2.2, 2.23), TFR = c(2.1, 2.16, 2.73, 2.49, 2.02, 2.17,
2.3, 2.16, 2.41, 2.62, 2.01, 2.21, 2.49, 1.97, 2.2, 2.25, 2.14,
2.33, 2.38), p0_1 = c(0.9, 0.905, 0.95, 0.952, 0.92, 0.966,
0.98, 0.957, 0.964, 0.96, 0.975, 0.98, 0.963, 0.98, 0.99, 0.988,
0.91, 0.93, 0.9), TFR_1 = c(2.24, 2.39, 2.87, 2.61, 2.2,
2.25, 2.4, 2.26, 2.5, 2.73, 2.06, 2.23, 2.59, 2, 2.22, 2.28,
2.23, 2.49, 2.37), O_TFR = c(1.75, NA, 1.82, NA, NA, NA, 2.03,
1.72, NA, 2.18, 1.443, 1.84, 1.7, NA, 1.95, NA, 1.67, NA, 1.72
), O_p0_1 = c(0.879, NA, 0.75, NA, NA, NA, 0.878, 0.883, NA,
0.926, 0.798, 0.837, 0.825, NA, 0.867, NA, 0.848, NA, 0.838),
TFR_1.1 = c(1.99, NA, 2.43, NA, NA, NA, 2.31, 1.94, NA, 2.35,
1.79, 2.17, 1.94, NA, 2.25, NA, 1.97, NA, 2.05)), .Names = c("Country",
"FFS_DFS", "TFR", "p0_1", "TFR_1", "O_TFR", "O_p0_1", "TFR_1.1"
), class = “data.frame", row.names = c(NA, -19L))


Ben Bond-Lamberty

unread,
Nov 17, 2015, 1:09:35 PM11/17/15
to Alessandra Carioli, ggplot2
I think you'll want to do some data reshaping and plot from two
separate data frames, e.g.:

library(reshape2)
pointdata <- melt(dt, id.vars="Country", measure.vars=c("O_TFR","TFR_1.1"))
pointdatay <- melt(dt, id.vars="Country", measure.vars=c("TFR","TFR_1"))
pointdata$yvalue <- pointdatay$value

# Plot the points, colored by old/new
ggplot(pointdata, aes(value, yvalue, color=variable)) + geom_point() +

# ...and the line segments
geom_segment(data=dt, aes(x=O_TFR, y=TFR, xend=TFR_1.1, yend=TFR_1),
color="black")

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dennis Murphy

unread,
Nov 17, 2015, 2:29:04 PM11/17/15
to Alessandra Carioli, ggplot2
Here's another way where you can create a factor on the fly, but in
general I agree with Ben's comments about reshaping the data.

ggplot(dt,aes(O_TFR,TFR))+
geom_point(aes(color="Time 0"), size = 2.5)+
geom_segment(aes(x=O_TFR,y=TFR,xend=TFR_1.1,yend=TFR_1))+
geom_point(aes(x = TFR_1.1, y = TFR_1, color ="Time 1"), size = 2.5) +
scale_color_manual("Time", breaks = c("Time 0", "Time 1"),
values = c("blue", "darkorange"))

BTW, I got an error when reading in your dput data because there was a
curly quote on the last line to the left of "data.frame".

The data reshaping problem is complicated by the fact that you have
several variables in 'wide form', whereas ggplot2 normally prefers
'long form'. The dplyr + tidyr combination is one way to do this;
another is to use the melt() function in the latest version of
data.table (1.9.6). To see how multiple groups of variables are
handled, read the reshaping vignette at
https://rawgit.com/wiki/Rdatatable/data.table/vignettes/datatable-reshape.html.

Dennis

Alessandra Carioli

unread,
Nov 17, 2015, 3:04:00 PM11/17/15
to Dennis Murphy, ggplot2
thanks a lot, they both work perfectly.

Ale
Reply all
Reply to author
Forward
0 new messages