繁体   English   中英

从ggplot2线型和颜色图表的图例中省略线型

[英]Omitting linetype from legend of a ggplot2 linetype and color chart

我对同时使用线型和线色的图表图例有疑问。 我希望图例仅显示我使用的两种颜色,并省略图例中的线型。 我计划注释行,因此不需要图例。 我找不到解决此问题的方法。

这是我使用的数据集的标题。 变量StateAndGender具有4个不同的值,两个国家中的每个国家都有两种性别。

head(le) 
    Age StateAndGender      Value V4
1    <1            EuM 0.03774679  1
2   4-5            EuM 0.02771025  2
3   5-9            EuM 0.03086298  3
4 10-14            EuM 0.03001608  4
5 15-19            EuM 0.04380364  5
6 20-24            EuM 0.05579602  6

当我执行以下代码时,我得到: 在此处输入图片说明

le.chart <- ggplot(data=le, aes(x=reorder(Age, V4), y=Value,
group=StateAndGender,linetype=StateAndGender, col=StateAndGender)) 
le.chart +
geom_line(size=1.25) +
coord_fixed(ratio=0.5)+
theme(text=element_text(size=12, family="Times New Roman"),
        axis.text=element_text(size=10),
        axis.title=element_text(size=11))+
scale_linetype_manual(values=c(1,8,1,8), guide=FALSE) +
scale_color_manual(values = c("#636363","#636363","#BDBDBD","#BDBDBD"),
                     labels=c("Country 1 / Italic text "," ", "Country / Italic text"),
                     guide=guide_legend(title = "Легенда"))+
annotate("text", label = "Males / ITALIC TEXT ", x = 13, y=18, family="Times New Roman")+
annotate("text", label = "Females / ITALIC TEXT ", x = 16, y =1.25, family="Times New Roman")+
xlab(NULL)+
ylab(NULL)+
guides(linetype=FALSE)

我尝试将StateAndGender变量拆分为两个并设置group = State,linetype = State,col = Gender,但出现错误:

"Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line"

如果无法完成我想做的事情,是否有办法手动定义图例的各个方面,将其与数据完全分开?

顺便说一下,我在使用带有部分斜体文本注释的注释时遇到了一个单独的问题。 expression(italic(“ Italic text”))似乎不起作用(它根本不显示文本)

尝试从aes(...)删除linetype=StateAndGender ,并将其放在整个ggplot(..)函数或geom_line(...)函数中。 它看起来像这样:

le.chart <- ggplot(data=le, aes(x=reorder(Age, V4), y=Value,
group=StateAndGender, col=StateAndGender),linetype=StateAndGender) 
le.chart + . . . 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM