So, I have the following data.frame, and I want to generate two plots in one graph for yval vs. xval, for each zval and type tp. The lef
> df xval yval se zval cond 1 1.0 1.831564e-02 1.831564e-03 0 a 2 1.2 2.705185e-02 2.705185e-03 0 a 3 1.4 3.916390e-02 3.916390e-03 0 a 4 1.6 5.557621e-02 5.557621e-03 0 a 5 1.8 7.730474e-02 7.730474e-03 0 a 6 2.0 1.053992e-01 1.053992e-02 0 a 7 2.2 1.408584e-01 1.408584e-02 0 a 8 2.4 1.845195e-01 1.845195e-02 0 a 9 2.6 2.369278e-01 2.369278e-02 0 a 10 2.8 2.981973e-01 2.981973e-02 0 a 11 3.0 3.678794e-01 3.678794e-02 0 a 12 3.2 4.448581e-01 4.448581e-02 0 a 13 3.4 5.272924e-01 5.272924e-02 0 a 14 3.6 6.126264e-01 6.126264e-02 0 a 15 3.8 6.976763e-01 6.976763e-02 0 a 16 4.0 7.788008e-01 7.788008e-02 0 a 17 4.2 8.521438e-01 8.521438e-02 0 a 18 4.4 9.139312e-01 9.139312e-02 0 a 19 4.6 9.607894e-01 9.607894e-02 0 a 20 4.8 9.900498e-01 9.900498e-02 0 a 21 5.0 1.000000e+00 1.000000e-01 0 a 22 5.2 9.900498e-01 9.900498e-02 0 a 23 5.4 9.607894e-01 9.607894e-02 0 a 24 5.6 9.139312e-01 9.139312e-02 0 a 25 5.8 8.521438e-01 8.521438e-02 0 a 26 6.0 7.788008e-01 7.788008e-02 0 a 27 6.2 6.976763e-01 6.976763e-02 0 a 28 6.4 6.126264e-01 6.126264e-02 0 a 29 6.6 5.272924e-01 5.272924e-02 0 a 30 6.8 4.448581e-01 4.448581e-02 0 a 31 7.0 3.678794e-01 3.678794e-02 0 a 32 7.2 2.981973e-01 2.981973e-02 0 a 33 7.4 2.369278e-01 2.369278e-02 0 a 34 7.6 1.845195e-01 1.845195e-02 0 a 35 7.8 1.408584e-01 1.408584e-02 0 a 36 8.0 1.053992e-01 1.053992e-02 0 a 37 8.2 7.730474e-02 7.730474e-03 0 a 38 8.4 5.557621e-02 5.557621e-03 0 a 39 8.6 3.916390e-02 3.916390e-03 0 a 40 8.8 2.705185e-02 2.705185e-03 0 a 41 9.0 1.831564e-02 1.831564e-03 0 a 42 9.2 1.215518e-02 1.215518e-03 0 a 43 9.4 7.907054e-03 7.907054e-04 0 a 44 9.6 5.041760e-03 5.041760e-04 0 a 45 9.8 3.151112e-03 3.151112e-04 0 a 46 10.0 1.930454e-03 1.930454e-04 0 a 47 1.0 3.726653e-06 7.453306e-07 0 b 48 1.2 9.929504e-06 1.985901e-06 0 b 49 1.4 2.541935e-05 5.083869e-06 0 b 50 1.6 6.252150e-05 1.250430e-05 0 b 51 1.8 1.477484e-04 2.954967e-05 0 b 52 2.0 3.354626e-04 6.709253e-05 0 b 53 2.2 7.318024e-04 1.463605e-04 0 b 54 2.4 1.533811e-03 3.067621e-04 0 b 55 2.6 3.088715e-03 6.177431e-04 0 b 56 2.8 5.976023e-03 1.195205e-03 0 b 57 3.0 1.110900e-02 2.221799e-03 0 b 58 3.2 1.984109e-02 3.968219e-03 0 b 59 3.4 3.404745e-02 6.809491e-03 0 b 60 3.6 5.613476e-02 1.122695e-02 0 b 61 3.8 8.892162e-02 1.778432e-02 0 b 62 4.0 1.353353e-01 2.706706e-02 0 b 63 4.2 1.978987e-01 3.957974e-02 0 b 64 4.4 2.780373e-01 5.560746e-02 0 b 65 4.6 3.753111e-01 7.506222e-02 0 b 66 4.8 4.867523e-01 9.735045e-02 0 b 67 5.0 6.065307e-01 1.213061e-01 0 b 68 5.2 7.261490e-01 1.452298e-01 0 b 69 5.4 8.352702e-01 1.670540e-01 0 b 70 5.6 9.231163e-01 1.846233e-01 0 b 71 5.8 9.801987e-01 1.960397e-01 0 b 72 6.0 1.000000e+00 2.000000e-01 0 b 73 6.2 9.801987e-01 1.960397e-01 0 b 74 6.4 9.231163e-01 1.846233e-01 0 b 75 6.6 8.352702e-01 1.670540e-01 0 b 76 6.8 7.261490e-01 1.452298e-01 0 b 77 7.0 6.065307e-01 1.213061e-01 0 b 78 7.2 4.867523e-01 9.735045e-02 0 b 79 7.4 3.753111e-01 7.506222e-02 0 b 80 7.6 2.780373e-01 5.560746e-02 0 b 81 7.8 1.978987e-01 3.957974e-02 0 b 82 8.0 1.353353e-01 2.706706e-02 0 b 83 8.2 8.892162e-02 1.778432e-02 0 b 84 8.4 5.613476e-02 1.122695e-02 0 b 85 8.6 3.404745e-02 6.809491e-03 0 b 86 8.8 1.984109e-02 3.968219e-03 0 b 87 9.0 1.110900e-02 2.221799e-03 0 b 88 9.2 5.976023e-03 1.195205e-03 0 b 89 9.4 3.088715e-03 6.177431e-04 0 b 90 9.6 1.533811e-03 3.067621e-04 0 b 91 9.8 7.318024e-04 1.463605e-04 0 b 92 10.0 3.354626e-04 6.709253e-05 0 b 93 1.0 6.065307e-01 1.819592e-01 1 a 94 1.2 7.261490e-01 2.178447e-01 1 a 95 1.4 8.352702e-01 2.505811e-01 1 a 96 1.6 9.231163e-01 2.769349e-01 1 a 97 1.8 9.801987e-01 2.940596e-01 1 a 98 2.0 1.000000e+00 3.000000e-01 1 a 99 2.2 9.801987e-01 2.940596e-01 1 a 100 2.4 9.231163e-01 2.769349e-01 1 a 101 2.6 8.352702e-01 2.505811e-01 1 a 102 2.8 7.261490e-01 2.178447e-01 1 a 103 3.0 6.065307e-01 1.819592e-01 1 a 104 3.2 4.867523e-01 1.460257e-01 1 a 105 3.4 3.753111e-01 1.125933e-01 1 a 106 3.6 2.780373e-01 8.341119e-02 1 a 107 3.8 1.978987e-01 5.936961e-02 1 a 108 4.0 1.353353e-01 4.060058e-02 1 a 109 4.2 8.892162e-02 2.667649e-02 1 a 110 4.4 5.613476e-02 1.684043e-02 1 a 111 4.6 3.404745e-02 1.021424e-02 1 a 112 4.8 1.984109e-02 5.952328e-03 1 a 113 5.0 1.110900e-02 3.332699e-03 1 a 114 5.2 5.976023e-03 1.792807e-03 1 a 115 5.4 3.088715e-03 9.266146e-04 1 a 116 5.6 1.533811e-03 4.601432e-04 1 a 117 5.8 7.318024e-04 2.195407e-04 1 a 118 6.0 3.354626e-04 1.006388e-04 1 a 119 6.2 1.477484e-04 4.432451e-05 1 a 120 6.4 6.252150e-05 1.875645e-05 1 a 121 6.6 2.541935e-05 7.625804e-06 1 a 122 6.8 9.929504e-06 2.978851e-06 1 a 123 7.0 3.726653e-06 1.117996e-06 1 a 124 7.2 1.343812e-06 4.031437e-07 1 a 125 7.4 4.655716e-07 1.396715e-07 1 a 126 7.6 1.549753e-07 4.649259e-08 1 a 127 7.8 4.956405e-08 1.486922e-08 1 a 128 8.0 1.522998e-08 4.568994e-09 1 a 129 8.2 4.496349e-09 1.348905e-09 1 a 130 8.4 1.275408e-09 3.826223e-10 1 a 131 8.6 3.475891e-10 1.042767e-10 1 a 132 8.8 9.101471e-11 2.730441e-11 1 a 133 9.0 2.289735e-11 6.869205e-12 1 a 134 9.2 5.534610e-12 1.660383e-12 1 a 135 9.4 1.285337e-12 3.856012e-13 1 a 136 9.6 2.867975e-13 8.603925e-14 1 a 137 9.8 6.148396e-14 1.844519e-14 1 a 138 10.0 1.266417e-14 3.799250e-15 1 a 139 1.0 2.096114e-01 1.676891e-02 1 b 140 1.2 2.664683e-01 2.131746e-02 1 b 141 1.4 3.320399e-01 2.656320e-02 1 b 142 1.6 4.055545e-01 3.244436e-02 1 b 143 1.8 4.855369e-01 3.884295e-02 1 b 144 2.0 5.697828e-01 4.558263e-02 1 b 145 2.2 6.554063e-01 5.243250e-02 1 b 146 2.4 7.389685e-01 5.911748e-02 1 b 147 2.6 8.166865e-01 6.533492e-02 1 b 148 2.8 8.847059e-01 7.077647e-02 1 b 149 3.0 9.394131e-01 7.515305e-02 1 b 150 3.2 9.777512e-01 7.822010e-02 1 b 151 3.4 9.975031e-01 7.980025e-02 1 b 152 3.6 9.975031e-01 7.980025e-02 1 b 153 3.8 9.777512e-01 7.822010e-02 1 b 154 4.0 9.394131e-01 7.515305e-02 1 b 155 4.2 8.847059e-01 7.077647e-02 1 b 156 4.4 8.166865e-01 6.533492e-02 1 b 157 4.6 7.389685e-01 5.911748e-02 1 b 158 4.8 6.554063e-01 5.243250e-02 1 b 159 5.0 5.697828e-01 4.558263e-02 1 b 160 5.2 4.855369e-01 3.884295e-02 1 b 161 5.4 4.055545e-01 3.244436e-02 1 b 162 5.6 3.320399e-01 2.656320e-02 1 b 163 5.8 2.664683e-01 2.131746e-02 1 b 164 6.0 2.096114e-01 1.676891e-02 1 b 165 6.2 1.616212e-01 1.292970e-02 1 b 166 6.4 1.221507e-01 9.772054e-03 1 b 167 6.6 9.049144e-02 7.239315e-03 1 b 168 6.8 6.571027e-02 5.256822e-03 1 b 169 7.0 4.677062e-02 3.741650e-03 1 b 170 7.2 3.263076e-02 2.610460e-03 1 b 171 7.4 2.231491e-02 1.785193e-03 1 b 172 7.6 1.495813e-02 1.196651e-03 1 b 173 7.8 9.828195e-03 7.862556e-04 1 b 174 8.0 6.329715e-03 5.063772e-04 1 b 175 8.2 3.995846e-03 3.196677e-04 1 b 176 8.4 2.472563e-03 1.978050e-04 1 b 177 8.6 1.499685e-03 1.199748e-04 1 b 178 8.8 8.915937e-04 7.132750e-05 1 b 179 9.0 5.195747e-04 4.156597e-05 1 b 180 9.2 2.967858e-04 2.374286e-05 1 b 181 9.4 1.661699e-04 1.329359e-05 1 b 182 9.6 9.119596e-05 7.295677e-06 1 b 183 9.8 4.905836e-05 3.924669e-06 1 b 184 10.0 2.586810e-05 2.069448e-06 1 b
I have used facet_grid to generate this plot, but there is one thing that I am trying to figure out. So, the right panel is for z=0, and the left is for z=1. I want to move the line legend to inside the left panel (for Z=1) (top corner). I couldn't find the option for that.
And here is my code that I used in R to generate the plot:
plot1 <- ggplot(data=df, aes(x=xval, y=yval, group=cond, colour=cond) ) + + geom_smooth(aes(ymin = yval-se, ymax = yval+se, linetype=cond, colour=cond, fill=cond), stat="identity", size=1.1) + + scale_colour_hue(l=25) + + ylim(-0.1,1.3) + scale_linetype_manual(values = c('a' = 1,'b' = 2)) plot1 + facet_grid(~ zval, scales="free_y") + theme(strip.text.x = element_blank(),strip.background = element_rect(colour="white", fill="white"))
You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.
In order to draw our legend outside of the plotting area, we can use a combination of the “topright” argument and an additional specification of inset. The “topright” argument specifies that the legend should be in the upper right corner of the graph.
plot1 <- ggplot(data=df, aes(x=xval, y=yval, group=cond, colour=cond) ) + geom_smooth(aes(ymin = yval-se, ymax = yval+se, linetype=cond, colour=cond, fill=cond), stat="identity", size=1.1) + scale_colour_hue(l=25) + ylim(-0.1,1.3) + scale_linetype_manual(values = c('a' = 1,'b' = 2))
The coordinates for legend.position are x- and y- offsets from the bottom-left of the plot, ranging from 0 - 1.
plot1 + facet_grid(~ zval, scales="free_y") + theme(strip.text.x = element_blank(), strip.background = element_rect(colour="white", fill="white"), legend.position=c(.9,.75) )
Tweak the legend.position values to suit your preference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With