3

这是数据:

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))

这是我可以绘制的情节:

 require(ggplot2)
 ggplot(myd, aes(x = X2, y = Y, group = X3)) + 
geom_point (aes(col = X3, pch = X3)) + geom_line (aes(col = X3))

除了 X2 值之外,我还希望 X1 文本到 x 轴上的相应位置。干草稿:

在此处输入图像描述

我该怎么做 ?编辑:

注意:目的是在X轴上同时显示连续的刻度和文本:

4

2 回答 2

6

创建两个新层:

  • geom_rug对于轴上的线
  • geom_text对于标签 - 但首先创建所需标签的摘要

编码:

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))

在此处输入图像描述

于 2012-07-11T16:54:30.113 回答
2

如果您只想要 X1 标签而不想要坐标,您可以执行以下操作:

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)

您的带有 x 轴标签的绘图。

于 2012-07-11T16:27:39.937 回答