Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Last active July 18, 2016 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffreyiacono/963243092481ed010e7c1dc6c1444841 to your computer and use it in GitHub Desktop.
Save jeffreyiacono/963243092481ed010e7c1dc6c1444841 to your computer and use it in GitHub Desktop.
Start / End Plotting with R
library(ggplot2)
library(reshape2)
# data setup
df <- data.frame(grp = 1:25, start = rnorm(25, 100, 10), end = rnorm(25, 10, 5))
# ** plotting using base plot functions
par(mfrow = c(1, 1))
rng <- range(df$start, df$end)
with(df, plot(rep(1, nrow(df)), df$start, xlim = c(0.5, 2.5), ylim = rng, xaxt = "n", xlab = "", ylab = "Random Counts"))
with(df, points(rep(2, nrow(df)), df$end))
segments(rep(1, nrow(df)), df$start, rep(2, nrow(df)), df$end)
axis(1, c(1, 2), c("start", "end"))
# ** plotting using ggplot
# reshape the data first ...
mdf <- melt(df, id.vars = "grp")
names(mdf) <- c("grp", "category", "value")
mdf$grp <- factor(mdf$grp)
mdf$category <- factor(mdf$category)
ggplot(mdf, aes(x = category, y = value, group = grp, color= grp)) + geom_point() + geom_line()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment