Remove white space in a multiple panel structure (

2019-08-15 06:29发布

问题:

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)