我有三组情节。我已经使用了facet来获取我想要的图形,并且我设法将颜色和形状组合成一个图例(如下所示)。然而,问题是图例包含所有六个变量名称,而只有两个足够。
这是我目前的输出:
是否可以获得只有两个键的图例:“分歧”和“女性百分比”(而不是当前的6个键)?
以下是用于制作情节的代码:
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))
答案 0 :(得分:1)
我会通过简单地为“女性百分比”和“分歧”制作分组变量来实现这一点。你的情况特别容易,因为这两个术语的长度完全相同。您可以使用substr
从整个字符串中拆分所需的术语。希望其他人会在ggplot2
内自行解决如何做到这一点。
这里我只创建一个新变量来表示你想要着色的两个组。
df.m$groups = substr(df.m$variable, 1, 10)
然后只使用此变量作为color
和shape
美学而不是variable
。
ggplot(df.m, aes(x=Years, y=value, colour=groups, shape=groups)) +
geom_point(size=3) +
facet_grid(.~facet) +
scale_colour_manual(values = c("chartreuse4", "deepskyblue3"))
答案 1 :(得分:1)
在定义df
使库明确使用:
library("ggplot2")
library("reshape2")
library("grid")
另一种制作df.m
的方法,其中还包括将两个不同的衡量标准(“女性百分比”和“分歧”)分为一个栏目和部门(“数学”,“物理”,“政治” Sci。“)进入另一个专栏。
df.m <- melt(df, id="Years")
df.m$measure <- gsub("(.*) \\(.*", "\\1", df.m$variable)
df.m$facet <- gsub(".*\\((.*)\\)", "\\1", df.m$variable)
您的绘图代码,放在一个声明中。 colour
和shape
现已映射到度量,而不是variable
。手册shape
和colour
比例也只有两个条目。我把传说移到了顶部只是因为它不再是相同的尺寸/形状,所以没有像以前那样顺利排列;你可以把它放在任何你想要的地方。
ggplot(df.m, aes(group=1, x=Years, y=value, colour=measure, shape=measure)) +
scale_colour_manual(name='', values=c('chartreuse4', 'deepskyblue3')) +
scale_shape_manual(name='', values=c(19, 17)) +
geom_point(size=3) +
facet_grid(.~facet) +
coord_cartesian(ylim=(c(0.0,0.45))) +
scale_x_discrete("", expand=c(0.01, 0.01)) +
scale_y_continuous(name="") +
guides(colour=guide_legend(title='', ncol=2, keywidth=unit(2,'lines'))) +
theme(legend.position="top",
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))
要回答关于仅在图例中显示某些值的确切问题,您可以使用breaks
参数进行比例。请改用以下scale_colour_manual
和scale_shape_manual
行:
g <- g + scale_colour_manual(name='',
breaks=c('% of Women (Physics)', 'Divergence (Physics)'),
labels=c('% of Women', 'Divergence'),
values=c('chartreuse4', 'deepskyblue3','chartreuse4',
'deepskyblue3', 'chartreuse4', 'deepskyblue3'))
g <- g + scale_shape_manual(name='',
breaks=c('% of Women (Physics)', 'Divergence (Physics)'),
labels=c('% of Women', 'Divergence'),
values=c(19, 17, 19, 17, 19, 17))
但是,从长远来看,让您的数据准确反映出您想要映射到美学的内容会更好。
答案 2 :(得分:0)
您应该能够通过使用scale_ *调用指南来选择性地从图例中移除参数指南=&#39;无&#39;或通过附加例如+指南(颜色=假)