简体   繁体   中英

Setting different linetypes in geom_smooth, ggplot2

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM