简体   繁体   English

geom_line 图例不出现

[英]geom_line legend does not appear

I have the data:我有数据:

No. s1      s2      s3      s4
1   0.52    0.25    0.03    0.12
2   0.32    0.45    0.12    0.98
3   0.46    0.48    0.52    0.82
4   0.25    0.47    0.24    0.5
5   0.09    0.53    0.85    0.41
6   0.98    0.02    0.72    0.35
7   0.54    0.91    0.63    0.65

I have plotted the graph but ggplot does not show the legend.我已经绘制了图表,但 ggplot 没有显示图例。 Do you have any idea how I can bring it?你知道我怎么带它吗?

code:代码:

ggplot(data=file1, aes(x = No.)) + 
  geom_line(aes(y = s1), colour="red",size=1)+
  geom_line(aes(y = s2), colour="firebrick", size=1, alpha=.2)+
  geom_line(aes(y = s3), colour="orange", size=1, alpha=.2)+
  geom_line(aes(y = s4), colour="darkolivegreen", size=1)+
  xlab("Number")+ylab("length")+
  theme(legend.position="bottom")+
  scale_x_discrete(limits=c("s1", "s2", "s3", "s4"))+
  scale_fill_discrete(name = "Dose", labels = c("group1", "group2", "group3", "group4"))

I also tried reshaping my data like the following:我还尝试像下面这样重塑我的数据:

No. Data    group
1   0.52    1
2   0.32    1
3   0.46    1
4   0.25    1
5   0.09    1
..  ...    ...
26  0.41    4
27  0.35    4
28  0.65    4

with the following code, it brings the legend but in a shades of color not different ones.使用以下代码,它带来了图例,但颜色深浅不同。

ggplot(file1, aes(x=No., colour=group))+
  geom_line(aes(y=Data))

Thanks谢谢

You made a mistake when assigning the colour aesthetic.您在指定颜色美学时犯了一个错误。

ggplot(file1, aes(x=No., colour=group) )+
  geom_line(aes(y=Data))

Starting with your data in a long form.从长格式的数据开始。 It was just a matter of using the scale_colour_manual( ) function to add the proper color to the chart.只需使用scale_colour_manual( ) function 为图表添加适当的颜色即可。

data<-structure(list(No. = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 
                             3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 
                             7L, 7L), group = c("s1", "s2", "s3", "s4", "s1", "s2", "s3", 
                                                "s4", "s1", "s2", "s3", "s4", "s1", "s2", "s3", "s4", "s1", "s2", 
                                                "s3", "s4", "s1", "s2", "s3", "s4", "s1", "s2", "s3", "s4"), 
                     Data = c(0.52, 0.25, 0.03, 0.12, 0.32, 0.45, 0.12, 0.98, 
                              0.46, 0.48, 0.52, 0.82, 0.25, 0.47, 0.24, 0.5, 0.09, 0.53, 
                              0.85, 0.41, 0.98, 0.02, 0.72, 0.35, 0.54, 0.91, 0.63, 0.65
                     )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -28L))


labels <- c("group1", "group2", "group3", "group4")
title<-"Legend"
ggplot(data, aes(x=No., y=Data, colour=group, alpha=group, size=group))+
  geom_line() + 
  xlab("Number")+ylab("length")+
  theme(legend.position="bottom")+
  scale_colour_manual(values=c("s1" = "red", "s2"= "firebrick", s3="orange", s4="darkolivegreen"), labels = labels) +
  scale_alpha_manual(values=c(1,0.4,0.4,1), labels = labels)+
  scale_size_manual(values=c(4,1,1,4), labels = labels) + 
  guides(colour = guide_legend(title = title), 
         alpha = guide_legend(title = title), 
         size = guide_legend(title = title))

在此处输入图像描述

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

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