简体   繁体   English

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

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

I have a problem with the legend of the graph that uses both line type and line color. 我对同时使用线型和线色的图表图例有疑问。 I want for the legend to show just the two colors I used and omit line type from the legend. 我希望图例仅显示我使用的两种颜色,并省略图例中的线型。 I plan to annotate the lines so the legend is not needed. 我计划注释行,因此不需要图例。 I couldn't find a solution to this problem. 我找不到解决此问题的方法。

Here's the head of the dataset I used. 这是我使用的数据集的标题。 Variable StateAndGender has 4 different values, each of the two countries by two genders. 变量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

When I execute the code below I get: 当我执行以下代码时,我得到: 在此处输入图片说明

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)

I tried splitting the StateAndGender variable into two and setting group=State, linetype=State, col=Gender, but I got the error: 我尝试将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"

If what I want can't be done, is there a way to manually define every aspect of the legend, completely separating it from the data? 如果无法完成我想做的事情,是否有办法手动定义图例的各个方面,将其与数据完全分开?

By the way I have a separate problem with using annotation with partly italic text annotations. 顺便说一下,我在使用带有部分斜体文本注释的注释时遇到了一个单独的问题。 expression(italic("Italic text")) doesn't seem to be working (it doesn't display text at all) expression(italic(“ Italic text”))似乎不起作用(它根本不显示文本)

Try removing the linetype=StateAndGender from the aes(...) and put it outside of it in the overall ggplot(..) function or in the geom_line(...) function. 尝试从aes(...)删除linetype=StateAndGender ,并将其放在整个ggplot(..)函数或geom_line(...)函数中。 It'll look like this: 它看起来像这样:

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