私は3つのグループを持つプロットを持っています。必要に応じてファセットを使用してグラフを取得し、色と形状を 1 つの凡例にまとめることができました (以下を参照)。ただし、問題は、凡例には 6 つの変数名がすべて含まれているのに、2 つしかないことです。
ここに私の現在の出力があります:
(現在の 6 つのキーの代わりに) "Divergence" と "% of Women" の 2 つのキーだけで凡例を取得することは可能ですか?
プロットを生成するために使用されるコードは次のとおりです。
years <- c('97','98','99','00','01','02','03','04','05','06','07','08','09','10','11')
years <- factor(years, levels=years, ordered=T)
phy_ratio <- c(0.124516129032258, 0.11545988258317, 0.115190784737221, 0.120919881305638, 0.132198952879581, 0.147636363636364, 0.171033478893741, 0.155994550408719, 0.150121065375303, 0.182989690721649, 0.19466515323496, 0.194550408719346, 0.203811540497618, 0.214399152991001, 0.195157384987893)
phy_kldiv <- c(0.040955264723678, 0.001463273151143, 0.011790601776013, 0.00575319295143, 0.003434619043043, 0.001405575036774, 0.012395353183334, 0.002864433864471, 0.006622155735437, 0.074859543690491, 0.013087320475828, 0.023585193439178, 0.08866626868359, 0.07879809266254, 0.04536730602564)
mat_ratio <- c(0.236086175942549, 0.253846153846154, 0.256481481481481, 0.246901811248808, 0.273267326732673, 0.290076335877863, 0.265861027190332, 0.283457249070632, 0.27098919368246, 0.296156744536549, 0.289834174477289, 0.309506790564689, 0.311612903225806, 0.293710691823899, 0.286604361370716)
mat_kldiv <- c(0.024935971694693, 0.012778283551598, 0.019350970177576, 0.00988763992456, 0.008284622131022, 0.014700010603506, 0.015235482499119, 0.023914776035294, 0.018878559121565, 0.073688344207842, 0.042784809873074, 0.052110805729914, 0.072367460713338, 0.017494663842138, 0.019605349179071)
psc_ratio <- c(0, 0, 0, 0.370182555780933, 0.325227963525836, 0.416528925619835, 0.379727685325265, 0.333901192504259, 0.396440129449838, 0.357142857142857, 0.412265758091993, 0.415605095541401, 0, 0, 0)
psc_kldiv <- c(0, 0, 0, 0.156958669813655, 0.02319115435268, 0.019560312744745, 0.142939013816555, 0.050687092785045, 0.030903744617805, 0.021234599637716, 0.049901381314152, 0.176930275568253, 0, 0, 0)
df <- data.frame("Years"=years,
'% of Women (Physics)'=phy_ratio,
'Divergence (Physics)'=phy_kldiv,
'% of Women (Maths)'=mat_ratio,
'Divergence (Maths)'=mat_kldiv,
'% of Women (Polit. Sci.)'=psc_ratio,
'Divergence (Polit. Sci.)'=psc_kldiv,
check.names=F)
df.m <- melt(df, id="Years")
df.m <- transform(df.m, facet=ifelse(variable %in% c('% of Women (Physics)',
'Divergence (Physics)'), 'phy',
ifelse(variable %in% c('% of Women (Maths)',
'Divergence (Maths)'),'mat',
ifelse(variable %in% c('% of Women (Polit. Sci.)', 'Divergence (Polit. Sci.)'), 'psc', 'mat'))))
g <- ggplot(df.m, aes(group=1, x=Years, y=value, colour=variable, shape=variable))
g <- g + scale_colour_manual(name='',
labels=c('Phy: % of Women', 'Phy: Divergence',
'Maths: % of Women', 'Maths: Divergence',
'Polit. Sci: % of Women', 'Polit. Sci: Divergence'),
values=c('chartreuse4', 'deepskyblue3', 'chartreuse4', 'deepskyblue3', 'chartreuse4', 'deepskyblue3'))
g <- g + scale_shape_manual(name='',
labels=c('Phy: % of Women', 'Phy: Divergence',
'Maths: % of Women', 'Maths: Divergence',
'Polit. Sci: % of Women', 'Polit. Sci: Divergence'),
values=c(19, 17, 19, 17, 19, 17))
g <- g + geom_point(aes(colour=variable), size=3)
g <- g + facet_grid(.~facet)
g <- g + coord_cartesian(ylim=(c(0.0,0.45)))
g <- g + scale_x_discrete("", expand=c(0.01, 0.01))
g <- g + scale_y_continuous(name="")
g <- g + guides(colour=guide_legend(title='', ncol=2, keywidth=unit(2,'lines')))
g <- g + theme(legend.position=c(0.33,0.72),
legend.justification=c(0,0),
legend.key=element_blank(),
legend.background=element_rect(colour='black', fill='transparent'),
legend.text=element_text(size=12),
panel.grid.minor = element_blank(),
panel.margin=unit(1, 'lines'),
axis.text=element_text(size=12,color="black"),
axis.title=element_text(size=16),
strip.text.y = element_text(size = 14))