2014-12-02 179 views
2

我试图建立一个阴谋的多元回归模型的数据是这样的:在geom_smooth设置不同的线型,GGPLOT2

subject iq  condition RT 
1  98  A   312 
1  98  B   354 
1  98  C   432 
2  102  A   134 
2  102  B   542 
2  102  C   621 
...  ...  ...  ... 

等。

我想在x轴上绘制iq,在y轴上绘制RT,并针对不同的条件使用具有不同线型的不同线条(虚线,虚线)。

到目前为止,我的代码看起来是这样的:

ggplot(DFplotlong, aes(iq, RT, colour = condition)) 
+ geom_smooth(method = lm, fullrange = TRUE, alpha = .15) 
+ theme_bw() 
+ labs(x = "iq", y = "reaction times") 
+ scale_colour_manual(values=c("#999999","#000000"), name="condition", breaks=c("A", "B", "C"), labels = c("easy", "medium", "hard")) 

现在,除了我觉得我有点需要设置线型,但我不知道是否使用scale_linetype_manual,scale_linetype_discrete,或什么的。另外,我不知道如何使用正确的功能。

任何人都可以帮我解决这个问题吗?那样就好了!

PS:我曾尝试过各种东西,但为R给我,其中的颜色被指定为目的的阴谋,但线型不改,但留下的固体,或它给了我这样的错误信息

Fehler in grid.Call.graphics(L_polygon, x$x, x$y, index) : 
ungültiger Linientyp: muss Länge 2, 4, 6, oder 8 haben 

我在英语猜应该是这样的

Error in grid.Call.graphics(L_polygon, x$x, x$y, index) : 
invalid linetype: must be length 2, 4, 6, or 8 

回答

3

这一切似乎你缺少的是里面的aes() ARG的linetype = condition ument。另外,你的scale_colour_manual调用似乎是错误的:你只给出两个值而不是三个。要正确计算比例尺,您可以使用scale_linetype_discrete()进行自动缩放,或使用scale_linetype_manual()手动设置线型。这里的例子:

# 
DFplotlong <- read.table(text='subject iq  condition RT 
1  98  A   312 
1  98  B   354 
1  98  C   432 
2  102  A   134 
2  102  B   542 
2  102  C   621', header=TRUE) 
# 
ggplot(DFplotlong, aes(iq, RT, colour = condition, linetype = condition)) + 
    geom_point() + 
    geom_smooth(method = lm, fullrange = TRUE, alpha = .15) + 
    theme_bw() + 
    labs(x = "iq", y = "reaction times") + 
    scale_colour_manual(values=c("#999999","#000000", "#900009"), 
        name="condition", 
        breaks=c("A", "B", "C"), 
        labels = c("easy", "medium", "hard")) + 
    scale_linetype_discrete(name="condition", 
          breaks=c("A", "B", "C"), 
          labels = c("easy", "medium", "hard")) 

enter image description here

+1

谢谢!这是我需要的。为了设置特定的线型,我还将'values = c(“A”= 2,“B”= 5,“C”= 1)'添加到'scale_linetype_manual()'中。 – bunsenbaer 2014-12-02 16:15:48

0
DFplotlong <- read.table(header = TRUE, text = " 
    subject iq  condition RT 
    1  98  1   312 
    1  98  2   354 
    1  98  3   432 
    2  102  1   134 
    2  102  2   542 
    2  102  3   621 
    3  105  1   137 
    3  105  2   545 
    3  105  3   624 
    ") 

DFplotlong$condition <- factor(DFplotlong$condition) 

ggplot(data=DFplotlong, aes(iq, RT, colour=condition, linetype=condition)) + 
    geom_smooth(method = lm, fullrange = TRUE) + 
    theme_bw() + 
    labs(x = "iq", y = "reaction times")