Last active
July 18, 2016 03:04
-
-
Save jeffreyiacono/963243092481ed010e7c1dc6c1444841 to your computer and use it in GitHub Desktop.
Start / End Plotting with R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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