Shalen
Shalen

Reputation: 95

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. 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

Upvotes: 0

Views: 422

Answers (2)

Dave2e
Dave2e

Reputation: 24079

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.

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

enter image description here

Upvotes: 1

Sergio Romero
Sergio Romero

Reputation: 368

You made a mistake when assigning the colour aesthetic.

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

Upvotes: 1

Related Questions