4

I have a dataset that looks like this:

   case prop weight  res
1     A   10    0.1 0.81
2     A   20    0.2 0.78
3     A   30    0.3 0.76
4     A   40    0.4 0.58
5     A   50    0.1 0.62
6     A   10    0.2 0.73
7     A   20    0.3 0.68
8     A   30    0.4 0.70
9     A   40    0.1 0.55
10    A   50    0.2 0.78
11    A   10    0.3 0.64
12    A   20    0.4 0.68
13    A   30    0.1 0.75
14    A   40    0.2 0.67
15    A   50    0.3 0.59
16    A   10    0.4 0.77
17    A   20    0.1 0.57
18    A   30    0.2 0.61
19    A   40    0.3 0.60
20    A   50    0.4 0.72
21    B   10    0.1 0.66
22    B   20    0.2 0.66
23    B   30    0.3 0.76
24    B   40    0.4 0.57
25    B   50    0.1 0.83
26    B   10    0.2 0.68
27    B   20    0.3 0.76
28    B   30    0.4 0.65
29    B   40    0.1 0.70
30    B   50    0.2 0.72
31    B   10    0.3 0.60
32    B   20    0.4 0.82
33    B   30    0.1 0.85
34    B   40    0.2 0.72
35    B   50    0.3 0.74
36    B   10    0.4 0.67
37    B   20    0.1 0.60
38    B   30    0.2 0.62
39    B   40    0.3 0.76
40    B   50    0.4 0.87
41    C   10    0.1 0.48
42    C   20    0.2 0.77
43    C   30    0.3 0.70
44    C   40    0.4 0.65
45    C   50    0.1 0.73
46    C   10    0.2 0.70
47    C   20    0.3 0.80
48    C   30    0.4 0.68
49    C   40    0.1 0.58
50    C   50    0.2 0.63
51    C   10    0.3 0.71
52    C   20    0.4 0.68
53    C   30    0.1 0.84
54    C   40    0.2 0.66
55    C   50    0.3 0.77
56    C   10    0.4 0.67
57    C   20    0.1 0.64
58    C   30    0.2 0.74
59    C   40    0.3 0.81
60    C   50    0.4 0.62

The data can be generated by the codes below:

 case = rep(c("A", "B", "C"), each=20)
    prop = rep(c("10", "20", "30", "40", "50"), 12)
    weight = as.factor(rep(c(0.1, 0.2, 0.3, 0.4), 15))
    res = round(rnorm(n=60, 0.7, 0.1), 2)

    dat = data.frame(case, prop, weight, res)
    dat

What I want to achieve is to have "prop" as x-axis, and "res" as y-axis, while using different colors to distinguish "case" and using different linetypes to distinguish "weight". For example, if weight=0.1, then use solid line; if weight=0.2, use dashed line, etc. From the codes below:

ggplot(data = dat, aes(x=prop, y=res, group=case, color=case)) +
  geom_line() +
  geom_point() + 
  theme_bw()

I can only get the following plot which is not desired... enter image description here

I tried to add geom_line(aes(linetype=weight)), but an error shows

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

Is there a way to map "weight" to the linetype in ggplot2? Thank you!

1 Answer 1

9

You could create a new variable to group by and to use for specifying the linetype..

dat$case.weight <- paste0(dat$case, dat$weight)

.. and add scale_linetype_manual():

ggplot(data = dat, aes(x=prop, y=res, group=case.weight, color=case, linetype=case.weight)) +
  scale_linetype_manual(values=rep(c("solid", "dashed", "dotted", "dotdash"),3), 
                        breaks=c("A0.1","A0.2", "A0.3", "A0.4"),
                        labels=c("0.1", "0.2", "0.3", "0.4"),
                        name="weight") +
  geom_line() +
  geom_point() + 
  theme_bw()

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Nice one. Maybe include facets as well. That will make the plot clearer imo.
@beetroot: Thank you very much! Just one quick question: is there a way to add a legend to show the different linetypes? I'd like readers to see that "solid", "dashed", "dotted", "dotdash" correspond to 0.1, 0.2, 0.3 and 0.4, respecitvely. Thanks!
@alittleboy I edited my answer to include a legend for weight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.