R - 将X轴值更改为类别

时间:2017-04-05 00:13:50

标签: r

我正在尝试为此数据绘制散点图

head(data)
  Subject Length  Verdict
1     2   4575  Partial
2     2   5060  Partial
3     2   8978 5'DEFECT
4     2   7224  Partial
5     2   7224  Partial
6     7   8978 5'DEFECT

我得到一个散点图:

Scatter dot plot

我有患者1,2,6,7,10例如。 R取出我的主题的名字并将它们用作x值。我想改变这一点,以便数据点出现在每个患者上方(不作为值,而是作为一个类别)。

感谢您的帮助!

这是我为编写散点图而编写的代码:

ggplot(final,
       aes(x=Subject,y=Length,colour=Verdict,shape=Verdict), group=Subject) +
  geom_point(position=position_jitter(width=0.1,height=0)) +
  scale_shape_manual(values=c(5,0,1,4,6)) +
  scale_colour_manual(values=c("blue","red","green","black","violet")) +
  scale_y_continuous(breaks=c(2000,4000,6000,8000,10000)) +
  labs(y="Amplicon Size in bps")

2 个答案:

答案 0 :(得分:0)

使用您发布的样本数据(6个观察结果),您可以使用as.factoras.character轻松完成此操作。我建议首先将Subject变量转换为字符(因为听起来你不希望它被视为数字)。

这应该有效:

data$Subject <- as.character(data$Subject)

ggplot(data,
       aes(x=Subject,y=Length,colour=Verdict,shape=Verdict), group=Subject) +
  geom_point(position=position_jitter(width=0.1,height=0)) +
  scale_shape_manual(values=c(5,0,1,4,6)) +
  scale_colour_manual(values=c("blue","red","green","black","violet")) +
  scale_y_continuous(breaks=c(2000,4000,6000,8000,10000)) +
  labs(y="Amplicon Size in bps")

enter image description here

数据:

data <- structure(list(Subject = c(2L, 2L, 2L, 2L, 2L, 7L), Length = 
                  c(4575L, 5060L, 8978L, 7224L, 7224L, 8978L), Verdict = c("Partial", "Partial", 
                  "5'DEFECT", "Partial", "Partial", "5'DEFECT")), .Names = c("Subject", 
                  "Length", "Verdict"), row.names = c(NA, -6L), class = "data.frame")

答案 1 :(得分:0)

您可以使用Decimal64p2as.factor(data$Subject)。这是一个虚拟的例子:

scale_x_discrete("Subject")