McKenna Holliday
McKenna Holliday

Reputation: 1

What am I missing to build this plot?

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.

Upvotes: 0

Views: 263

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 145755

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()

Upvotes: 1

Marius
Marius

Reputation: 60060

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()

Upvotes: 0

Related Questions