Loop and plot rectangles with colors automatically - r

I have this dataset listed below that will be used for my question below.
Data<-read.table(file=file.choose(),header=T)
Data;
VARIABLE TYPE NGENES BETA BETA_STD SE P
black SET 43 -0.049246 -0.0078434 0.14654 0.63156
blue SET 152 -0.080217 -0.023193 0.08137 0.83781
brown SET 163 -0.057881 -0.017266 0.079054 0.76791
cyan SET 42 0.1498 0.023586 0.14128 0.1446
darkgreen SET 2 -0.65338 -0.022727 0.67635 0.83292
green SET 172 -0.13458 -0.041115 0.073527 0.96631
greenyellow SET 40 0.026733 0.0041104 0.14624 0.42749
grey SET 4 0.16388 0.0080567 0.53064 0.37874
grey60 SET 23 -0.1455 -0.017054 0.20066 0.76576
lightcyan SET 41 0.083008 0.012918 0.15225 0.29284
magenta SET 32 -0.10777 -0.014858 0.16601 0.74184
midnightblue SET 23 0.00024188 2.84E-05 0.19544 0.49951
pink SET 64 -0.017662 -0.0034093 0.12521 0.55608
purple SET 60 0.12025 0.022504 0.12624 0.17048
red SET 73 0.40737 0.083745 0.11427 0.00018742
royalblue SET 7 -0.27895 -0.018125 0.36009 0.78067
salmon SET 170 0.040831 0.01241 0.076001 0.29559
turquoise SET 450 0.027806 0.012383 0.050585 0.29131
With this dataset I am wanting to create several rectangles on a plot what are each color coded and have a pvalue labeled on top of the rectangle. I am wanting to loop through the VARIABLE column and for each rectangle assign a color. Furthermore, I want to loop through the P column and write the P value on top of each rectangle. Thus for each row in the dataset, the color and p value should be the same. This is the script I am trying right now. I am not seeing how to loop the associated columns with this script. Any help would be nice.
coords <- matrix(
c(100, 300, 110, 310,
120, 300, 130, 310,
140, 300, 150, 310,
160, 300, 170, 310,
180, 300, 190, 310,
100, 320, 110, 330,
120, 320, 130, 330,
140, 320, 150, 330,
160, 320, 170, 330,
180, 320, 190, 330,
100, 340, 110, 350,
120, 340, 130, 350,
140, 340, 150, 350,
160, 340, 170, 350,
180, 340, 190, 350,
100, 360, 110, 370,
120, 360, 130, 370,
140, 360, 150, 370),
ncol=4,byrow=TRUE)
plot(c(100, 200), c(300, 450), type = "n",
main = "Test")
rfun <- function(x,i) {
do.call(rect,as.list(x))
}
apply(coords,1,rfun)
text((coords[,1]+coords[,3])/2,
(coords[,2]+coords[,4])/2,
seq(nrow(coords)))

I am not sure, but maybe you want something like this?
DF <- structure(list(VARIABLE = c("black", "blue", "brown", "cyan",
"darkgreen", "green", "greenyellow", "grey", "grey60", "lightcyan",
"magenta", "midnightblue", "pink", "purple", "red", "royalblue",
"salmon", "turquoise"),
TYPE = c("SET", "SET", "SET", "SET",
"SET", "SET", "SET", "SET", "SET", "SET", "SET", "SET", "SET",
"SET", "SET", "SET", "SET", "SET"),
NGENES = c(43L, 152L, 163L, 42L, 2L, 172L, 40L, 4L, 23L, 41L, 32L, 23L,
64L, 60L, 73L, 7L, 170L, 450L),
BETA = c(-0.049246, -0.080217, -0.057881, 0.1498, -0.65338, -0.13458,
0.026733, 0.16388, -0.1455, 0.083008, -0.10777, 0.00024188,
-0.017662, 0.12025, 0.40737, -0.27895, 0.040831, 0.027806),
BETA_STD = c(-0.0078434, -0.023193, -0.017266, 0.023586, -0.022727,
-0.041115, 0.0041104, 0.0080567, -0.017054, 0.012918,
-0.014858, 2.84e-05, -0.0034093, 0.022504, 0.083745,
-0.018125, 0.01241, 0.012383),
SE = c(0.14654, 0.08137, 0.079054, 0.14128, 0.67635, 0.073527, 0.14624,
0.53064, 0.20066, 0.15225, 0.16601, 0.19544, 0.12521, 0.12624,
0.11427, 0.36009, 0.076001, 0.050585),
P = c(0.63156, 0.83781, 0.76791, 0.1446, 0.83292, 0.96631, 0.42749,
0.37874, 0.76576, 0.29284, 0.74184, 0.49951, 0.55608, 0.17048,
0.00018742, 0.78067, 0.29559, 0.29131)),
class = "data.frame",
row.names = c(NA, -18L))
coords <- matrix(
c(100, 300, 110, 310,
120, 300, 130, 310,
140, 300, 150, 310,
160, 300, 170, 310,
180, 300, 190, 310,
100, 320, 110, 330,
120, 320, 130, 330,
140, 320, 150, 330,
160, 320, 170, 330,
180, 320, 190, 330,
100, 340, 110, 350,
120, 340, 130, 350,
140, 340, 150, 350,
160, 340, 170, 350,
180, 340, 190, 350,
100, 360, 110, 370,
120, 360, 130, 370,
140, 360, 150, 370),
ncol=4,byrow=TRUE)
rfun <- function(x, i) do.call(rect, c(as.list(x), border = i))
plot(c(100, 200), c(300, 450), type = "n",
main = "Test")
invisible(sapply(seq_len(nrow(DF)),
function(y) do.call(rect, c(as.list(coords[y,]), border = DF$VARIABLE[y]))))
text((coords[,1]+coords[,3])/2,
(coords[,2]+coords[,4])/2,
round(DF$P, 2))
Created on 2020-08-04 by the reprex package (v0.3.0)

Related

Change the color of part of line in ggplot in R

I would like to change part of a line in a time series in ggplot.
Desired output
I just want a section of the line to be red (I drew this on so ignore that you can still see black).
Current output
Current code
fin_plot <- ggplot(data, aes(x = `Distance`)) +
geom_line(aes(y = Mn), size = 1.5, alpha = 1, color = "black", linetype = "solid" ) +
theme_bw() + labs(y="", x= "") +
theme_classic() + theme(text=element_text(size=36, family="serif", face = "bold", color = "black")) +
scale_x_continuous(limits = c(0, 450),labels = scales::number_format(accuracy = 1)) + theme(axis.line = element_line(colour = 'black', size = 1.5)) + theme(axis.ticks = element_line(colour = "black", size = 1.5)) + scale_y_continuous(limits = c(0, 3), labels = scales::number_format(accuracy = 0.1))+ theme(axis.ticks.length = unit(.3, "cm")) + coord_capped_cart(bottom='right', left='none', gap = 0.15)
fin_plot
data <- structure(list(Mg = c(0.903247645, 0.912560748, 0.896003508,
0.909572697, 0.883631829, 0.905722594, 0.892465355, 0.909271173,
0.880506202, 0.889278401, 0.878534542, 0.959209459, 0.913303825,
0.929893977, 0.97778374, 0.9885554, 0.929716333, 1.028422583,
1.025638955, 1.011352651, 1.041343955, 1.092562951, 1.129761801,
1.088857171, 1.107257284, 1.116728405, 1.103053734, 1.041662037,
1.134182243, 1.104550315, 1.086952767, 1.106004784, 1.057688595,
1.034347579, 1.04641385, 1.139270945, 1.048446018, 1.033827731,
1.075554754, 1.029893202, 1.074749532, 1.001626205, 0.977053541,
0.987467665, 0.999540478, 0.945184816, 0.959677178, 0.962807712,
0.967023936, 1.024286493, 0.881264816, 0.967181342, 1.000316876,
0.956168258, 1.003214572, 1.00047837, 0.940103474, 0.929875987,
0.928227112, 0.982410241, 0.983035162, 0.976666772, 1.019755049,
1.075189042, 0.975380543, 0.981316782, 0.986876269, 1.026690916,
1.052379934, 1.001547298, 0.979888683, 1.008209647, 0.976098272,
0.944479556, 0.996767684, 1.018077758, 1.028862706, 1.08510417,
1.08963868, 1.048481179, 1.139954126, 1.107066353, 1.122920581,
1.23904326, 1.19449336, 1.179971969, 1.165865352, 1.068804094,
1.099436469, 1.073307737, 1.07045113, 1.101007051, 1.011962649,
1.11202545, 1.097883672, 1.05361424, 0.993283703, 1.046635444,
1.04951188, 1.055736151, 1.063705172, 0.977095039, 1.015650848,
1.029367222, 1.003814349, 0.973376993, 1.021665177, 0.925511352,
1.014703757, 0.933654542, 1.027336075, 0.961163947, 1.022921765,
0.910164297, 0.937410814, 0.935246588, 0.925900983, 0.934477753,
0.927973832, 0.946372309, 0.950554394, 0.9386026, 1.000712639,
0.947846812, 0.953585987, 0.967735737, 0.927914753, 0.943303715,
0.935435884, 0.987648375, 0.902379461, 0.939086878, 1.018529942,
0.973874968, 0.974093087, 0.984149676, 0.948669001, 0.934863295,
1.011232041, 0.942884239, 0.978044788, 1.023700208, 1.011714275,
0.999153709, 1.06822476, 0.967735328, 1.131133479, 1.011068503,
1.034903609, 1.078701437, 1.049655794, 1.097777577, 1.06172881,
1.197399846, 1.079896085, 1.186101797, 1.199388268, 1.15701997,
1.089004764, 1.115041506, 1.154124932, 1.133278285, 1.096056948,
1.131271873, 1.116672802, 1.078030317, 1.040967817, 1.053747728,
1.100372522, 1.133677114, 1.223971358, 1.117169754, 1.084546095,
1.047035909, 1.030681048, 1.076345258, 1.122537084, 1.072586737,
1.047181662, 1.039218365, 1.068698816, 1.128110676, 1.006461218,
1.093800528, 1.052872185, 1.048359752, 1.07256098, 1.011260865,
1.071809317, 1.037520223, 1.074106418, 1.05109023, 1.03030123,
1.044579971, 0.997926967, 1.00759045, 1.025063505, 1.028076086,
0.9757945), Mn = c(0.086720869, 0.113119382, 0.088197332, 0.081547788,
0.079373211, 0.07888827, 0.072865285, 0.079637996, 0.066314774,
0.097585729, 0.185034982, 0.214466904, 0.294317625, 0.481389256,
0.531196058, 0.715842439, 0.865098887, 0.987242052, 1.081028291,
1.240920518, 1.313524957, 1.543771699, 1.78495042, 1.746572555,
2.048760527, 2.101438775, 1.967474033, 2.000286925, 2.014020838,
1.924470659, 1.75696549, 1.786681246, 1.633290961, 1.455799758,
1.315346538, 1.435348984, 1.27887702, 1.152818928, 1.095127218,
0.987502349, 1.062278922, 0.898540082, 0.83617998, 0.889057689,
0.825563648, 0.788347646, 0.790973555, 0.775541228, 0.815063004,
0.848723108, 0.66783059, 0.672629631, 0.747809615, 0.72338158,
0.666220438, 0.664051795, 0.597260657, 0.689282162, 0.663808452,
0.678551141, 0.672917354, 0.686199986, 0.724202364, 0.746195474,
0.686135659, 0.654148537, 0.713488795, 0.72446665, 0.699529989,
0.630120423, 0.661767463, 0.663290351, 0.705879842, 0.709399338,
0.76228353, 0.714368918, 0.720561695, 0.837036666, 0.923882149,
1.014163852, 1.221410703, 1.315825246, 1.368054705, 1.641746627,
1.630198312, 1.698589629, 1.562956393, 1.427322658, 1.53964983,
1.574583495, 1.527101216, 1.380123116, 1.28649445, 1.29251968,
1.330565441, 1.317758525, 1.19292313, 1.217953538, 1.218591815,
1.163372928, 1.091026791, 0.878691182, 0.903966928, 0.917620557,
0.838430901, 0.825709255, 0.839298558, 0.76309434, 0.97617394,
0.739885015, 0.822159341, 0.785335779, 0.771926988, 0.766619321,
0.832448556, 0.733734124, 0.787221188, 0.685452005, 0.740552711,
0.707414697, 0.781271754, 0.72652958, 0.729470139, 0.71649368,
0.681176551, 0.683977986, 0.711079301, 0.681092777, 0.747615639,
0.700953146, 0.692246657, 0.673560118, 0.820384633, 0.740567172,
0.72070082, 0.795192662, 0.773897168, 0.74552279, 0.735710787,
0.768825863, 0.746016457, 0.736542042, 0.744507532, 0.784312542,
0.758393534, 0.7600356, 0.797384742, 0.773626898, 0.744557896,
0.855000158, 0.867564728, 0.870754116, 0.891695067, 1.050643841,
1.086358398, 1.217041501, 1.373025781, 1.492662215, 1.563662984,
1.834762408, 1.877449635, 1.766642359, 1.731492777, 1.728551719,
1.947777721, 1.951293228, 1.918622452, 1.979387861, 1.922875966,
2.150308307, 1.863476368, 1.836654797, 1.841008541, 1.517006974,
1.721636892, 1.537470714, 1.590262156, 1.505842124, 1.398523427,
1.180718089, 1.330067785, 1.264569656, 1.174501376, 1.116396782,
1.205453294, 1.193128576, 1.199711798, 1.18175828, 1.14051266,
1.107141774, 1.028976851, 1.101895442, 0.933531591, 0.985243449,
0.883647299, 0.871531516, 0.791794339, 0.82156345, 0.734425258
), Zn = c(0.746612627, 0.818368055, 0.696689824, 0.748702805,
0.717457681, 0.766243608, 0.805305259, 0.855909762, 0.803357905,
0.889646097, 0.854456208, 1.067795473, 1.051422575, 1.17061972,
1.138440648, 1.052796919, 1.040998633, 1.161739158, 1.025956799,
0.971567748, 1.072911493, 0.952121155, 1.040392714, 1.069745522,
1.068549198, 1.090194087, 1.214584829, 1.157485471, 1.245813376,
1.336359991, 1.204038397, 1.126255292, 1.131057736, 0.922042386,
1.037566449, 1.100852394, 1.121842367, 0.998657748, 1.006938923,
1.002800377, 0.897387497, 0.93902937, 0.889327622, 0.802133735,
0.855245047, 0.860702407, 0.704324249, 0.905827093, 0.760155095,
0.760247698, 0.655991619, 0.677006743, 0.668001976, 0.623410532,
0.569302474, 0.523713794, 0.690042836, 0.539115342, 0.528696218,
0.57851915, 0.60294784, 0.581392042, 0.65277069, 0.65620614,
0.625397246, 0.697647782, 0.6180657, 0.632326126, 0.684659215,
0.606197513, 0.630134281, 0.637151517, 0.574538208, 0.605993607,
0.533522181, 0.544522236, 0.577535469, 0.573427383, 0.672984155,
0.735286828, 0.7532343, 0.881292245, 0.801132661, 1.122761046,
1.137397845, 1.173190388, 1.138033979, 1.126494557, 1.144871399,
1.087042815, 0.981750792, 0.992888445, 0.955352455, 1.074357698,
1.027127808, 1.083248059, 1.010304962, 1.037776316, 1.052809984,
0.959161909, 0.939369893, 0.932304641, 0.912110856, 1.035278327,
0.825391661, 0.883818816, 0.880397247, 0.775385156, 0.860535004,
0.75878312, 0.764243502, 0.788209749, 0.736029937, 0.746966542,
0.762295984, 0.804665042, 0.797845669, 0.744225613, 0.846139103,
0.806957411, 0.789078125, 0.912631032, 0.926629248, 0.807376002,
0.795165332, 0.776764645, 0.811532921, 0.740169463, 0.707007363,
0.764252403, 0.754265833, 0.656183602, 0.78602999, 0.734580057,
0.756587437, 0.750509131, 0.727536118, 0.676232276, 0.714439923,
0.720668076, 0.763533465, 0.60234143, 0.651920197, 0.744086872,
0.633919728, 0.615213712, 0.705944962, 0.667362984, 0.742636421,
0.748062261, 0.718290208, 0.866047893, 0.754624731, 0.753850346,
0.723216532, 0.874448292, 0.967358157, 1.102255319, 1.225145961,
1.375482642, 1.508302364, 1.452575717, 1.456946995, 1.477964732,
1.711047017, 1.662721246, 1.539275314, 1.595349018, 1.699728078,
1.849605828, 1.688668818, 1.819401302, 1.972652441, 1.876873848,
1.920264068, 2.061536452, 2.00629895, 1.959281378, 1.956005981,
1.841888926, 1.929803629, 1.669570099, 1.596624552, 1.654602575,
1.433160972, 1.487724776, 1.457591295, 1.558387099, 1.505276849,
1.389005594, 1.538544454, 1.296637953, 1.343107585, 1.27500613,
1.181323757, 1.181523008, 1.309586833, 1.148849891, 1.129687476
), Ba = c(0.742734852, 0.839492568, 0.743899849, 0.817080816,
0.773569657, 0.735728339, 0.715168283, 0.78077814, 0.694280484,
0.773303425, 0.768041196, 0.883401699, 0.818274274, 0.715927964,
0.696938222, 0.832246446, 0.73089346, 0.790965216, 0.799717389,
0.865896893, 0.946771069, 0.954212275, 1.023740345, 1.027036123,
1.086336263, 1.064542815, 0.9463809, 0.924081609, 0.999832641,
0.911277648, 0.922871168, 0.953134033, 0.786732115, 0.802026729,
0.832863371, 0.863952475, 0.817833153, 0.748586924, 0.72095701,
0.738213943, 0.672736744, 0.704947698, 0.531743532, 0.634123809,
0.683548549, 0.733277161, 0.608993729, 0.752162246, 0.568705823,
0.643172511, 0.597251486, 0.655514695, 0.583437677, 0.557676441,
0.646713866, 0.527005047, 0.578023512, 0.576281064, 0.600923204,
0.578475648, 0.551957027, 0.585007991, 0.623858699, 0.630936819,
0.636198589, 0.565476603, 0.658861425, 0.577557604, 0.629178306,
0.646092809, 0.566079299, 0.60953767, 0.680135261, 0.500802233,
0.704656678, 0.61109605, 0.645344144, 0.667139888, 0.734969576,
0.780062983, 0.783090234, 0.83005691, 0.905356723, 0.933746319,
0.947613375, 0.923115827, 0.873482691, 0.746883952, 0.850273618,
0.795256154, 0.800825928, 0.772630039, 0.749567395, 0.7823457,
0.772609842, 0.736269985, 0.699705666, 0.716860238, 0.65909369,
0.806743181, 0.604632102, 0.629103485, 0.669824708, 0.545219042,
0.605081484, 0.545598194, 0.612458887, 0.640840679, 0.568115521,
0.578270006, 0.642784637, 0.486235168, 0.608704086, 0.449107996,
0.603056279, 0.573624703, 0.527880861, 0.479058818, 0.608581986,
0.497792884, 0.736359035, 0.560758315, 0.59150912, 0.491623628,
0.646548159, 0.559243084, 0.554057512, 0.542344646, 0.583808567,
0.623315676, 0.521008383, 0.511710892, 0.633820855, 0.529775704,
0.590383598, 0.500021436, 0.602344336, 0.499887402, 0.534870849,
0.583225149, 0.623554367, 0.62596102, 0.585378422, 0.648988779,
0.577416685, 0.632021029, 0.644454559, 0.684966009, 0.595845502,
0.738800574, 0.699692813, 0.735600272, 0.916889652, 0.957440119,
0.940642207, 0.982945679, 1.040869211, 0.968988195, 1.202672647,
1.125246185, 1.072149728, 1.163386327, 1.082737071, 0.957718286,
1.073014525, 1.200702065, 1.062026456, 1.027530031, 1.066130572,
1.039337914, 0.970167423, 1.134895746, 1.013487628, 0.824278282,
0.843864753, 1.05735407, 1.112876459, 0.962913956, 0.90808515,
0.871953689, 0.993060229, 0.869462576, 0.949186443, 0.989669433,
0.764506726, 0.755280383, 0.870762986, 0.941377101, 0.837105653,
0.946608575, 0.921411019, 0.937765975, 0.908506991, 0.810663688,
0.848783174, 0.856162412, 0.913266132, 0.848558712, 0.878929447
), All = c(2.479315993, 2.683540753, 2.424790513, 2.556904106,
2.454032378, 2.486582811, 2.485804182, 2.625597071, 2.444459365,
2.649813652, 2.686066928, 3.124873535, 3.077318299, 3.297830917,
3.344358668, 3.589441204, 3.566707313, 3.968369009, 3.932341434,
4.08973781, 4.374551474, 4.54266808, 4.97884528, 4.932211371,
5.310903272, 5.372904082, 5.231493496, 5.123516042, 5.393849098,
5.276658613, 4.970827822, 4.972075355, 4.608769407, 4.214216452,
4.232190208, 4.539424798, 4.266998558, 3.933891331, 3.898577905,
3.758409871, 3.707152695, 3.544143355, 3.234304675, 3.312782898,
3.363897722, 3.32751203, 3.063968711, 3.396338279, 3.110947858,
3.27642981, 2.802338511, 2.972332411, 2.999566144, 2.860636811,
2.88545135, 2.715249006, 2.805430479, 2.734554555, 2.721654986,
2.81795618, 2.810857383, 2.829266791, 3.020586802, 3.108527475,
2.923112037, 2.898589704, 2.977292189, 2.961041296, 3.065747444,
2.883958043, 2.837869726, 2.918189185, 2.936651583, 2.760674734,
2.997230073, 2.888064962, 2.972304014, 3.162708107, 3.42147456,
3.577994842, 3.897689363, 4.134240754, 4.19746467, 4.937297252,
4.909702892, 4.974867813, 4.740338415, 4.369505261, 4.634231316,
4.530190201, 4.380129066, 4.246648651, 4.003376949, 4.261248528,
4.228186763, 4.190890809, 3.896217461, 4.019225536, 3.980007369,
3.985014169, 3.698733958, 3.417194347, 3.50155334, 3.527485148,
3.272718395, 3.228503258, 3.353819869, 3.104831527, 3.419528222,
3.010592683, 3.256523555, 3.020944643, 3.139582776, 2.872858156,
3.135211633, 3.047270457, 3.038848701, 2.843214189, 3.123247632,
2.958537301, 3.257263308, 3.138521527, 3.248321146, 2.963340122,
3.076476029, 2.987721452, 3.004584487, 2.906910601, 2.973867453,
3.0761696, 2.869900334, 2.78054149, 3.25876542, 2.978797901,
3.041764942, 3.029872905, 3.052446623, 2.856505763, 2.9962536,
3.015603327, 3.111149077, 2.9885447, 2.993520426, 3.176541902,
3.037954707, 2.975005669, 3.278917742, 3.137024394, 3.117943428,
3.42056443, 3.335203543, 3.570179858, 3.62493826, 3.959334152,
3.830113222, 4.260537269, 4.580641417, 4.720925699, 5.080486356,
5.450532741, 5.612026659, 5.515882688, 5.367233791, 5.29550661,
5.848512065, 5.892746856, 5.560892039, 5.656014638, 5.789107138,
6.172929163, 5.746283967, 5.908121599, 5.911694705, 5.265195013,
5.516446761, 5.732706494, 5.831974649, 5.500624195, 5.30979622,
4.933779069, 5.321630459, 4.931713007, 4.726773589, 4.854469318,
4.455993177, 4.484493487, 4.600627059, 4.692783345, 4.554704479,
4.480276166, 4.563038742, 4.3873896, 4.215447397, 4.115493238,
3.911681197, 3.916807386, 4.039710809, 3.847048139, 3.718836681
), Distance = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173,
174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186,
187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199
)), row.names = c(NA, -199L), class = c("tbl_df", "tbl", "data.frame"
), na.action = structure(c(`18435` = 18435L, `18457` = 18457L,
`18458` = 18458L, `18459` = 18459L, `18460` = 18460L, `18515` = 18515L,
`18868` = 18868L, `18869` = 18869L, `18870` = 18870L, `19173` = 19173L,
`19174` = 19174L, `19293` = 19293L, `19417` = 19417L, `19418` = 19418L,
`19419` = 19419L, `19420` = 19420L, `20062` = 20062L, `20063` = 20063L,
`20064` = 20064L, `20065` = 20065L, `20066` = 20066L, `20237` = 20237L,
`20238` = 20238L, `20239` = 20239L), class = "omit"))
A simple approach would be to use a second geom_line for which you use only the data you want to highlight:
library(ggplot2)
ggplot(data, aes(x = Distance)) +
geom_line(aes(y = Mn), size = 1.5, alpha = 1, color = "black", linetype = "solid") +
geom_line(data = ~subset(.x, Distance > 40 & Distance < 60), aes(y = Mn), size = 1.5, alpha = 1, color = "red", linetype = "solid") +
labs(y = "", x = "") +
theme_classic() +
theme(text = element_text(size = 36, family = "serif", face = "bold", color = "black")) +
scale_x_continuous(labels = scales::number_format(accuracy = 1)) +
theme(axis.line = element_line(colour = "black", size = 1.5)) +
theme(axis.ticks = element_line(colour = "black", size = 1.5)) +
scale_y_continuous(limits = c(0, 3), labels = scales::number_format(accuracy = 0.1)) +
theme(axis.ticks.length = unit(.3, "cm"))

How to create a factor variable from a data.frame and plot the columns on a side-by-side plot

Some of you this could be an easy question.
I have 2 data frames:
dput(head(Activitieslessthan35))
structure(list(`Main job: Working time in main job` = c(470,
440, 430, 430, 410, 150), Sleep = c(420, 450, 450, 420, 450,
460), `Unspecified TV video or DVD watching` = c(60, 40, 210,
190, 60, 0), Eating = c(80, 60, 40, 70, 60, 130), `Other personal care:Wash and dress` = c(60,
60, 50, 50, 70, 50), `Travel to work from home and back only` = c(60,
60, 50, 90, 90, 30), `Unspecified radio listening` = c(140, 180,
50, 90, 140, 160), `Other specified social life` = c(350, 270,
310, 330, 710, 440), `Socialising with family` = c(350, 270,
360, 330, 730, 540), `Food preparation and baking` = c(410, 310,
420, 380, 1000, 950)), row.names = c(NA, 6L), class = "data.frame")
and
dput(head(ActivitiesMoreOrEqual35))
structure(list(`Main job: Working time in main job` = c(360,
420, 390, 490, 540, 390), Sleep = c(590, 480, 310, 560, 280,
370), `Unspecified TV video or DVD watching` = c(100, 60, 130,
120, 60, 30), Eating = c(70, 100, 70, 40, 190, 80), `Other personal care:Wash and dress` = c(10,
30, 100, 60, 270, 90), `Travel to work from home and back only` = c(0,
50, 260, 50, 0, 0), `Unspecified radio listening` = c(50, 80,
260, 80, 210, 200), `Other specified social life` = c(190, 320,
790, 250, 580, 420), `Travel in the course of work` = c(50, 80,
260, 70, 120, 200), `Food preparation and baking` = c(440, 570,
820, 570, 820, 590)), row.names = c(NA, 6L), class = "data.frame")
I would like to convert the data.frames into factors - for example to have a factor variable called Activitieslessthan35 with colums of the data frame to be used as levels such as `Main job: Working time in main job', 'Sleep', etc. Later I would like also to plot (the sum) the levels of the factors on a side-by-side bar plot.
I don't know if you care transform a data.frame into factor variable as well how to change the format of the data.frames to create the plot
Any suggestion is welcome
If I understand well, you want to have both of your dataframe in a long format of two columns, one column containing all colnames of your dataframe, and the second column with all values, then summarise each "factor" of the first column, merging both dataframes and plotting both dataframes into a single plot. Am I right ?
Here a way to do it. I called df the dataframe Activitieslessthan35 and df2 the
dataframe ActivitiesMoreOrEqual35.
First, we are going to transpose to a long format each of your dataframe using pivot_longer
library(tidyr)
library(dplyr)
df <- df %>% pivot_longer(everything(), names_to = "Activities", values_to = "Values_less_than35")
df2 <- df2 %>% pivot_longer(everything(),names_to = "Activities", values_to = "Values_More_than_35")
Then, we will calculate the sum value for each factor of each of your dataframe:
df_sum = df%>% group_by(Activities) %>% summarise(Values_less_than35 = sum(Values_less_than35))
df2_sum = df2 %>% group_by(Activities) %>% summarise(Values_More_than_35 = sum(Values_More_than_35))
Then, we are merging both dataframe into a singe one by using "Activities" as merging columns
final_df = merge(df_sum,df2_sum, by.x = "Activities", by.y = "Activities", all = TRUE)
Finally, we are transposing one last time final_df in order to have values in the correct shape for plotting them with ggplot2
final_df <- final_df %>% pivot_longer(., -Activities, names_to = "Variable", values_to = "Value")
And now we can plot your final dataframe using ggplot2
library(ggplot2)
ggplot(final_df, aes(x = stringr::str_wrap(Activities, 15), y = Value, fill = Variable)) +
geom_col(stat = "identity", position = position_dodge()) +
coord_flip()+
xlab("")
And you get the following plot:
Does it look what you are expecting ?

gvisMotionChart blank when animated

Problem:
My gvisMotionChart works fine when paused, but is blank when animated.
Code:
Motion=gvisMotionChart(mydat,
idvar="Time_Period_Year_Cd",
timevar="Year",
xvar="Total_Customers",
yvar="Percent_Happy",
colorvar = "New_Product_Count",
sizevar = "Group_A_Count"
)
plot(Motion)
Result (when paused):
Result (when animated):
I imagine it must be something with my code or data, because I was able to get the example in the documentation to work properly. I tried several variations, like not specifying the xvar then plotting "year" as the x-axis, etc.
Data:
mydat <- structure(list(Time_Period_Year_Cd = c(201220L, 201320L, 201340L,
201360L, 201420L, 201440L, 201460L, 201480L, 201520L, 201540L,
201560L, 201580L, 201620L, 201640L, 201660L, 201680L, 201720L
), New_Product_Count = c(1606L, 1834L, 1205L, 1204L, 1645L, 704L,
651L, 473L, 692L, 559L, 535L, 531L, 911L, 663L, 599L, 702L, 512L
), Group_A_Count = c(616, 670, 512, 520, 594, 265, 215, 148,
235, 171, 160, 166, 231, 220, 148, 138, 101), Group_B_Count = c(267,
288, 177, 194, 320, 122, 156, 103, 121, 108, 105, 105, 187, 146,
134, 152, 103), Group_C_Count = c(365, 420, 293, 269, 373, 172,
151, 120, 192, 132, 135, 148, 225, 150, 191, 205, 177), Group_D_Count = c(333,
429, 202, 204, 335, 132, 121, 97, 133, 143, 131, 107, 264, 139,
119, 196, 129), Number_Bought_Per_Customer = c(5.46637608966376,
6.4432933478735, 6.79668049792531, 7.04734219269103, 7.2468085106383,
7.41193181818182, 7.44086021505376, 6.48625792811839, 6.91329479768786,
7.16994633273703, 6.49906542056075, 5.30885122410546, 4.78155872667398,
4.09049773755656, 3.80801335559265, 3.04415954415954, 2.826171875
), Total_Customers = c(5038L, 5940L, 5557L, 5472L, 6052L, 5164L,
4544L, 3954L, 4473L, 4948L, 3884L, 3723L, 4011L, 4303L, 3413L,
3421L, 2964L), Percent_Happy = c(0.797988105101756, 0.83794901700776,
0.773512106024391, 0.775157532067893, 0.834237370911927, 0.8306291015089,
0.820150552373225, 0.824696031621165, 0.776615269241095, 0.848073917652629,
0.841092657179119, 0.781823675677749, 0.840457049668049, 0.763698900181159,
0.872781703430453, 0.896473511416122, 0.787873482140602), Year = c(2012,
2013, 2013, 2013, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015,
2016, 2016, 2016, 2016, 2017)), .Names = c("Time_Period_Year_Cd",
"New_Product_Count", "Group_A_Count", "Group_B_Count", "Group_C_Count",
"Group_D_Count", "Number_Bought_Per_Customer", "Total_Customers",
"Percent_Happy", "Year"), row.names = c(NA, 17L), class = "data.frame")
Each idvar value has only one timevar value:
mydat %>% count(Time_Period_Year_Cd) %>% head
# Time_Period_Year_Cd n
# <int> <int>
# 1 201220 1
# 2 201320 1
# 3 201340 1
# 4 201360 1
# 5 201420 1
# 6 201440 1
So there cannot be any transition. In contrast to e.g.
df <- mydat %>% mutate(idvar = substr(Time_Period_Year_Cd, 1, 4)) %>% group_by(idvar) %>% mutate(timevar = 1:n()) %>% ungroup
Motion=gvisMotionChart(df,
idvar="idvar",
timevar="timevar",
xvar="Total_Customers",
yvar="Percent_Happy",
colorvar = "New_Product_Count",
sizevar = "Group_A_Count"
)
plot(Motion)

Subsetting a vector base on another vector

I have 2 input vectors, iv1 and iv2, as shown below. I would like to separate the elements of the second vector according to elements of the first. It works like this: The values in iv2 between the first 2 values of iv1 are stored in ov1, the values in iv2 between the second and third values of iv1 are stored in ov2, and so on. Note: The values in iv1 and iv2 are already in ascending order. Any thoughts please?
Input:
iv1 <- c(100, 200, 300, 400, 435)
iv2 <- c(60, 120, 140, 160, 180, 230, 250, 255, 265, 270, 295, 340, 355, 401, 422, 424, 430)
Desired output:
ov1 = c(120, 140, 160, 180)
ov2 = c(230, 250, 255, 265, 270, 295)
ov3 = c(340, 355)
ov4 = c(401, 422, 424, 430)
As #RonakShah suggested, the most efficient way in this case may be this:
split(iv2, cut(iv2, breaks = iv1,labels = paste0('ov',1:4)))
Output:
$ov1
[1] 120 140 160 180
$ov2
[1] 230 250 255 265 270 295
$ov3
[1] 340 355
$ov4
[1] 401 422 424 430

Remove white space in a multiple panel structure (16 graphs) to make pie charts bigger

EDIT: I am struggling with the title of this post since I am not sure anymore whether the white space is the problem. My main concern is that the pie charts are too small and I wanted to enlarge them by filling up the white margins in between them. Not sure whether this means that the pie charts are the problem, or whether the margins are!
I made the following graph:
However, the pie charts are way too small to be of added value to this graph. I gave them a grey background and in that way it is clear that there is quite some white space that remains unused. Is there a way to make the piecharts bigger in this structure?
NOTE: originally I did not want to use the white space below (left and right from the legend) but now I am okay with all possible solutions that increase the visibility of this graph!
DATA LINE CHART
outfull<-structure(list(MEt_R = c(0.0804285986755867, 0.0818290494516811,
0.08210154234207, 0.0814755456594059, 0.0831102114593095, 0.0816695357763684,
0.0813328121344033, 0.0810108624189488, 0.0818598254382056, 0.0830431177501689,
0.0816376764003817, 0.0840073817361272, 0.0853936930533085, 0.0879219679095458,
0.0854048968568587, 0.0860759049369366, 0.0882493829941351, 0.0867703899775953,
0.0865942045402731, 0.0810145344754354), MEp_R = c(0.0403177921121929,
0.0406138102668729, 0.0400078146373141, 0.037249138567354, 0.0382399652387079,
0.0361846833846079, 0.0324923094020387, 0.0321970548437844, 0.0325638786466341,
0.0316121666375798, 0.0325852079234172, 0.0332434908844657, 0.0355976600643392,
0.0393330869225411, 0.0395149563774575, 0.0411816120852081, 0.0430085728809272,
0.0403688229638535, 0.039708256572692, 0.0396710567412495), MEt_Irr = c(-0.210579335646362,
-0.20887479701005, -0.209463300907731, -0.21134881250515, -0.213848355342722,
-0.216138343894695, -0.218218940681691, -0.220626646376378, -0.220536909843105,
-0.221776071703525, -0.22166868418364, -0.22263154911707, -0.222198289987863,
-0.222405303282204, -0.221323983219654, -0.223034048807441, -0.223231412833866,
-0.220724611396726, -0.22184116121713, -0.221652312691637), MEp_Irr = c(-0.091005037463965,
-0.0871543068244998, -0.0860520133830844, -0.082885475024097,
-0.0852796850484205, -0.0791969759021717, -0.0778689132953799,
-0.0767341772452676, -0.0756081808361748, -0.0748414459803997,
-0.0754126518879104, -0.0762317968359476, -0.0755876836232721,
-0.0758199300658314, -0.0745577666874828, -0.0762573255685292,
-0.0763362684722027, -0.074483807673175, -0.0744398384221861,
-0.0744598479378566), se_MEt_Rainfed = c(0.0774330661227882,
0.0773250866974692, 0.077256061062775, 0.0771327497535611, 0.0771521985759051,
0.0708916514716712, 0.0694665306934766, 0.0694972720680839, 0.0693729541545876,
0.0699669865284523, 0.070260199431952, 0.0690461062237432, 0.0711819979382877,
0.0710875280360804, 0.0708689510634827, 0.0711765465845632, 0.0708297931421112,
0.0704738634823944, 0.0708072460217385, 0.0709953190951179),
se_MEp_Rainfed = c(0.192756229054221, 0.191856228578138,
0.192226563324443, 0.19097117645322, 0.191387874467092, 0.173921574335822,
0.165088971309669, 0.165115227802311, 0.164795115225161,
0.164140221638083, 0.164937684783056, 0.165268057600504,
0.1649368806914, 0.168713285650115, 0.168149249109913, 0.169106046673566,
0.167335760047029, 0.165795756250856, 0.165689006757723,
0.16551179995361), se_MEt_Irrigation = c(0.12059098436772,
0.120899123289279, 0.120407371708647, 0.120696007335912,
0.120177269171968, 0.123769133952132, 0.127425159597542,
0.128368483598982, 0.128864767741048, 0.128965437272282,
0.128581625585643, 0.127783623408749, 0.127472047347015,
0.12734592667827, 0.127159348618761, 0.12634060930837, 0.127889037344552,
0.128896014165611, 0.128718310299992, 0.128757570918953),
se_MEp_Irrigation = c(0.0914799237827752, 0.0949973010336107,
0.0954100933352331, 0.0961863807496929, 0.0937431456706336,
0.0984291865206365, 0.100823347187465, 0.101012516949386,
0.101921512108335, 0.102756591718654, 0.102207359450442,
0.101524016844143, 0.102057567028856, 0.10215500585099, 0.103433290392082,
0.101335657592202, 0.102157397442543, 0.103959921670023,
0.103233139264567, 0.102622388936846), Irrigationtotal = c(4925,
4918, 4914, 4906, 4902, 4893, 4890, 4882, 4877, 4871, 4866,
4860, 4857, 4854, 4844, 4841, 4834, 4830, 4825, 4817), Irrigation0 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Irrigation10 = c(529, 522, 518, 510, 506, 497, 494, 486,
481, 475, 470, 464, 461, 458, 448, 445, 438, 434, 429, 421
), Irrigation20 = c(431, 431, 431, 431, 431, 431, 431, 431,
431, 431, 431, 431, 431, 431, 431, 431, 431, 431, 431, 431
), Irrigation30 = c(349, 349, 349, 349, 349, 349, 349, 349,
349, 349, 349, 349, 349, 349, 349, 349, 349, 349, 349, 349
), Irrigation40 = c(333, 333, 333, 333, 333, 333, 333, 333,
333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333
), Irrigation50 = c(373, 373, 373, 373, 373, 373, 373, 373,
373, 373, 373, 373, 373, 373, 373, 373, 373, 373, 373, 373
), Irrigation60 = c(255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
), Irrigation70 = c(288, 288, 288, 288, 288, 288, 288, 288,
288, 288, 288, 288, 288, 288, 288, 288, 288, 288, 288, 288
), Irrigation80 = c(239, 239, 239, 239, 239, 239, 239, 239,
239, 239, 239, 239, 239, 239, 239, 239, 239, 239, 239, 239
), Irrigation90 = c(274, 274, 274, 274, 274, 274, 274, 274,
274, 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, 274
), Irrigation100 = c(1854, 1854, 1854, 1854, 1854, 1854,
1854, 1854, 1854, 1854, 1854, 1854, 1854, 1854, 1854, 1854,
1854, 1854, 1854, 1854)), .Names = c("MEt_R", "MEp_R", "MEt_Irr",
"MEp_Irr", "se_MEt_Rainfed", "se_MEp_Rainfed", "se_MEt_Irrigation",
"se_MEp_Irrigation", "Irrigationtotal", "Irrigation0", "Irrigation10",
"Irrigation20", "Irrigation30", "Irrigation40", "Irrigation50",
"Irrigation60", "Irrigation70", "Irrigation80", "Irrigation90",
"Irrigation100"), row.names = c(NA, 20L), class = "data.frame")
DATA PIE CHARTS
percentageIT<-structure(list(nuts0 = c("IT", "IT", "IT", "IT", "IT", "IT",
"IT", "IT", "IT", "IT"), variable = structure(1:10, .Label = c("percentage_irri10",
"percentage_irri20", "percentage_irri30", "percentage_irri40",
"percentage_irri50", "percentage_irri60", "percentage_irri70",
"percentage_irri80", "percentage_irri90", "percentage_irri100"
), class = "factor"), value = c(0.108497262218617, 0.0874062056378017,
0.0707767187183127, 0.0675319407828027, 0.0756438856215778, 0.0517136483471912,
0.0584060028391807, 0.0484688704116812, 0.0555668221456094, 0.375988643277226
)), .Names = c("nuts0", "variable", "value"), row.names = c(10L,
25L, 40L, 55L, 70L, 85L, 100L, 115L, 130L, 145L), class = "data.frame")
PIE CHART
library(ggplot2)
percentlabelsIT<- round(100*percentageIT$value, 0)
pielabelsIT<- paste(percentlabelsIT, "%", sep="")
l12<-ggplot(data=percentageIT, aes(x=factor(1), y=percentlabelsIT, fill=factor(variable))) +
geom_bar(width=1, stat="identity") +
coord_polar(theta="y") +
ylab("") +
xlab("") +
labs(fill="") +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
legend.position="none",
line = element_blank()
)+
geom_text(aes(x = 2.3, # Solution for part 2,
y = percentlabelsIT / 2 + c(0, cumsum(percentlabelsIT)[-length(percentlabelsIT)]),
label=pielabelsIT))+
scale_fill_manual(values=cols)+ggtitle("IT")
cols <- c("yellow","greenyellow","#00FF00", "#00C639","#00AA55", "#00718E", "#0055AA", "#001CE3","blue4","midnightblue")
MAKE LINE CHART
library(ggplot2)
library(gtable)
library(reshape2)
out121<-outfull
# line plot
out121$perc1<-c(1:20)
l64<-ggplot(out121,aes(perc1))
l65<-l64+geom_line(aes(y=MEt_R,colour="Rainfed"),size=1.3)+
geom_line(aes(y=MEt_R+se_MEt_Rainfed,colour="Rainfed range"),size=0.7)+
geom_line(aes(y=MEt_R-se_MEt_Rainfed,colour="Rainfed range"),size=0.7)+
geom_line(aes(y=MEt_Irr,colour="Irrigation"),size=1.3)+
geom_line(aes(y=MEt_Irr+se_MEt_Irrigation,colour="Irrigation range"),size=0.7)+
geom_line(aes(y=MEt_Irr-se_MEt_Irrigation,colour="Irrigation range"),size=0.7)+
scale_colour_manual(values=c("blue3","mediumslateblue","green3","green"), name="MEt")+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="MEt",limits = c(-0.7, 0.5),breaks=c(-0.3,-0.1,0,0.1,0.3,0.5))
l66<-l65+ theme_bw()+ggtitle("Full sample") +
theme(plot.title = element_text(lineheight=.8, face="bold"),legend.position="bottom")+
guides(col=guide_legend(ncol=2,title.position="top"))+
theme(panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
l66
out121$perc<-c(1:20)
out121$perc<- as.character(out121$perc)
out121$perc <- factor(out121$perc, levels=unique(out121$perc))
test <- data.frame(out121$perc,out121$Irrigation10,out121$Irrigation20,out121$Irrigation30,out121$Irrigation40,
out121$Irrigation50,out121$Irrigation60,out121$Irrigation70,out121$Irrigation80,
out121$Irrigation90,out121$Irrigation100)
# barplot(as.matrix(test))
library(reshape2)
foo.long<-melt(test)
foo.long$out121.perc<- as.character(foo.long$out121.perc)
foo.long$out121.perc <- factor(foo.long$out121.perc, levels=unique(foo.long$out121.perc))
cbbPalette <- c("yellow","greenyellow","#00FF00", "#00C639","#00AA55", "#00718E", "#0055AA", "#001CE3","blue4","midnightblue")
l2 <- ggplot(foo.long, aes(out121.perc,value,fill=variable))+
geom_bar(position="stack",stat="identity")+
scale_fill_manual(values=cbbPalette,name = "% of irrigation",
labels = c("0-10% irrigation", "10-20% irrigation", "20-30% irrigation",
"30-40% irrigation", "40-50% irrigation", "50-60% irrigation",
"60-70% irrigation", "70-80% irrigation", "80-90% irrigation",
"90-100% irrigation"))+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="number of farms",limits = c(0, 30000), breaks=c(0, 2000,4000,6000,8000,10000,12000))+
theme(legend.position="bottom", panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())+
guides(fill=guide_legend(title="Number of irrigated farms",ncol=5, title.position = "top"))
l2
# ggplotGrob
g1 <- ggplotGrob(l2)
g2 <- ggplotGrob(l66)
# Add plots together
pp <- c(subset(g2$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g2, g1$grobs[[which(g1$layout$name == "panel")]], pp$t,
pp$l, pp$b, pp$l)
# Add second axis for accuracy
ia <- which(g1$layout$name == "axis-l")
ga <- g1$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
# Add second y-axis title
ia <- which(g1$layout$name == "ylab")
ax <- g1$grobs[[ia]]
# str(ax) # you can change features (size, colour etc for these -
# change rotation below
ax$rot <- 270
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
# extract legend
leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]]
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]]
legc = gtable:::cbind_gtable(leg1, leg2, "first")
g$grobs[[which(g$layout$name == "guide-box")]] <-
gtable:::cbind_gtable(leg1, leg2, "first")
grid.draw(g) # Note: Legend does not fit
g$heights[[6]] = unit(3, "cm") # Add more space for the legend - can adjust this to suit
grid.draw(g)
COMBINE PIECHART AND LINE CHART (so probably the solution to the question is in this part of the code)
library(gridExtra)
library(grid)
l <- cbind(c(5:1, NA), rbind(6:10, matrix(16, 5, 5)), c(11:15, NA))
g12 <- ggplotGrob(l12)
grid.arrange(grobs=list(g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g),
layout_matrix=l)

Resources