1
votes

I'm plotting regression lines from two different variables on the same plot. I'd like to make one regression line dashed, and the other solid.

I've tried using the solution suggested in a similar question: Plot dashed regression line with geom_smooth in ggplot2, but when I use linetype="dashed" in my code, I still get solid lines. I think I must have overridden the linetype command somewhere in my code. So really, 2 questions: (1) how do I fix my code to produce dashed regression lines, and (2) is it possible to make 1 regression solid and 1 dashed on the same plot?

Any suggestions appreciated!

Current ggplot code:

ggplot(CanksVolumes, aes(TotalCankers, BA_05thru14_mm2, 
                         color=Site, alpha=Treatment)) + 
  scale_alpha_discrete(range = c(0.4, 1)) + 
  scale_color_brewer(palette = 'Set1') +
  geom_point() + geom_smooth(method = "lm", se=F, aes(fill=Site, linetype="dashed")) + 
  xlab("Total Number of Cankers per Tree") + 
  ylab("Tree Basal Area (mm^2) 2005-2014") +
  ggtitle("Total Number of Cankers Associated with Recent Growth (Basal Area Last 10 Years)") +
  facet_wrap(~Site)

And it's large, but a reproducible set of data:

CanksVolumes <- structure(list(Unique_ID_Tree = c("M3T_1", "M3T_2", "M3T_3", 
"M3T_4", "M3T_5", "M3T_6", "M3U_1", "M3U_2", "M3U_3", "M3U_4", 
"M3U_5", "M3U_6", "N1T_1", "N1T_2", "N1T_3", "N1T_4", "N1T_5", 
"N1T_6", "N1U_1", "N1U_2", "N1U_3", "N1U_4", "N1U_5", "N1U_6", 
"N2T_1", "N2T_2", "N2T_3", "N2T_4", "N2T_5", "N2T_6", "N2U_1", 
"N2U_2", "N2U_3", "N2U_4", "N2U_5", "N2U_6"), Disc_ID = c("M3T1045", 
"M3T2045", "M3T3045", "M3T4045", "M3T5045", "M3T6045", "M3U1045", 
"M3U2045", "M3U3045", "M3U4045", "M3U5045", "M3U6045", "N1T1045", 
"N1T2045", "N1T3045", "N1T4045", "N1T5045", "N1T6045", "N1U1045", 
"N1U2045", "N1U3045", "N1U4045", "N1U5045", "N1U6045", "N2T1045", 
"N2T2045", "N2T3045", "N2T4045", "N2T5045", "N2T6045", "N2U1045", 
"N2U2045", "N2U3045", "N2U4045", "N2U5045", "N2U6045"), TotalBA_mm2 = c(48422.0665741449, 
67345.5883698897, 93132.2607802485, 131867.048278746, 70774.4553777143, 
107360.658676709, 21773.5273135633, 24311.3559989519, 35102.0690873188, 
51817.4544898566, 24865.5461220263, 39005.9400223312, 24441.4199925537, 
43910.513721618, 20070.0181216216, 37068.1963964739, 49682.9417571679, 
86858.8816403778, 22417.9549103913, 35151.8985952557, 61251.0544197884, 
19683.6928319038, 30376.0140061034, 37578.4060759377, 68388.2050444103, 
65538.2384603496, 49352.4211809415, 39297.7321533062, 58503.2107932949, 
66813.9962945424, 51944.222010287, 68390.0591244299, 19013.6024003523, 
27550.4271046627, 39560.2876397285, 19580.3808789065), BA_85thru14_mm2 = c(14611.6335711751, 
45711.4998451498, 44404.7114510255, 83650.6979165133, 39953.2185049916, 
74813.8132733016, 6097.83748871461, 11675.9659731525, 11733.732636389, 
31782.0733791433, 8772.57695473424, 21371.2188664859, 11481.2053713276, 
25176.936475585, 7234.60603316087, 16459.7694084024, 32266.4395069078, 
60914.7750214847, 8231.02514160435, 16712.6392484347, 39236.2448736159, 
8996.60527146338, 12366.013456859, 23577.0687802852, 42737.3221959725, 
45669.5980237521, 28106.5850019948, 27127.3841712043, 37867.2943668496, 
44642.1681882516, 27352.3825332051, 43690.2558752766, 12840.7406254419, 
16954.3949913134, 26754.5778521084, 12128.8850034988), BA_90thru14_mm2 = c(9894.92804248437, 
36167.3432956235, 35155.176482765, 68790.9162216748, 33046.7820717523, 
61587.8471229157, 3486.06635597643, 8982.57698269882, 8583.83641098213, 
25750.4409909132, 6665.70684447704, 17971.4243926971, 7512.21138830519, 
19330.3468603148, 4458.61783312802, 11432.6404706726, 24656.1459780374, 
52524.978876548, 4933.55167452531, 10792.431696628, 31519.0778152806, 
5710.4451978927, 7394.0671124523, 19331.1910313936, 29202.1275852957, 
34989.6048109836, 21700.6971659423, 20344.6934766608, 25223.2209601062, 
32210.916157596, 20425.1667390753, 31990.4055072938, 10303.0519074308, 
13579.6935867213, 18470.724354459, 8081.36636860446), BA_95thru14_mm2 = c(4740.04634002735, 
28490.028172621, 27997.2429880936, 55112.0390582871, 27269.3294451689, 
51404.7947400297, 2273.91726244429, 6946.27671954662, 6407.33971367258, 
21356.7385496624, 4166.94646811615, 14773.5595314962, 3825.3588872101, 
14318.9577635937, 2535.89333865027, 7251.80786143471, 18995.9844461404, 
47198.4101565899, 2771.04844456734, 6794.24947471245, 25274.2196871474, 
2796.4306942262, 3428.30101220893, 15553.6019328705, 18429.9112362874, 
25781.6974753387, 15706.6985412864, 14790.3176255157, 14877.48038484, 
19672.7805789366, 12867.5381751576, 21446.4414468684, 7524.84918302853, 
9595.09411604172, 11102.2617687705, 4291.07679115986), BA_00thru14_mm2 = c(2668.97176894301, 
20957.1604073531, 22583.0193029237, 44065.3476653289, 22046.9060484913, 
42231.0690665709, 1676.88363814062, 5235.62447896369, 4398.9640151846, 
16670.8727260078, 2452.89855543217, 11330.6319456177, 583.376762896961, 
9263.89816022673, 913.416304739346, 3838.62931387087, 14006.0342876873, 
42521.6950917601, 819.828294146425, 2275.50594071647, 19359.5637568162, 
701.849592792751, 1359.72696666447, 11193.9304013406, 11317.4437752774, 
18190.5251810393, 10743.8303157992, 10742.7464663337, 7512.77441511578, 
11790.8703276434, 7618.23120253274, 14767.3494636164, 4860.00919904441, 
6318.53964422177, 6823.86677348072, 1638.34567393514), BA_05thru14_mm2 = c(1208.47399247252, 
15818.2262837189, 18911.4211191174, 34809.4601705281, 17218.2163290883, 
34546.9538971548, 1082.06713002035, 3497.33449484262, 2737.87164592433, 
12372.7144771307, 1568.68567918651, 7711.78904441141, 309.367959571136, 
6352.72038906271, 639.154383293422, 2846.52320059213, 11798.1159335768, 
33797.0001936679, 500.867418575699, 1658.15875632353, 13619.8306278706, 
351.531651566084, 457.927944832468, 6665.61686298025, 7822.17194021538, 
12104.9842818115, 7253.89335745231, 7342.93037838743, 3517.49728361725, 
7245.34692795678, 5273.63431945295, 9505.533501567, 3488.01046775825, 
4839.60067489241, 4142.03476620495, 463.990530645106), TotalCankers = c(61, 
65, 44, 31, 24, 23, 29, 37, 36, 9, 39, 24, 91, 89, 53, 61, 47, 
6, 51, 47, 39, 60, 90, 34, 34, 30, 47, 17, 62, 49, 61, 84, 59, 
84, 0, 65), WidthCankers_cm = c(131.5, 139.2, 95.6, 50, 51.2, 
76.4, 59.8, 38.8, 39.7, 13.6, 156.9, 24.3, 594.9, 136.6, 188.6, 
141.7, 275.8, 11.9, 118.8, 353, 53.7, 256.5, 347.3, 80.8, 49, 
28.2, 20.3, 5.5, 77.4, 56, 147.5, 155, 48.6, 117.4, 0, 79.1), 
    Treatment = c("T", "T", "T", "T", "T", "T", "U", "U", "U", 
    "U", "U", "U", "T", "T", "T", "T", "T", "T", "U", "U", "U", 
    "U", "U", "U", "T", "T", "T", "T", "T", "T", "U", "U", "U", 
    "U", "U", "U"), Site = c("M3", "M3", "M3", "M3", "M3", "M3", 
    "M3", "M3", "M3", "M3", "M3", "M3", "N1", "N1", "N1", "N1", 
    "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N2", "N2", 
    "N2", "N2", "N2", "N2", "N2", "N2", "N2", "N2", "N2", "N2"
    )), .Names = c("Unique_ID_Tree", "Disc_ID", "TotalBA_mm2", 
"BA_85thru14_mm2", "BA_90thru14_mm2", "BA_95thru14_mm2", "BA_00thru14_mm2", 
"BA_05thru14_mm2", "TotalCankers", "WidthCankers_cm", "Treatment", 
"Site"), row.names = c(NA, -36L), class = "data.frame")
2
Take the linetype out of aes. So, your geom_smooth command should look like geom_smooth(method = "lm", se=F, aes(fill=Site), linetype="dashed").A Gore
Great, that solved the first half of my problem and worked perfectly! Thanks.KKL234

2 Answers

4
votes

You can use aes(linetype=Treatment) on geom_smooth and add a scale_linetype_manual without the need to change your data. I also used group for Treatment on the global aes assuming this was the original intention:

ggplot(CanksVolumes, aes(TotalCankers, BA_05thru14_mm2, 
                         color=Site,group=Treatment)) + 
  scale_alpha_discrete(range = c(0.4, 1)) + 
  scale_color_brewer(palette = 'Set1') +
  scale_linetype_manual(values=c("solid","dashed"))+
  geom_point() + geom_smooth(method = "lm", se=F, aes(linetype=Treatment)) + 
  xlab("Total Number of Cankers per Tree") + 
  ylab("Tree Basal Area (mm^2) 2005-2014") +
  ggtitle("Total Number of Cankers Associated with Recent Growth (Basal Area Last 10 Years)") +
  facet_wrap(~Site)

.

1
votes
CanksVolumes %<>% mutate(Line = ifelse(Treatment == "T", "dashed", "solid"))

ggplot(CanksVolumes, aes(TotalCankers, BA_05thru14_mm2, 
                         color=Site, alpha=Treatment)) + 
  scale_alpha_discrete(range = c(0.4, 1)) + 
  scale_color_brewer(palette = 'Set1') +
  geom_point() + geom_smooth(method = "lm", se=F, aes(fill=Site, linetype=Line)) + 
  xlab("Total Number of Cankers per Tree") + 
  ylab("Tree Basal Area (mm^2) 2005-2014") +
  ggtitle("Total Number of Cankers Associated with Recent Growth (Basal Area Last 10 Years)") +
  facet_wrap(~Site)