Add a variable as legend/factor to XY-Plot / GG Plot

Hey guys,

I'm totally new to R but I have to get into it prior to my master thesis. I hope you can help me with a problem so I can finally move on.
I'm trying to create a XY-Plot for porpoise surfacings. I'm gonna use X and Y coordinates for the axes and want to set the porpoise individuals/numbers as legend. So each identified porpoise should have its own color so that we can see all the surfacing coordinates for one porpoise as blue dots for porpoise 1 e.g. and all coordinates for porpoise 2 as yellow squares etc.

This is the code I got right now:
ggplot(example_data, aes(x = XNorth, y = YEast, color = as.factor("Porposise Number"))) + geom_point(size = 3, shape = 18) + labs(y= "East coordinate", x = "North coordinate", color = "Porpoise Number") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

I tried using a code I created before for a different research. Right now my plot looks like this:

How do I edit my code to show me the different porpoises (3 in this case)? I though it'd work when setting the Porpoise Number as factor. Worked before on my previous plot.

Thanks for any help,

Kingpin

I can't say for sure since your example is not reproducible (you haven't provided sample data) but I think you just have to remove the quotes from this.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

When I remove the quotes it says: unexpected token 'Number'
This is my table/data I want to get into the plot. Is this what you need or do you need smth else? Sorry, I'm a total greenhorn. :grimacing:

Porpoise No. Porpoise (x) Porpoise (y)
1 6172826,456 222676,4847
1 6172414,223 222076,6291
1 6172414,221 222076,6538
1 6172567,639 222190,1839
2 6172559,282 222237,9557
2 6172559,289 222237,9375
2 6172567,881 222186,9024
3 6172572,18 222199,7656
3 6172654,191 222214,7645
3 6172882,358 222630,447

Click on the link I gave you before and read the reprex guide so you can learn how to most effectively (and properly) ask for help.

Still not sure if that's what you need. I spent like an hour trying to figure out how to use reprex and datapasta. I just wished we have learned some R at college before. :frowning:

dpasta(example_data)
#> Error in dpasta(example_data): konnte Funktion "dpasta" nicht finden
data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
Porpoise Number = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L),
XNorth = c("6172826,456",
"6172414,223","6172414,221","6172567,639",
"6172559,282","6172559,289","6172567,881",
"6172572,18","6172654,191","6172882,358"),
YEast = c("222676,4847",
"222076,6291","222076,6538","222190,1839",
"222237,9557","222237,9375","222186,9024",
"222199,7656","222214,7645","222630,447")
)
#> Porpoise Number XNorth YEast
#> 1 1 6172826,456 222676,4847
#> 2 1 6172414,223 222076,6291
#> 3 1 6172414,221 222076,6538
#> 4 1 6172567,639 222190,1839
#> 5 2 6172559,282 222237,9557
#> 6 2 6172559,289 222237,9375
#> 7 2 6172567,881 222186,9024
#> 8 3 6172572,18 222199,7656
#> 9 3 6172654,191 222214,7645
#> 10 3 6172882,358 222630,447

ggplot(example_data, aes(x = XNorth, y = YEast, color = as.factor("Porposise Number"))) + geom_point(size = 3, shape = 18) + labs(y= "East coordinate", x = "North coordinate", color = "Porpoise Number") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

You were close to a proper reprex, luckily your attempt is good enough.

Porpoise Number is a non-syntactic variable name, if you want to use this kind of names in R you have to wrap them in back tics (i.e. `Porpoise Number`).

library(ggplot2)

example_data <- data.frame(
    stringsAsFactors = FALSE,
    check.names = FALSE,
    `Porpoise Number` = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L),
    XNorth = c("6172826,456",
               "6172414,223","6172414,221","6172567,639",
               "6172559,282","6172559,289","6172567,881",
               "6172572,18","6172654,191","6172882,358"),
    YEast = c("222676,4847",
              "222076,6291","222076,6538","222190,1839",
              "222237,9557","222237,9375","222186,9024",
              "222199,7656","222214,7645","222630,447")
)

ggplot(example_data, aes(x = XNorth, y = YEast, color = as.factor(`Porpoise Number`))) +
    geom_point(size = 3, shape = 18) +
    labs(y = "East coordinate",
         x = "North coordinate",
         color = "Porpoise Number") +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

Created on 2021-03-30 by the reprex package (v1.0.0.9002)

1 Like

Thank you so much!

Just one more thing. I'm normally loading my data frames from my working space where I have them as a .txt. In this case, do I need to change the "Harbour porpoise" into the integer format with the as.integer command? Or won't this work?

Best regards,
Kingpin

Sorry but I don't understand your question, Can you elaborate?

Nevermind, got it. :slight_smile:

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.