0

I need help:

I cannot seem to add a legend to the following piece of code for ggplot R STDUIO

ggplot(Report_Data, 
   aes(x=Report_Data$Transect Point), show.legend = TRUE) + 
geom_point(aes(y=Report_Data$Q1North), 
   shape = 6, size = 5, colour = label , show.legend = TRUE) + 
geom_point(aes(y=Report_Data$Q1South), 
   shape = 4, size = 5, colour = label, show.legend = TRUE)+ 
labs(title="Density of Trees Species found North & South of the creek using two sampling methods",
   y="Density in Tree Species Found", x="Transect Points",caption = "n7180853")+
geom_line(aes(y=Report_Data$Q1North, colour = Q1North), 
   colour = "green", size = 1, show.legend = TRUE)+  
geom_line(aes(y=Report_Data$Q1South, color = Q1South), 
   colour = "pink4", size = 1, show.legend = TRUE)+ theme(legend.position = "right")
dshkol
  • 1,208
  • 7
  • 23
  • 1
    Your code is unreadable. 1. Please format your code; 2. Add data `Report_Data` to your question using `dput` function. Otherwise no-one will be able to help you. – pogibas Mar 23 '18 at 06:12
  • 1
    ggplot likes data in long format - [convert you dataframe from it's current wide format to long format](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format) where the values for Q1South and Q1North are in the same column. Then you will be able to plot as the answer below shows. – Jan Boyer Mar 23 '18 at 15:14

1 Answers1

0

Hard to replicate without your data, but if you want to have your plot to have a legend for point shape, you need to include it within aes(...) for that layer. Similar with colour, or size, if you want. Then, add scale_shape_manual(...) or scale_colour_manual(...) as needed, specifying your specific values.

Here's a toy example using the default diamonds dataset.

data("diamonds")

ggplot(diamonds, aes(x = carat)) +
  geom_point(data = diamonds[diamonds$cut == "Ideal",], 
             aes(y= price, shape = cut)) +
  geom_point(data = diamonds[diamonds$cut == "Good",],
             aes(y = price, shape = cut)) +
  scale_shape_manual(values = c(6,4))

enter image description here

dshkol
  • 1,208
  • 7
  • 23