2016-07-21 42 views
0

这是从我的CSV文件中的数据:情节线组点

Tx, Varx, Scale, Val1, Val2 
A, VAR1, 5, 516, 2 
A, VAR1, 10, 447.4, 5 
A, VAR1, 15, 294, 8 
A, VAR1, 20, 217.2, 12 

A, VAR2, 5, 675.4, 4 
A, VAR2, 10, 423.2, 9 
A, VAR2, 15, 276, 12 
A, VAR2, 20, 200, 15 

B, VAR1, 5, 624, 6 
B, VAR1, 10, 465.2, 13 
B, VAR1, 15, 315.2, 16 
B, VAR1, 20, 234.8, 18 

B, VAR2, 5, 518.8, 8 
B, VAR2, 10, 443, 17 
B, VAR2, 15, 278.4, 20 
B, VAR2, 20, 217.8, 24 

我想绘制线(不只是分)VARx前提和Tx值之间distingish。我想使用此代码绘制:

data_table = read.csv("PATH/file.csv",check.names=FALSE,header=T,sep=",") 
data_table$NScale <- as.numeric(as.character((data_table$Scale))) 
ggplot(data_table, aes(x=NScale, y=Val2, colour=Tx, shape=Varx, linetype=Varx, group=Tx)) + geom_point() 

enter image description here

当我尝试绘图加盟蓝triangules,红triangules,红色圆圈,蓝色圆圈,用geom_line(),geom_path线()显示错误消息:

Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line 

如何通过形状和颜色创建线条组?我尝试了几种方法,但仍然没有得到它。问题是什么?

回答

1

你应该尝试制作一个新的“分组”变量,并分隔你如何做“点”和“线”的情节。

我做这个用在这里,但dplyr你可以使用基础R以及做到这一点:

library(dplyr) 

data_table <- data_table %>% 
    rowwise() %>% 
    mutate(TxVarxgroup = paste0(Tx, Varx, collapse="")) 

ggplot(data_table) + 
    geom_point(aes(x=NScale, y=Val2, colour=Tx, shape=Varx)) + 
    geom_line(aes(x=NScale, y=Val2, group=TxVarxgroup)) 

enter image description here

+0

非常感谢。你的回答非常有帮助。其实我使用'geom_line(aes(x = NScale,y = Val2,color = Tx,shape = Varx,linetype = Varx))' –

+0

只是一个FYI,'geom_line'不理解'shape'唯美它实际上并没有做任何事情:P – chappers

+0

对! 'geom_line(aes(x = NScale,y = Val2,color = Tx,linetype = Varx))'谢谢! –