Wafa
Wafa

Reputation: 89

Manually adjusting colors and line types in ggplot with interactions

I have some data that is sorted by three categories, as follows:

 toy
# A tibble: 20 x 5
# Groups:   v190, treatedgroup [10]
   Size   Animal Fluffy    AvgWeight     SE
   <fct>  <fct>  <fct>         <dbl>  <dbl>
 1 Tiny   Dog    Shorthair      114. 0.0724
 2 Tiny   Dog    Longhair       113. 0.0904
 3 Tiny   Cat    Shorthair      117. 0.150 
 4 Tiny   Cat    Longhair       114. 0.318 
 5 Small  Dog    Shorthair      116. 0.0685
 6 Small  Dog    Longhair       115. 0.0974
 7 Small  Cat    Shorthair      119. 0.119 
 8 Small  Cat    Longhair       116. 0.316 
 9 Medium Dog    Shorthair      116. 0.0662
10 Medium Dog    Longhair       117. 0.106 
11 Medium Cat    Shorthair      120. 0.118 
12 Medium Cat    Longhair       117. 0.294 
13 Large  Dog    Shorthair      117. 0.0662
14 Large  Dog    Longhair       118. 0.111 
15 Large  Cat    Shorthair      120. 0.114 
16 Large  Cat    Longhair       118. 0.267 
17 Giant  Dog    Shorthair      118. 0.0633
18 Giant  Dog    Longhair       120. 0.103 
19 Giant  Cat    Shorthair      121. 0.123 
20 Giant  Cat    Longhair       120. 0.265 

I would like to plot this as a line chart with confidence intervals. I'd like the color to reflect the animal and the line type to reflect Fluffy. This plot below is exactly what I want, but the legend is not consolidated. The black lines representing line types are confusing. I followed the advice here to try and fix it, but as you can see it did not work.

ggplot(data = toy, aes(x = Size, y = AvgWeight, color=Animal, 
                       linetype=Fluffy)) +
  geom_line(aes(x = Size, y = AvgWeight, 
                group=interaction(Animal,Fluffy)), size=1) + 
  geom_point() +
  geom_errorbar(aes(ymax = AvgWeight + SE, 
                    ymin = AvgWeight - SE), width = 0.05, size=1) + 
  scale_color_discrete("Key") +
  scale_linetype_manual(name="Key",values=  rep(1:2,2))

Image with incorrect legend

Then I tried to create interactions, as follows:

ggplot(data = toy, aes(x = Size, y = AvgWeight, color=interaction(Animal,Fluffy), 
                       linetype=interaction(Animal,Fluffy))) +
  geom_line(aes(x = Size, y = AvgWeight, 
                group=interaction(Animal,Fluffy)), size=1) + 
  geom_point() +
  geom_errorbar(aes(ymax = AvgWeight + SE, 
                    ymin = AvgWeight - SE), width = 0.05, size=1) + 
  scale_color_discrete("Key") +
  scale_linetype_manual(name="Key",values=  rep(1:2,2))

This code gives me a legend that looks correct, but now the colors are a mess:

enter image description here

Specifying the colors in scale_color_discrete gave me the following error:

Error in discrete_scale(aesthetics, "hue", hue_pal(h, c, l, h.start, direction),  : 
  unused argument (values = c(Dog.Shorthair = "indianred4", Cat.Shorthair = "dodgerblue4", Dog.Longhair = "indianred4", Cat.Longhair = "dodgerblue4"))

What am I doing wrong? How can I either manually specify colors in the second graph, or consolidate the legend in the first?

Upvotes: 2

Views: 777

Answers (1)

StupidWolf
StupidWolf

Reputation: 46898

I can only read it your table from above, so I don't have the levels of your factor. So the below should work more or less, you just need to try it on your data frame with the correct levels, and flip the colors / linetype if need be:

toy$int = with(toy,interaction(Animal,Fluffy))

ggplot(data = toy, aes(x = Size, y = AvgWeight,color=int, 
                       linetype=int)) +
  geom_line(aes(group=int)) + 
  geom_point() +
  geom_errorbar(aes(ymax = AvgWeight + SE, 
                    ymin = AvgWeight - SE), width = 0.05, size=1)+
scale_color_manual(values=rep(c("dodgerblue4","indianred4"),2)) +
scale_linetype_manual(values=rep(c("solid","dashed"),each=2))

enter image description here

Upvotes: 1

Related Questions