0

I am trying to make a Rare Earth Elements spider diagram that places concentration in log10 on the y-axis, and each respective element from the Rare Earth Elements on the x-axis. I then am trying to compare several units of rock with each other. An example of what I am looking for and what I am getting is added to the google doc link below.

So, with the code I have added I have two problems: 1. The elements are being listed on the x-axis in alphabetical order, not in the order that I have in my CSV 2. I don't know what I am missing in my code to correlate the points together in each sample to build a line. I couple this with not knowing if that is an issue with my code, or with the way my data is arranged in the CSV.

I have seen someone else tackle this issue by treating the respective elements as dates. I have played with lubridate a bit, but I feel like it wasn't as successful as the code that I've added below... which is saying something.

ggplot(data=dataMGSREE) +
geom_point(mapping = aes(x = Concentration, y = Element, color=Group),  show.legend = FALSE) +
  coord_flip() +
  scale_x_log10()

Analysis    Name        Element Concentration
HM030218-2  Haycock Upper   La  65.00   
HM030218-2  Haycock Upper   Ce  127.00  
HM030218-2  Haycock Upper   Pr  13.46   
HM030218-2  Haycock Upper   Nd  44.00   
HM030218-2  Haycock Upper   Sm  6.70    
HM030218-2  Haycock Upper   Eu  0.75    
HM030218-2  Haycock Upper   Gd  4.48    
HM030218-2  Haycock Upper   Tb  0.64    
HM030218-2  Haycock Upper   Dy  3.40    
HM030218-2  Haycock Upper   Ho  0.73    
1-10 of 14 rows

Something similar to the expected result is listed above, while the actual result is here:https://docs.google.com/document/d/1p7QY8Ie_bmav1XApTSy1TCECvteUcxckZXpsy9Ib7Ew/edit?usp=sharing

Please forgive me also for not knowing how to upload the screenshots on here.

  • I'll fix that. I just added the excel generated plot to the google doc – McKenna Holliday Jun 18 '19 at 00:31
  • 1
    Your description suggests that the x-axis data (`Concentration`) came in as data formatted as `character` or `factor`, and not numeric. Please include the output of `dput(head(dataMGSREE))` in the body of your question so we can reproduce your issue. – Jon Spring Jun 18 '19 at 00:33
  • @JonSpring Hopefully that helps! – McKenna Holliday Jun 18 '19 at 00:39
  • The reason I requested the output of `dput` is that it allows other people to recreate a table with the same data formats as yours. For instance, I can't tell from what you pasted whether the first Concentration of `65.00` is stored as character (text) or a named factor or numeric, and that distinction is often a crucial thing to know for debugging R questions. – Jon Spring Jun 18 '19 at 00:46
  • Is there any particular reason for using `coord_flip()` here? It looks like you could leave that out and just have `aes(x = Element, y = Concentration, ...)` instead. I think it's also confusing people as to whether you're having issues with `Element` or `Concentration`. – Marius Jun 18 '19 at 00:49

2 Answers2

1

A few things going on

  • (a) If you want lines, you need to add geom_line() to your plot. You'll also need to add a group aesthetic to indicate which points to connect, presumably group = Analysis inside aes(). This is necessary whenever you plot a line with a discrete variable on an axis.
  • (b) See this FAQ for getting a custom order of your elements.
  • (c) If you want points and lines, put aes() inside the original ggplot() call, it will be passed on to both geom_point() and geom_line() so you don't have to re-specify it in subsequent layers
  • (d) I don't see a reason to use coord_flip here, I'd just map what you want to go on x and y from the start
  • (e) You don't show a column called Group in your data, so I'm surprised your color = Group works at all...

Something like this:

# change factor levels to order they occur
# you could also custom-specify an order, with, e.g., `levels = c("Li", "Ce", "Pr", ...)`
dataMGSREE$Element = factor(dataMGSREE$Element, levels = unique(dataMGSREE$Element))

# plot with changes explained above
ggplot(data = dataMGSREE,
  mapping = aes(x = Element, y = Concentration, color = Analysis, group = Analysis)) +
  geom_point(show.legend = FALSE) +
  geom_line() +
  scale_y_log10()
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thank you so much! I was having issue initially with using characters for my x-axis. I made a quick fix with the coord_flip, but I am glad that there was a more sufficient response. I filtered some of my columns for clarity, but I see where the confusion for the group column came from. – McKenna Holliday Jun 18 '19 at 00:57
  • Thanks, I certainly appreciate you *not* posting a bunch of irrelevant columns! – Gregor Thomas Jun 18 '19 at 00:59
0

The axis ordering for discrete data like Element is determined by how the factor levels are set. It looks like here the factor levels should be in the same order they already are in the data, so you can do:

dataMGSREE$Element = factor(dataMGSREE$Element, levels = dataMGSREE$Element)

ggplot(data=dataMGSREE) +
    # I set color = Analysis here because the example data didn't
    # contain a Group column, replace as appropriate
    geom_point(mapping = aes(x = Concentration, y = Element, color=Analysis),  
               show.legend = FALSE) +
    coord_flip() +
    scale_x_log10()
Marius
  • 58,213
  • 16
  • 107
  • 105