0

我正在尝试制作一个稀土元素蜘蛛图,将浓度放在 y 轴上的 log10 中,并将稀土元素中的每个元素放在 x 轴上。然后我试图比较几个单位的岩石。我正在寻找什么以及我得到什么的一个例子被添加到下面的谷歌文档链接中。

因此,对于我添加的代码,我有两个问题: 1. 元素按字母顺序排列在 x 轴上,而不是我在 CSV 中的顺序 2. 我不知道我错过了什么在我的代码中将每个样本中的点关联在一起以构建一条线。我不知道这是否与我的代码有关,或者与我的数据在 CSV 中的排列方式有关。

我见过其他人通过将各自的元素视为日期来解决这个问题。我玩过 lubridate 一点,但我觉得它不像我在下面添加的代码那么成功......这说明了一些事情。

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

上面列出了类似于预期结果的内容,而实际结果在这里:https ://docs.google.com/document/d/1p7QY8Ie_bmav1XApTSy1TCECvteUcxckZXpsy9Ib7Ew/edit?usp=sharing

请原谅我不知道如何在这里上传屏幕截图。

4

2 回答 2

1

发生了一些事情

  • (a) 如果你想要线条,你需要添加geom_line()到你的情节中。您还需要添加一个组美学来指示要连接的点,大概是group = Analysisaes(). 每当您在轴上绘制带有离散变量的线时,这都是必需的。
  • (b) 请参阅此常见问题解答以获取元素的自定义顺序。
  • (c) 如果你想要点线,放在aes()原来的ggplot()调用里面,它将被传递给两者geom_point()geom_line()所以你不必在后续层中重新指定它
  • (d)我看不出在coord_flip这里使用的理由,我只是从一开始就映射你想要继续的x东西y
  • (e) 你没有Group在你的数据中显示一个名为的列,所以我对你的color = Group作品感到惊讶......

像这样的东西:

# 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()
于 2019-06-18T00:52:11.817 回答
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()
于 2019-06-18T00:46:18.037 回答