How to color the legend according to the chart in R

Asked

Viewed 2,685 times

4

Hello guys I have a chart on R and would like to make the caption equal to the chart. follows below the command used and the graphic image.

control = c(100,100,100,100,100)
dez = c(100,90,0,0,0)
cinco = c(100,100,100,50,0)
dois = c(100,100,100,100,0)
hours = c(0,24,48,60,72)

plot(control~hours,ylim = c(0,120),xlim = c(0,80),type = 'l',col='black',pch = 1,lwd = 2)

points(control~hours,t = 'p',pch = 16,col='green')
points(dez~hours,type = 'l',pch = 16,col='black',lwd = 2)
points(dez~hours,t = 'p',pch = 16,col='blue')
points(cinco~hours,t = 'l',col='black',lwd = 2)
points(cinco~hours,t = 'p',pch = 16,col='red')
points(dois~hours,t = 'l',col='black',lwd =2)
points(dois~hours,t = 'p',pch = 16,col='orange')

legend('bottomleft',legend = c('Control','10 mg/mL','5 mg/mL','2,5 mg/mL'),col = "black",lty = 1,lwd=2 ,pch=16)

And with that generated this graph.

Gráfico

What I’d like to do is paint the caption balls according to the color that’s on the chart. Thank you.

  • try to build your chart in ggplot2, it will be easier to associate the legend to the chart, see my question: http://answall.com/questions/192221/howto reproduce-um-gr%C3%A1fico-que-foi-criado-com-o-comando-Plot-utilizando-o-ggplot

1 answer

3


You can put a legend on top of another with transparent background:

legend('bottomleft',
       legend = c('Control','10 mg/mL','5 mg/mL','2,5 mg/mL'),
       col = c("black"), lty = 1, lwd=2, pch = NA)

legend('bottomleft',
       legend = c('Control','10 mg/mL','5 mg/mL','2,5 mg/mL'),
       col = c("green", "blue", "red", "orange"), lty = 0, lwd=2, pch = 16, 
       bg='transparent')

inserir a descrição da imagem aqui

  • Thank you, that’s what I wish.

  • @Joãopedropires for nothing! You can accept the answer by clicking the accept button on the left of the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.