使用三个不同的因变量序列通过ggplot绘制图例

I had previously posted where I was unable to plot a legend using one dependent variable and three different predictor variables using ggplot's geom_smooth function。我很幸运能够获得与此问题相关的指导。

我现在面临第二个难题。尝试创建带有图例的图例,该图例包括多条geom_smooth线,其中每条趋势线表示一组不同的预测变量 。当前(无传奇)代码如下:

png(filename="D:/Users/...",width = 10,height = 8,units = 'in',res = 300)
ggplot(Data) + 
  geom_smooth(aes(BAp1,BAgp1),colour="red",fill="red") + 
  geom_smooth(aes(BAp2,BAgp2),colour="turquoise",fill="turquoise") + 
  geom_smooth(aes(BAp3,BAgp3),colour="orange",fill="orange") + 
  xlab(bquote('Tree Basal Area ('~cm^2~')')) + 
  ylab(bquote('Predicted Basal Area Growth ('~cm^2~')')) + 
  labs(title = expression(paste("Other Softwoods")),subtitle = "Tree Level Basal Area Growth") +
  theme_bw()
dev.off()

使用三个不同的因变量序列通过ggplot绘制图例

以前的解决方案采用了ivot_longer函数,仅在所有三个回归都使用相同的依存关系的情况下才有效,因此不能在此处应用。

编辑

我现在提供了上图中使用的数据集的样本:

YEAR,SPC,BA,BAg,BAs,BAp1,BAgp1,BAp2,BAgp2,BAp3,BAgp3,2010,GS,203.58307,NA,0.002070873,292.55294,320.47389,271.71636,120.76281,191.13451,251.64941,323.6547,479.1636,221.67076,314.15927,143.13882,91.60885,235.0618,128.67964,116.89866,240.52819,181.45839,76.97687,147.41138,103.86891,160.60607,132.73229,153.93804,339.79464,56.74502,162.86015,151.74677,111.22023,229.65829,0.003241326,502.72548,615.75216,109.35884,289.5292,430.0526,198.5565,156.14502,213.82465,359.68093,295.59244,2.2698,352.98938,411.87064,96.76891,366.43538,254.469,494.80871,555.71634,383.59633,274.64591,263.02197,183.85386,369.83617,107.51315,419.09633,122.71846,283.52874,683.49275,510.70516,165.12996,2011,38.48451,0.001999849,84.94866,15.20531,65.03883,105.68318,21.23716,95.03318,43.0084,51.52998,19.63495,9.62113,8.55299,130.6981,93.31315,67.92909,47.78363,5.72555,62.21138,196.0668,9.0792,3.46361,98.52034,138.92909,52.81017,78.53982,23.75829,17.34944,124.68982,8.04248,72.3823,86.59015,34.21194,3.80133,0.002976025,134.78219,100.28749,186.26502,172.03362,169.71668,58.08805,80.11847,66.4761,59.44678,69.39778,118.82289,70.88218,89.92023,193.59279,0.001146144,25.51758,63.61725,15.90431,7.06858,27.33971,11.94591,35.25652,10.17876,24.63009,32.16991,22.06184,31.17245,16.61902,22.90221,20.42821,41.85387,18.85741,18.09558,37.39281,75.42964,45.3646,44.17865,0.0072219,301.71857,158.36768,346.36059,30.1907,40.71504,216.42433,201.06193,208.67242,145.26725,232.35221,188.69191,113.09734,2012,0.000270151,6.6052,2.83529,4.90874,6.15752,4.52389,4.15476,13.20254,13.85442,12.56637,0.785398,5.30929,10.7521,0.502655,14.52201,11.34115,0.000615944,1.13097,1.32732,7.54768,33.18307,1.53938,1991,0.000129404,2003,13.0847344,0.003090766,39.66762,4.5739326,113.00754,4.6844832,146.22059,7.29150748,2008,10.44108426,0.0042038,64.18895,6.1426749,136.55422,4.4894855,172.93962,3.02896885,219.03971,6.97630185,0.004530738,84.26219,6.812149,155.42866,4.50066,186.73834,2.38524049,1995,0.50069132,0.000749127,13.13855,2.8423224,1999,0.22776552,0.001892294,27.66004,4.1044984,8.18703,1.1184496,7.68315694,13.72853,2.9012578,18.99092631,28.50867,4.1791816,118.33232,7.1120902,21.83406834,46.02372,5.1020081,143.4212,5.8313954,208.2341,9.67759599,268.80252,14.04920425,73.47428,6.9080746,172.93508,5.6709952,249.09621,5.26243503,8.22901219,96.45316,7.7556394,199.6002,6.3364367,272.37363,3.97850066,9.80176885,16.27949,3.136689,15.53713858,32.10894,4.4623873,115.66877,8.1556236,19.08517739,50.94628,5.4811665,141.13239,5.764764,193.33334,9.47947965,14.12302886,80.73998,7.5382957,170.32979,5.5791972,229.21812,4.58140665,5.04422531,105.98191,8.5122454,196.69386,6.289291,251.65457,3.88456353,10.74424815,19.92864,3.452061,18.2899604,37.18107,4.8400702,133.76642,9.0765972,18.46667005,57.54763,5.9228014,163.02259,6.7293613,209.07802,10.52151769,14.63197192,89.87941,8.186619,197.06897,6.5385713,251.23609,5.2890248,298.64765,6.73282297,117.66054,9.4086648,227.67492,7.2823007,274.96065,4.30888721,7.80882155,15.28188367,105.35308,6.8327348,17.79908562,128.75935,5.5595442,178.05133,8.33464615,11.9883205,156.81984,5.3981216,209.81761,3.85660184,5.4977819,179.59762,5.4342788,228.50327,3.19001167,6.9684453,14.95397,3.0132536,88.24734,13.7660679,30.1993,4.2944465,94.23995,5.9926068,149.57123,15.3309716,48.24276,5.2335099,114.8419,4.7214463,157.03939,7.4681619,12.85068392,76.63461,7.1581349,138.51638,4.5167748,184.41302,3.12287756,4.63189082,100.51495,8.0424901,158.24583,4.5211926,200.3679,3.02820153,36.31681,7.64781507,15.60207,3.067957,14.24908404,31.18053,4.4020865,99.99643,6.6832802,15.70796701,49.63783,5.3747012,121.68928,4.8377575,164.0409,7.89588074,9.48761005,78.74908,7.3374128,145.96838,4.6370767,194.72718,3.74884103,1.27233773,103.23439,8.2732858,166.21787,4.6631889,212.9025,3.08333016,0.636172,3.76991102,8.82182,2.4178751,49.0167,8.09941903,21.4849,3.6096224,52.59159,3.5748876,7.38077895,36.46379,4.3377525,66.96562,3.4858304,82.72449,4.1846743,9.27751581,17.68187,3.2593534,11.85754958,34.09134,4.6271977,98.78823,7.1793815,141.02609,12.35431029,53.54448,5.660999,120.30488,4.7928505,148.48802,7.46192924,4.36681367,84.38349,7.808137,144.39893,4.5996242,174.95356,3.04325908,176.71459,3.46360806,110.81629,8.9793126,164.47864,4.6075064,188.82099,2.39053144,14.41991043,26.79089,4.0074362,19.13229823,46.38589,5.4484046,158.52939,11.1180098,246.05741,24.66150866,69.24519,6.6154131,194.1273,8.3284548,258.2328,12.17538864,280.55207,6.89893096,105.31282,9.0822677,233.17628,7.5382663,305.30992,6.08359541,4.52389625,136.40607,10.5118973,268.0019,8.2514072,334.70723,5.39582561,7.17657604,12.01707,2.7297405,16.14975116,26.02869,3.9618723,102.95209,6.183171,17.09026237,42.5563,4.7945855,125.4901,5.5013052,173.26155,8.13158982,8.70849093,68.28839,6.415111,152.01955,4.7109596,204.955,3.82817882,5.24646718,89.53456,7.2058142,174.53924,5.3586628,223.52047,3.16490979,39.59192,8.57065729,16.06924669,111.37113,7.5022219,
soyi8 回答:使用三个不同的因变量序列通过ggplot绘制图例

没有示例数据,则更难验证某件事是否有效。

也就是说,要在ggplot中获得图例,您需要在美学中映射一些内容。
在示例代码中,将颜色映射移至aes(),然后为色标提供标签:

ggplot(Data) + 
  geom_smooth(aes(BAp1,BAgp1,colour="red"),fill="red") + 
  geom_smooth(aes(BAp2,BAgp2,colour="turquoise"),fill="turquoise") + 
  geom_smooth(aes(BAp3,BAgp3,colour="orange"),fill="orange") + 
  xlab(bquote('Tree Basal Area ('~cm^2~')')) + 
  ylab(bquote('Predicted Basal Area Growth ('~cm^2~')')) + 
  scale_colour_manual(breaks = c("red","turquoise","orange"),labels = c("something","another","third"),values =  c("red","orange"))+
  labs(title = expression(paste("Other Softwoods")),subtitle = "Tree Level Basal Area Growth") +
  theme_bw()

现在,您提供了示例数据,我们可以建议一种在绘制之前重塑数据的方法。如果您要对多个绘图使用相同的数据集,则将获得回报。

new_d <- Data %>%
      gather(key= "group",value = "p",starts_with("BAp")) %>%
      gather(key = "group2",value = "gp",starts_with("BAg"))

# You could rename your groups at this point,or drop the second group variable.
# But at this point we can proceed with a more straight forward plotting script

ggplot(new_d)+
      geom_smooth(aes(x=p,y = gp,fill = group,color = group))+
      scale_color_manual(
            values =  c("red",aesthetics = c("fill","color")
      )+
      xlab(bquote('Tree Basal Area ('~cm^2~')')) + 
      ylab(bquote('Predicted Basal Area Growth ('~cm^2~')')) + 
      labs(title = expression(paste("Other Softwoods")),subtitle = "Tree Level Basal Area Growth") +
      theme_bw()
,

如布赖恩·费舍尔(Brian Fisher)所建议的那样,您需要在aes()内映射颜色以使图例自动显示。理想情况下,您将调整数据的形状,以便可以一次调用geom_smooth,但这也足够了。

d <- read.csv("tmp.csv")

ggplot(d) + 
  geom_smooth(aes(BAp1,color = "value 1",fill = "value 1")) + 
  geom_smooth(aes(BAp2,color = "value 2",fill = "value 2")) + 
  geom_smooth(aes(BAp3,color = "value 3",fill = "value 3")) +
  scale_color_manual(
    values = c("value 1" = "red","value 2" = "turquoise","value 3" = "orange"),name = "Scale name","color")
  ) +
  xlab(bquote('Tree Basal Area ('~cm^2~')')) + 
  ylab(bquote('Predicted Basal Area Growth ('~cm^2~')')) + 
  labs(title = expression(paste("Other Softwoods")),subtitle = "Tree Level Basal Area Growth") +
  theme_bw()

the plot

本文链接:https://www.f2er.com/3140767.html

大家都在问