私はdata.frame(要約テーブル)を持っています:
head(hejALLA)
variable År N Sum Mean sd Variance Median Min Max
1 N_kvinnor 1944 28 2419650 86416.07 223776.7 50076008602 54003.0 413 1209825
2 N_kvinnor 1945 28 2595794 92706.93 240100.4 57648222451 58040.0 459 1297897
3 N_kvinnor 1946 29 2752518 94914.41 250209.5 62604783658 55624.0 470 1376259
4 N_kvinnor 1947 30 2865208 95506.93 255979.9 65525698129 50186.5 468 1432604
5 N_kvinnor 1948 30 2993572 99785.73 267368.3 71485801980 52581.0 461 1496786
6 N_kvinnor 1949 35 3108982 88828.06 257942.9 66534536980 48729.0 456 1554491
dput(hejALLA)
structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("N_kvinnor", "N_män"), class = "factor"), År = c(1944L,
1945L, 1946L, 1947L, 1948L, 1949L, 1950L, 1951L, 1952L, 1953L,
1954L, 1944L, 1945L, 1946L, 1947L, 1948L, 1949L, 1950L, 1951L,
1952L, 1953L, 1954L), N = c(28, 28, 29, 30, 30, 35, 37, 37, 37,
37, 33, 28, 28, 29, 30, 30, 35, 37, 37, 37, 37, 33), Sum = c(2419650,
2595794, 2752518, 2865208, 2993572, 3108982, 3223124, 3341098,
3461784, 3547744, 3375942, 2275460, 2415543, 2558005, 2653682,
2760069, 2851398, 2947550, 3040133, 3144944, 3190284, 2960046
), Mean = c(86416.0714285714, 92706.9285714286, 94914.4137931034,
95506.9333333333, 99785.7333333333, 88828.0571428571, 87111.4594594595,
90299.9459459459, 93561.7297297297, 95884.972972973, 102301.272727273,
81266.4285714286, 86269.3928571429, 88207.0689655172, 88456.0666666667,
92002.3, 81468.5142857143, 79663.5135135135, 82165.7567567568,
84998.4864864865, 86223.8918918919, 89698.3636363636), sd = c(223776.693607701,
240100.442420795, 250209.479552851, 255979.878367345, 267368.289032438,
257942.894804022, 260324.366472718, 269857.390823679, 279547.104579955,
286473.142701748, 287193.206911879, 210220.469220861, 223156.992413054,
232223.774918247, 236806.100206249, 246289.093343316, 236350.737393712,
237813.20060053, 245315.995040117, 253783.749535529, 257471.044723456,
251390.219349299), Variance = c(50076008601.9947, 57648222450.6614,
62604783658.1084, 65525698128.9609, 71485801980.1333, 66534536979.879,
67768775779.4219, 72823011382.1637, 78146583679.036, 82066861489.4159,
82479938096.3295, 44192645679.4392, 49799043262.84, 53927881637.2808,
56077129094.892, 60658317499.8724, 55861671066.5513, 56555118379.8679,
60179937422.5225, 64406191528.3123, 66291338870.988, 63197042384.4886
), Median = c(54003, 58040, 55624, 50186.5, 52581, 48729, 43842,
44712, 46106, 47298, 54758, 50976, 54560, 56087, 49240.5, 51312.5,
42191, 39722, 40397, 41574, 42091, 48537), Min = c(413, 459,
470, 468, 461, 456, 455, 445, 435, 416, 1850, 360, 409, 418,
401, 386, 369, 364, 357, 346, 332, 4572), Max = c(1209825, 1297897,
1376259, 1432604, 1496786, 1554491, 1611562, 1670549, 1730892,
1773872, 1687971, 1139425, 1209436, 1280617, 1328515, 1381879,
1427689, 1475842, 1522245, 1574886, 1597663, 1480023)), .Names = c("variable",
"År", "N", "Sum", "Mean", "sd", "Variance", "Median", "Min",
"Max"), row.names = c(NA, -22L), class = "data.frame")
次に、関数でグラフを作成していますmultiplot
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
上記の関数と組み合わせたい 2 つのプロットがあります。
plotONE=ggplot(subset(hejALLA,variable=="N_män"), aes(x = År)) +
geom_line(aes(y = Mean, color = "Medelvärde delägare män",linetype = "Medelvärde delägare män"),
size = 1.7, alpha = 1) +
geom_ribbon(aes(ymax = Max,
ymin = Min, fill = "Delägare Män Max/Min"), color="blue",linetype = 3,
alpha = 0.2) +
geom_ribbon(aes(ymax = Mean+sd, ymin = Mean-sd, fill = "Mean +- sd"),
colour = "black", linetype = 2, alpha = 0.5)+
#geom_line(aes(y = Sum,
#color = "Sum Delägare Män"), size = 0.9, linetype = 1, alpha = 1) +
geom_line(aes(y = N,
color = "Antal Kassor",linetype="Antal Kassor"), size = 0.9, alpha = 1)+
scale_y_continuous(breaks = seq(-200000, 1700000, by = 50000), limits = c(-200000, 1700000),
labels = seq(-200000, 1700000, by = 50000))+
scale_x_continuous(breaks=seq(1944,1954,1))+
labs(title = "Manliga Delägare",x="Year\n",y="Value\n") +
scale_color_manual("Variabler", breaks = c("Antal Kassor","Medelvärde delägare män"),
values = c("Antal Kassor" = "black", "Medelvärde delägare män" = "#6E6E6E")) +
scale_fill_manual(" Ribbons", breaks = c("Delägare Män Max/Min", "Mean +- sd"),
values = c(`Delägare Män Max/Min` = "blue", `Mean +- sd` = "red")) +
scale_linetype_manual("Variabler",values=c("Antal Kassor"=2,"Medelvärde delägare män"=1))+
theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(),
legend.background = element_rect(fill = "white", colour = "gray30")) +
guides(fill = guide_legend(keywidth = 2, keyheight = 1), linetype=guide_legend(keywidth = 3, keyheight = 1),
colour=guide_legend(keywidth = 6, keyheight = 1)) +
coord_cartesian(ylim = c(-200000, 1700000),xlim=c(1944,1954))+
theme(axis.text.x = element_text(hjust=0.5, size=9,colour="black",face="bold"),
axis.text.y = element_text(size=9,colour="black",face="bold"),
#panel.grid.minor = element_blank(),
#panel.background = element_blank(), #färgen på backgrunden. Vill ej ha nån (vit) så tar bort.
axis.ticks = element_blank(), ##Ska det gå en linje från siffrorna på axlarna?
plot.title = element_text(size = rel(1.2)),
axis.title.y = element_text(size = rel(1.1)),
axis.title.x=element_text(size = rel(1.1)))
#
plotTWO=ggplot(subset(hejALLA,variable=="N_kvinnor"), aes(x = År)) +
geom_line(aes(y = Mean, color = "Medelvärde delägare kvinnor",linetype = "Medelvärde delägare kvinnor"),
size = 1.7, alpha = 1) +
geom_ribbon(aes(ymax = Max,
ymin = Min, fill = "Delägare Kvinnor Max/Min"), color="blue",linetype = 3,
alpha = 0.2) +
geom_ribbon(aes(ymax = Mean+sd, ymin = Mean-sd, fill = "Mean +- sd"),
colour = "black", linetype = 2, alpha = 0.5)+
#geom_line(aes(y = Sum,
#color = "Sum Delägare Män"), size = 0.9, linetype = 1, alpha = 1) +
geom_line(aes(y = N,
color = "Antal Kassor",linetype="Antal Kassor"), size = 0.9, alpha = 1)+
scale_y_continuous(breaks = seq(-200000, 2100000, by = 50000), limits = c(-200000, 2100000),
labels = seq(-200000, 2100000, by = 50000))+
scale_x_continuous(breaks=seq(1944,1954,1))+
labs(title = "Kvinnliga Delägare",x="Year\n",y="Value\n") +
scale_color_manual("Variabler", breaks = c("Antal Kassor","Medelvärde delägare kvinnor"),
values = c("Antal Kassor" = "black", "Medelvärde delägare kvinnor" = "#6E6E6E")) +
scale_fill_manual("Ribbons", breaks = c("Delägare Kvinnor Max/Min", "Mean +- sd"),
values = c("Delägare Kvinnor Max/Min" = "blue", `Mean +- sd` = "red")) +
scale_linetype_manual("Variabler",values=c("Antal Kassor"=2,"Medelvärde delägare kvinnor"=1))+
theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(),
legend.background = element_rect(fill = "white", colour = "gray30")) +
guides(linetype=guide_legend(keywidth = 3, keyheight = 1),fill = guide_legend(keywidth = 2, keyheight = 1),
colour=guide_legend(keywidth = 6, keyheight = 1)) +
coord_cartesian(ylim = c(-200000, 2100000),xlim=c(1944,1954))+
theme(axis.text.x = element_text(hjust=0.5, size=9,colour="black",face="bold"),
axis.text.y = element_text(size=9,colour="black",face="bold"),
#panel.grid.minor = element_blank(),
#panel.background = element_blank(), #färgen på backgrunden. Vill ej ha nån (vit) så tar bort.
axis.ticks = element_blank(), ##Ska det gå en linje från siffrorna på axlarna?
plot.title = element_text(size = rel(1.2)),
axis.title.y = element_text(size = rel(1.1)),
axis.title.x=element_text(size = rel(1.1)))
次に、次のように呼び出します。
multiplot(plotONE,plotTWO, cols=2)
はresult
:
2枚の写真の一番下の場所が変わる理由、legend
つまり2枚の写真で場所が変わる理由がわかりません...Variabler
Ribbons
何か案は?
よろしくお願いします!
EDIT
内部で
使用しようとするguides
と、結果は次のようになります。order
plotTWO=ggplot(subset(hejALLA,variable=="N_kvinnor"), aes(x = År)) +
geom_line(aes(y = Mean, color = "Medelvärde delägare kvinnor",linetype = "Medelvärde delägare kvinnor"),
size = 1.7, alpha = 1) +
geom_ribbon(aes(ymax = Max,
ymin = Min, fill = "Delägare Kvinnor Max/Min"), color="blue",linetype = 3,
alpha = 0.2) +
geom_ribbon(aes(ymax = Mean+sd, ymin = Mean-sd, fill = "Mean +- sd"),
colour = "black", linetype = 2, alpha = 0.5)+
#geom_line(aes(y = Sum,
#color = "Sum Delägare Män"), size = 0.9, linetype = 1, alpha = 1) +
geom_line(aes(y = N,
color = "Antal Kassor",linetype="Antal Kassor"), size = 0.9, alpha = 1)+
scale_y_continuous(breaks = seq(-200000, 2100000, by = 50000), limits = c(-200000, 2100000),
labels = seq(-200000, 2100000, by = 50000))+
scale_x_continuous(breaks=seq(1944,1954,1))+
labs(title = "Kvinnliga Delägare",x="Year\n",y="Value\n") +
scale_color_manual("Variabler", breaks = c("Antal Kassor","Medelvärde delägare kvinnor"),
values = c("Antal Kassor" = "black", "Medelvärde delägare kvinnor" = "#6E6E6E")) +
scale_fill_manual("Ribbons", breaks = c("Delägare Kvinnor Max/Min", "Mean +- sd"),
values = c("Delägare Kvinnor Max/Min" = "blue", `Mean +- sd` = "red")) +
scale_linetype_manual("Variabler",values=c("Antal Kassor"=2,"Medelvärde delägare kvinnor"=1))+
theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(),
legend.background = element_rect(fill = "white", colour = "gray30")) +
guides(fill = guide_legend(keywidth = 2, keyheight = 1,order=2), linetype=guide_legend(keywidth = 3, keyheight = 1,order=1),
colour=guide_legend(keywidth = 6, keyheight = 1)) +
coord_cartesian(ylim = c(-200000, 2100000),xlim=c(1944,1954))+
theme(axis.text.x = element_text(hjust=0.5, size=9,colour="black",face="bold"),
axis.text.y = element_text(size=9,colour="black",face="bold"),
#panel.grid.minor = element_blank(),
#panel.background = element_blank(), #färgen på backgrunden. Vill ej ha nån (vit) så tar bort.
axis.ticks = element_blank(), ##Ska det gå en linje från siffrorna på axlarna?
plot.title = element_text(size = rel(1.2)),
axis.title.y = element_text(size = rel(1.1)),
axis.title.x=element_text(size = rel(1.1)))
だから私は3つの伝説を手に入れます...
何か案は?