How to delete legend in ggplot with reression model?

Viewed 79

I'd like to ask how to delete legend in ggplot with regression model.

I already added theme(legend.position = "None")

but the legend cannot be deleted. Could you tell me what I was doing wrong?

Extra question!! In my current code, how to change the symbol size and shape between N0 and N1? I want more bigger size of 'open circle', and 'closed square' shape.

Many thanks!!!

enter image description here

ggplot(data=x, aes(x=agw, y=pgw)) + 
  geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen)) + 
  stat_smooth(method = 'lm', se=FALSE, color="Black") +
  scale_color_manual(values = c("Dark gray","Black")) +
  theme(legend.position = "None") +
  geom_text(x=30, y=70, label="", size=3.5, col="Black") + 
  geom_text(x=30, y=60, label="", size=3.5, col="Black") +
  scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
  scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
  theme_bw() +
  theme(panel.grid = element_blank())
2 Answers

This should work in lack of reproducible data. Be careful that functions like theme_bw() use to remove previous theme() settings as mentioned by @Ronald. So it is better to add in the final part of the plot. For shapes, you can enable shape in aes() like this and format with scale_shape_manual() (the numbers inside belong to the shape you want):

library(ggplot2)
#Code
ggplot(data=x, aes(x=agw, y=pgw)) + 
  geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen,shape=Nitrogen,size=3)) + 
  stat_smooth(method = 'lm', se=FALSE, color="Black") +
  scale_color_manual(values = c("Dark gray","Black")) +
  scale_shape_manual(values = c(1,15))+
  geom_text(x=30, y=70, label="", size=3.5, col="Black") + 
  geom_text(x=30, y=60, label="", size=3.5, col="Black") +
  scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
  scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
  theme_bw() +
  theme(panel.grid = element_blank(),
        legend.position = 'none')

For the legend: add the argument show.legend = F inside geom_point. For the different point size: can you give us an example of your dataset? We may need to reshape it.

ggplot(data=x, aes(x=agw, y=pgw)) + 
  geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen), show.legend = F) + 
  stat_smooth(method = 'lm', se=FALSE, color="Black") +
  scale_color_manual(values = c("Dark gray","Black")) +
  theme(legend.position = "None") +
  geom_text(x=30, y=70, label="", size=3.5, col="Black") + 
  geom_text(x=30, y=60, label="", size=3.5, col="Black") +
  scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
  scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
  theme_bw() +
  theme(panel.grid = element_blank()) 
Related