在geom_smooth,ggplot2中设置不同的线型 [英] Setting different linetypes in geom_smooth, ggplot2

查看:894
本文介绍了在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
... ... ... ...

等等。 p>

我想在x轴上绘制iq,在y轴上绘制RT,并使用不同线条的不同线条(虚线,虚线,例如)条件。

到目前为止,我的代码如下所示:

  
+ theme_bw()
+ labs(() x =iq,y =反应次数)
+ scale_colour_manual(values = c(#999999,#000000),name =condition,breaks = c(A B,C),标签= c (easy,medium,hard))

现在,不知何故需要设置线型,但我不知道是否使用scale_linetype_manual,scale_linetype_discrete或其他。此外,我不知道如何使用正确的功能。



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



Ps:我已经尝试了各种各样的东西,但是R给了我一个颜色按照预期指定的情节,但是线型不会改变,但保持稳定,或者它给了我一些错误信息,比如在grid.Call.graphics(L_polygon,x $ x,...)中的

  Fehler, x $ y,index):
ungültigerLinientyp:mussLänge2,4,6,or 8 haben

我猜英文应该是像

  grid.Call.graphics中的错误(L_polygon,x $ x ,x $ y,index):
无效的线型:必须是长度2,4,6或8


aes( linetype = condition >) )参数。另外,您的 scale_colour_manual 调用似乎是错误的:您只给出两个值而不是三个值。要正确衡量比例,您可以使用 scale_linetype_discrete()进行自动缩放,或者使用 scale_linetype_manual()手动设置线型。下面是这个例子:

pre $
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,color = condition,linetype = condition))+
geom_point()+
geom_smooth(method = lm,fullrange = TRUE,alpha = .15)+
theme_bw()+
labs(x =iq,y =反应次数)+
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))


I am trying to set up a plot for a multiple regression model for data that look like this:

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

and so on.

I would like to plot iq on the x-axis, RT on the y-axis, and use differently coloured lines with different linetypes (dashed, dotted, e.g.) for the different conditions.

Thus far, my code looks like this:

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

Now, in addition I think I somehow need to set linetype, but I do not know whether to use scale_linetype_manual, scale_linetype_discrete, or whatever. Also, I would not know how to use the correct function.

Could anyone help me out on this one? That would be nice!

Ps: I have tried various things, but either R gives me a plot in which the colours are specified as intended, but linetypes do not change, but stay solid, or it gives me error messages such as

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

which I guess in English should be something like

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

解决方案

It seems that all you are missing is the linetype = condition inside the aes() argument. In addition, your scale_colour_manual call seems to be wrong: you give only two values instead of three. To get the scales right, you can either use scale_linetype_discrete() for automatic scaling or scale_linetype_manual() for manually setting the linetypes. Here's the example:

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

这篇关于在geom_smooth,ggplot2中设置不同的线型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆