2

我有一个三组的情节。我已经根据需要使用构面来获取图形,并且我设法将颜色和形状统一为一个图例(如下所示)。然而,问题是图例包含所有六个变量名,而只有两个就足够了。

这是我当前的输出: 在此处输入图像描述

是否可以获得只有两个键的图例:“分歧”和“女性百分比”(而不是当前的 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))
4

3 回答 3

2

在你定义之后拿起df

明确使用库:

library("ggplot2")
library("reshape2")
library("grid")

一种不同的制作方法,df.m其中还包括将两个不同的度量(“女性百分比”和“分歧”)拉到一个列中,将部门(“数学”、“物理”、“政治。科学”)拉到另一列.

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. 手册shapecolour秤也只有两个条目。我将图例移到顶部只是因为它不再是相同的大小/形状,因此没有像以前那样很好地排列;你可以把它放在你想要的任何地方。

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_manualscale_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))

但是,从长远来看,让您的数据准确地反映您试图映射到美学的事物会更好。

于 2013-10-11T21:19:17.040 回答
1

我会通过简单地为“女性百分比”和“分歧”创建一个分组变量来做到这一点。您的案例特别简单,因为这两个术语的长度完全相同。您可以使用substr从整个字符串中拆分出您想要的术语。希望其他人会在自己内部加入如何做到这一点ggplot2

在这里,我只是创建了一个新变量,代表您要着色的两组。

df.m$groups = substr(df.m$variable, 1, 10)

然后只需将此变量用作您的colorshape美学而不是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"))
于 2013-10-11T21:19:04.093 回答
0

您应该能够通过使用带有参数 guide='none' 的 scale_* 调用或通过附加 eg + guides(color=FALSE) 来选择性地从图例中删除

http://docs.ggplot2.org/0.9.2.1/guides.html

于 2013-10-11T19:41:14.047 回答