Reputation: 621
Here is data:
myd <- data.frame (X1 = rep (c("A0001", "B0002", "C0003", "D0004"), each = 2),
X2 = rep (c(1, 5.3, 8.2, 12.5), each = 2), X3 = rep (c("A", "B"), 4),
Y = rnorm (8, 5, 2))
Here is plot I could plot:
require(ggplot2)
ggplot(myd, aes(x = X2, y = Y, group = X3)) +
geom_point (aes(col = X3, pch = X3)) + geom_line (aes(col = X3))
I want to the X1 text to corresponding position in x axis in addition to X2 values. Just dry sketch:
How can I do it ? Edit:
Note: Objective is to display continous scale and the texts at X axis same time:
Upvotes: 4
Views: 6993
Reputation: 179428
Create two new layers:
geom_rug
for the lines on the axisgeom_text
for the labels - but first create a summary of the required labelsThe code:
ruglabels <- unique(myd[, 1:2])
require(ggplot2)
ggplot(myd, aes(x=X2, y=Y)) +
geom_point (aes(col = X3, pch = X3, col=X3)) +
geom_line (aes(col = X3, col=X3)) +
geom_rug(sides="b") +
geom_text(data=ruglabels, aes(x=X2, label=X1, y=2))
Upvotes: 7
Reputation: 1843
If you just want X1 labels and don't want coordinates, you can do something like:
require(ggplot2)
myd <- data.frame (X1 = rep (c("A0001", "B0002", "C0003", "D0004"), each = 2),
X2 = rep (c(1, 5.3, 8.2, 12.5), each = 2), X3 = rep (c("A", "B"), 4),
Y = rnorm (8, 5, 2))
ggplot(myd, aes(x = X2, y = Y, group = X3)) +
geom_point (aes(col = X3, pch = X3)) + geom_line (aes(col = X3))+
scale_x_continuous( breaks = myd$X2, labels = myd$X1)
Upvotes: 2