2017-04-05 78 views
1

我想画出散点图散点图此数据的R - 改变X轴值类别

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") 
+0

尝试用'x = as.factor(Subject)'或'x = as.character(Subject)'替换'x = Subject'。 –

+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") 
0

你可以使用as.factor(data$Subject)scale_x_discrete("Subject")。这里假设一个例子:

data <- read.table(textConnection('i Subject Length Verdict 
1 2 4575 Partial 
2 2 5060 Partial 
3 2 8978 5DEFECT 
4 2 7224 Partial 
5 2 7224 Partial 
6 7 8978 5DEFECT'), header = TRUE, stringsAsFactors = FALSE) 

data$Subject <- as.factor(data$Subject) 
p = ggplot(data) + geom_point(aes(x=Subject,y=Length, colour=Verdict))+ scale_shape_manual(values= c(5,0,1,4,6)) 
p = p + scale_colour_manual(values=c("blue","red","green","black","violet")) 
p = p+ scale_x_discrete("Subject") 
p = p + scale_y_continuous(breaks=c(2000,4000,6000,8000,10000))+labs(y="Amplicon Size in bps") 
p