I would like to plot the following dataset
structure(list(X = structure(c(3L, 12L, 11L, 7L, 13L, 2L, 1L,
10L, 5L, 4L, 8L, 14L, 9L, 6L), .Label = c(\"BUM\", \"DD
This is an extension to @Andrie's solution. It combines the faceting idea with that of overplotting (stolen liberally from the learnr blog, which I find results in a cool visualization. Here is the code and the resulting output. Comments are welcome
mdf <- melt(df, id.vars="X")
mdf = transform(mdf, variable = reorder(variable, value, mean), Y = X)
ggplot(mdf, aes(x = variable, y = value)) +
geom_line(data = transform(mdf, X = NULL), aes(group = Y), colour = "grey80") +
geom_line(aes(group = X)) +
facet_wrap(~X) +
opts(axis.text.x = theme_text(angle=90, hjust=1))
EDIT: If you have groupings of milieus, then a better way to present might be the following
mycols = c(brewer.pal(4, 'Oranges'), brewer.pal(4, 'Greens'),
brewer.pal(3, 'Blues'), brewer.pal(3, 'PuRd'))
mdf2 = read.table(textConnection("
V1, V2
ETB, LEIT
PMA, LEIT
PER, LEIT
LEIT, LEIT
KON, TRADITION
TRA, TRADITION
DDR, TRADITION
TRADITION, TRADITION
BUM, MAIN
MAT, MAIN
MAIN, MAIN
EXP, HEDOS
HED, HEDOS
HEDOS, HEDOS"), sep = ",", header = T, stringsAsFactors = F)
mdf2 = data.frame(mdf2, mycols = mycols)
mdf3 = merge(mdf, mdf2, by.x = 'X', by.y = "V1")
p1 = ggplot(mdf3, aes(x = variable, y = value, group = X, colour = mycols)) +
geom_line(subset = .(nchar(as.character(X)) == 3)) +
geom_line(subset = .(nchar(as.character(X)) != 3), size = 1.5) +
facet_wrap(~ V2) +
scale_color_identity(name = 'Milieus', breaks = mdf2$mycols, labels = mdf2$V1) +
theme_bw() +
opts(axis.text.x = theme_text(angle=90, hjust=1))
First, melt the data to put it in a long format.
melted_data <- melt(the_data, id.vars = "X")
Now draw the plot with a numeric x axis, and fix up the labels.
p <- ggplot(melted_data, aes(as.numeric(variable), value, colour = X)) +
geom_line() +
scale_x_continuous(
breaks = seq_len(nlevels(melted_data$variable)),
labels = levels(melted_data$variable)
) +
opts(axis.text.x = theme_text(angle = 90))
p
Having answered this, I'm not sure what the plot tells you &ndahs; it's just a jumble of lines to me. You might be better greying out most of the lines, and highlighting one or two interesting ones.
Add a column that picks out, e.g., EXP.
melted_data$is_EXP <- with(melted_data, X == "EXP")
Ignore my previous anser; Andrie's is better. Use manual colour and size scales to highlight your new column.
p <- ggplot(melted_data, aes(variable, value, colour = is_EXP, size = is_EXP, group = X)) +
geom_line() +
scale_colour_manual(values = c("grey80", "black")) +
scale_size_manual(values = c(0.5, 1.5)) +
opts(axis.text.x = theme_text(angle = 90, hjust=1))
p
The trick is to reshape your data into tall format before you pass it to ggplot
. This is easy when using the melt
function in package reshape2
:
Assuming your data is a variable called df
:
library(reshape2)
library(ggplot2)
mdf <- melt(df, id.vars="X")
str(mdf)
ggplot(mdf, aes(x=variable, y=value, colour=X, group=X)) + geom_line() +
opts(axis.text.x = theme_text(angle=90, hjust=1))
Edit As @Chase
suggests, you can use facetting to make the plot more readable:
ggplot(mdf, aes(x=X, y=value)) + geom_point() +
opts(axis.text.x = theme_text(angle=90, hjust=1)) + facet_wrap(~variable)