no able to produce a legend

Hello guys, I am not able to visualise a legend using the following code, does anyone know why?

dot_graph = ggplot()+
  geom_point(data = table_3, aes(x = time, y=control), shape = "triangle", color = "blue")+
  geom_point(data = table_3, aes(x = time, y=temp_20_pH_7.67), shape = "circle", color = "green")+
  geom_point(data = table_3, aes(x = time, y=temp_23_pH_8.1), shape = "square", color = "limegreen")+
  geom_point(data = table_3, aes(x = time, y=temp_23_ph_7.67), shape = "cross", color = "mediumblue")+
  theme(legend.position = "top")+
  xlab('time')+
  ylab('% of settlement')+
  scale_x_discrete(limits = c(0,20,40,60,80,100,120))+
  scale_y_discrete(limits = c(0,20,40,60,80,100))+
  theme_classic()+
  theme(plot.margin = unit(c(2,2,2,2),"cm"))+
  expand_limits(y=c(0,100))

Many thanks!

You have to map your variables to an aesthetics (shape, color, etc.) to get an automatic legend, this could be achieved by reshaping your dataframe to a long format before plotting or you could manually add some scales to get a legend.

We could give you more specific help if you provide a reproducible example, could you please turn this into a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

Thanks Andres, here it is, I've inserted (-) between each header of each column :
time - control - temp_20_pH_7.67 - temp_23_pH_8.1 - temp_23_ph_7.67
10 10 18 3 10
20 20 30 5 10
30 25 40 8 13
40 38 43 10 15
50 40 48 13 25
60 40 48 13 30

Please read the link I gave you, that is not a copy/paste friendly format, is actually pretty easy to properly share sample data, give it a try.

here it is:

#libraries
library(sqldf)
library(ggplot2)
library(ggpubr)
library(dplyr)
library(tibble)

#the dataframe
data.frame(
time = c(10, 20, 30, 40, 50),
control = c(10, 20, 25, 38, 40),
temp_20_pH_7.67 = c(18, 30, 40, 43, 48),
temp_23_pH_8.1 = c(3, 5, 8, 10, 13),
temp_23_ph_7.67 = c(10, 10, 13, 15, 25)
)

#the command
dot_graph = ggplot()+
geom_point(data = table_3, aes(x = time, y=control), shape = "triangle", color = "blue")+
geom_point(data = table_3, aes(x = time, y=temp_20_pH_7.67), shape = "circle", color = "green")+
geom_point(data = table_3, aes(x = time, y=temp_23_pH_8.1), shape = "square", color = "limegreen")+
geom_point(data = table_3, aes(x = time, y=temp_23_ph_7.67), shape = "cross", color = "mediumblue")+
theme(legend.position = "top")+
xlab('time')+
ylab('% of settlement')+
scale_x_discrete(limits = c(0,20,40,60,80,100,120))+
scale_y_discrete(limits = c(0,20,40,60,80,100))+
theme_classic()+
theme(plot.margin = unit(c(2,2,2,2),"cm"))+
expand_limits(y=c(0,100))+
ggtitle("Larvae settlement time in each treatment")+
theme(plot.title = element_text(hjust=0.5))

That was very close to a proper reprex, you just need to integrate all the parts and narrow down your code to the problematic part, take a look at the way I'm sharing my solution.

library(ggplot2)
library(dplyr)
library(tidyr)

table_3 <- data.frame(
    time = c(10, 20, 30, 40, 50),
    control = c(10, 20, 25, 38, 40),
    temp_20_pH_7.67 = c(18, 30, 40, 43, 48),
    temp_23_pH_8.1 = c(3, 5, 8, 10, 13),
    temp_23_ph_7.67 = c(10, 10, 13, 15, 25)
)

table_3 %>% 
    gather(type, settlement, -time) %>% 
    ggplot(aes(x = time, y=settlement, shape = type, color = type))+
    geom_point()+
    labs(title = "Larvae settlement time in each treatment",
         x = 'Time',
         y = '% of settlement') +
    theme_classic()+
    expand_limits(y=c(0,100))+
    theme(plot.title = element_text(hjust=0.5))

Created on 2019-02-22 by the reprex package (v0.2.1)

1 Like

Thanks Andres, it looks perfect!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.