简体   繁体   中英

How to plot two sets of data with two different color schemes on the same graph using ggplot?

Note: as I'm writing this I can't figure out how to insert images, I'll work on it after posting, but if you run the code below, you should be able to see the graphs I'm talking about....sorry!

Essentially, I have these two graphs and I want them to be on the same plot (overlayed on top of one another), but I need them to use different color schemes or I won't be able to tell them apart very easily.

I've looked everywhere on this site and while there are a lot of similar questions, none of them have worked quite in the way that I need them to. The closest ones I've linked below, just know that I've read them and they did not solve my issues:

Distinct color palettes for two different groups in ggplot2

R ggplot two color palette on the same plot

The first graph uses this data (shortened to 50 lines, actually goes to about 1000), RuleCount repeats 1-14 over and over, TrainingPass goes up until about 60

     RuleCount TrainingPass  m4Accuracy
1           1           -1 0.000000000
2           2           -1 0.000000000
3           3           -1 0.004225352
4           4           -1 0.014225352
5           5           -1 0.022816901
6           6           -1 0.182957746
7           7           -1 0.194507042
8           8           -1 0.207183099
9           9           -1 0.239859155
10         10           -1 0.362394366
11         11           -1 0.430704225
12         12           -1 0.567887324
13         13           -1 0.582535211
14         14           -1 0.602676056
15          1            0 0.000000000
16          2            0 0.000281690
17          3            0 0.006901408
18          4            0 0.018732394
19          5            0 0.031267606
20          6            0 0.202676056
21          7            0 0.215633803
22          8            0 0.231830986
23          9            0 0.262253521
24         10            0 0.373661972
25         11            0 0.440281690
26         12            0 0.573802817
27         13            0 0.588169014
28         14            0 0.608873239
29          1            1 0.000985915
30          2            1 0.014788732
31          3            1 0.032957746
32          4            1 0.071408451
33          5            1 0.113943662
34          6            1 0.276760563
35          7            1 0.290281690
36          8            1 0.303943662
37          9            1 0.335633803
38         10            1 0.438028169
39         11            1 0.501971831
40         12            1 0.625070423
41         13            1 0.637323944
42         14            1 0.658169014
43          1            2 0.000985915
44          2            2 0.015915493
45          3            2 0.030704225
46          4            2 0.076619718
47          5            2 0.119436620
48          6            2 0.280563380
49          7            2 0.294507042
50          8            2 0.308732394

I graphed it using this code:

ggplot(df_m4, aes(x=RuleCount, y=m4Accuracy, group = TrainingPass, color = TrainingPass)) +
  geom_line()+
  scale_color_gradient(low = "green", high = "blue")

Resulting in this graph:

m4 Accuracy

The second graph is essentially the same data and code, except rather than getting a bunch of slightly varying lines on the graph, each of the lines ends up being the same line

data:

    RuleCount TrainingPass    Accuracy
1           1           -1 0.000422535
2           2           -1 0.000422535
3           3           -1 0.002676056
4           4           -1 0.005915493
5           5           -1 0.007746479
6           6           -1 0.053239437
7           7           -1 0.059718310
8           8           -1 0.068309859
9           9           -1 0.099859155
10         10           -1 0.197042254
11         11           -1 0.256197183
12         12           -1 0.421971831
13         13           -1 0.440422535
14         14           -1 0.468028169
15          1            0 0.000422535
16          2            0 0.000422535
17          3            0 0.002676056
18          4            0 0.005915493
19          5            0 0.007746479
20          6            0 0.053239437
21          7            0 0.059718310
22          8            0 0.068309859
23          9            0 0.099859155
24         10            0 0.197042254
25         11            0 0.256197183
26         12            0 0.421971831
27         13            0 0.440422535
28         14            0 0.468028169
29          1            1 0.000422535
30          2            1 0.000422535
31          3            1 0.002676056
32          4            1 0.005915493
33          5            1 0.007746479
34          6            1 0.053239437
35          7            1 0.059718310
36          8            1 0.068309859
37          9            1 0.099859155
38         10            1 0.197042254
39         11            1 0.256197183
40         12            1 0.421971831
41         13            1 0.440422535
42         14            1 0.468028169
43          1            2 0.000422535
44          2            2 0.000422535
45          3            2 0.002676056
46          4            2 0.005915493
47          5            2 0.007746479
48          6            2 0.053239437
49          7            2 0.059718310
50          8            2 0.068309859

code:

ggplot(df_rules_only, aes(x=RuleCount, y=Accuracy, group = TrainingPass, color = TrainingPass)) +
  geom_line() +
  scale_color_gradient(low = "green", high = "blue")

Resulting in this graph:

rules only Accuracy

I understand how to get the data on to the same graph. By combining my two data frames and using the code below, I can add the 'rules_only' data to the 'm4' graph:

ggplot(df_Training, aes(x=ruleCount, y=m4Accuracy, group = training_pass, color = training_pass)) +
  geom_line() +
  scale_color_gradient(low = "green", high = "blue")+
  geom_line(aes(x=ruleCount, y=rulesOnlyAccuracy))

Resulting in this graph:

both_data_sets

The problem is that the new data blends right in with the old because it has the same color scheme.

At first I tried keeping them in the same data frame and just adding "color = 'orange'" to the last line of the previous code, but that gives me the error: "Error: Discrete value supplied to continuous scale"

Next I split them up into the two data frames you see above and tried to graph them this way:

ggplot(df_m4, aes(x=RuleCount, y=m4Accuracy, group = TrainingPass, color = TrainingPass)) +
  geom_line() +
  scale_color_gradient(low = "green", high = "blue")+
  geom_line(df_rules_only, aes(x=RuleCount, y=Accuracy, color = "orange"))

but I get the error: "Error: mapping must be created by aes() "

Those last two attempts were kind of shots in the dark since I couldn't find anything else to try, but I'm pretty certain R doesn't work that way.

I'd really prefer for answers to use ggplot since other graphs never look quite as good. Just really feel like I've been going about this all wrong and could really use some help! Thank you in advance :)

Very complicated question for a very simple answer. Wanted to move this out of the comments but @aosmith helped me out. The code below makes my second group of data a different color:

ggplot(df_Training, aes(x=ruleCount, y=m4Accuracy, group = training_pass, color = training_pass)) +
  geom_line() +
  geom_line(aes(x=ruleCount, y=rulesOnlyAccuracy), color = "orange")

Just have to work on adding a second legend now!

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