为 ggplot 中的特定组添加标签

huangapple go评论53阅读模式
英文:

Adding labels to a specific group in ggplot

问题

我想为散点图中的特定组(鸟类)添加标签,该散点图包含多个组。

我尝试了https://stackoverflow.com/questions/15015356/how-to-do-selective-labeling-with-ggplot-geom-point 中的一些建议,但没有成功。下面的代码获取了所有组的标签,并且看起来非常杂乱。由于存在误差棒,我可能应该将标签移动到上方并向上移动以避免误差棒。

谢谢,

Jeff

p <- ggplot(data = NescoA_sum, aes(
  x = d13C_mean,
  y = d15N_mean,
  color=Class, shape=Class)) + 
  geom_point(stroke=2) +
  geom_errorbar(aes(ymin = d15N_mean - d15N_sd, ymax = d15N_mean + d15N_sd)) +
  geom_errorbarh(aes(xmin = d13C_mean - d13C_sd, xmax = d13C_mean + d13C_sd))
p + ggtitle("Nescopeck Warm Season") +
  xlab("dC13") + ylab("d15N")
p+ geom_text(data=subset(NescoA_sum, Class="Aves"),
          aes(x = d13C_mean, y = d15N_mean,label=Genus))

数据

structure(list(Class = c("Arachnida", "Arachnida", "Aves", "Aves", 
"Aves", "Aves", "Aves", "Aves", "Aves", "Aves", "Aves", "Aves", 
"Aves", "Aves", "Diplopoda", "Gastropoda", "Insecta", "Insecta", 
"Insecta", "Insecta", "Insecta", "Insecta", "Insecta", "Insecta", 
"Insecta", "Insecta", "Insecta", "Insecta", "Insecta", "Insecta", 
"Insecta", "Insecta", "Liliopsida", "Magnoliopsida", "Magnoliopsida", 
"Mammalia", "Mammalia", "Mammalia", "Mammalia"), Genus = c("(Lycosidae)", 
NA, "Catharus ", "Dumetella", "Empidonax", "Geothylpis", "Melospiza", 
"Passerina", "Pipilo", "Spizella", "Toxostoma", "Troglodytes", 
"Turdus", "Vireo ", "Cambala", "(Gastropoda)", "(Acrididae)", 
"(Scolytidae)", "Anasa", "Aphaenogaster", "Atlanticus", "Conocephalus", 
"Forficula", "Formica", "Melanolestes", "Melanoplus", "Metrioptera", 
"Myrmica", "Oulema", "Phyciodes", "Phyllopalpus", NA, "Andropogon", 
"Achillea", "Solidago", "Blarina", "Microtus", "Peromyscus", 
"Zapus"), d15N_mean = c(4.32, 5.235, 3.83, 5.068, 3.98666666666667, 
3.18, 5.22, 2.86, 4.39, 4.17, 4.84, 5.32, 3.44, 2.43, 2.31, 3.2, 
2.31, 2.46, 0.94, 4.16, 1.68, 1.32, -0.9, 7.22, 1.47, 0.26, 2.575, 
4.29, 0.32, 0.995, 2.435, 2.485, NA, 0.28, -1.54, 6.11, 4.30875, 
3.87666666666667, 4.13333333333333), d13C_mean = c(-25.07, -24.055, 
-24.92, -24.628, -23.9733333333333, -24.2, -24.8466666666667, 
-23.64, -25.28, -22.34, -24.21, -24.45, -24.285, -25.09, -19.95, 
-24.56, -27.94, -18.54, -26.18, -17.72, -24, -21.41, -24.75, 
-25.42, -25.81, -23.04, -27.265, -24.19, -21.935, -15.34, -19.865, 
-23.7664285714286, -11.915, -31.65, -30.55, -22.0733333333333, 
-22.98875, -22.81, -23.92), d15N_sd = c(NA, 1.46371103705615, 
NA, 0.939079336371534, 1.09367880720682, 0.74953318805774, 0.762692598626734, 
NA, NA, NA, NA, NA, 0.636396103067893, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 0.742462120245875, NA, 0.834386001800126, 
0.261629509039022, 0.912167747730646, 2.35327840060823, NA, NA, 
1.04651803615609, 1.25729869164014, 0.398405303320985, 1.60593690203984, 
0.849548899907082), d13C_sd = c(NA, 1.71826947828331, NA, 0.60363068179144, 
0.285014619508076, 0.127279220613578, 0.209841209807163, NA, 
NA, NA, NA, NA, 0.219203102167831, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 0.0212132034355972, NA, 3.34461507501237, 
3.62038671967512, 0.51618795026618, 5.32832143743781, 0.0919238815542517, 
NA, 

<details>
<summary>英文:</summary>

I would like to add labels for a specific group (birds) in a scatterplot with several groups. 

I tried the several suggestions in https://stackoverflow.com/questions/15015356/how-to-do-selective-labeling-with-ggplot-geom-point but no success. The code below gets the labels but for all groups and looks really noisy. Since there are error bars I probably should move the labels up and over to avoid the error bars. 

Thanks, 

Jeff

```p &lt;- ggplot(data = NescoA_sum, aes(
  x = d13C_mean,
  y = d15N_mean,
  color=Class, shape=Class)) + 
  geom_point(stroke=2) +
  geom_errorbar(aes(ymin = d15N_mean - d15N_sd, ymax = d15N_mean + d15N_sd)) +
  geom_errorbarh(aes(xmin = d13C_mean - d13C_sd, xmax = d13C_mean + d13C_sd))
p + ggtitle(&quot;Nescopeck Warm Season&quot;) +
  xlab(&quot;dC13&quot;) + ylab(&quot;d15N&quot;)
p+ geom_text(data=subset(NescoA_sum, Class=&quot;Aves&quot;),
          aes(x = d13C_mean, y = d15N_mean,label=Genus))

The data

structure(list(Class = c(&quot;Arachnida&quot;, &quot;Arachnida&quot;, &quot;Aves&quot;, &quot;Aves&quot;, 
&quot;Aves&quot;, &quot;Aves&quot;, &quot;Aves&quot;, &quot;Aves&quot;, &quot;Aves&quot;, &quot;Aves&quot;, &quot;Aves&quot;, &quot;Aves&quot;, 
&quot;Aves&quot;, &quot;Aves&quot;, &quot;Diplopoda&quot;, &quot;Gastropoda&quot;, &quot;Insecta&quot;, &quot;Insecta&quot;, 
&quot;Insecta&quot;, &quot;Insecta&quot;, &quot;Insecta&quot;, &quot;Insecta&quot;, &quot;Insecta&quot;, &quot;Insecta&quot;, 
&quot;Insecta&quot;, &quot;Insecta&quot;, &quot;Insecta&quot;, &quot;Insecta&quot;, &quot;Insecta&quot;, &quot;Insecta&quot;, 
&quot;Insecta&quot;, &quot;Insecta&quot;, &quot;Liliopsida&quot;, &quot;Magnoliopsida&quot;, &quot;Magnoliopsida&quot;, 
&quot;Mammalia&quot;, &quot;Mammalia&quot;, &quot;Mammalia&quot;, &quot;Mammalia&quot;), Genus = c(&quot;(Lycosidae)&quot;, 
NA, &quot;Catharus &quot;, &quot;Dumetella&quot;, &quot;Empidonax&quot;, &quot;Geothylpis&quot;, &quot;Melospiza&quot;, 
&quot;Passerina&quot;, &quot;Pipilo&quot;, &quot;Spizella&quot;, &quot;Toxostoma&quot;, &quot;Troglodytes&quot;, 
&quot;Turdus&quot;, &quot;Vireo &quot;, &quot;Cambala&quot;, &quot;(Gastropoda)&quot;, &quot;(Acrididae)&quot;, 
&quot;(Scolytidae)&quot;, &quot;Anasa&quot;, &quot;Aphaenogaster&quot;, &quot;Atlanticus&quot;, &quot;Conocephalus&quot;, 
&quot;Forficula&quot;, &quot;Formica&quot;, &quot;Melanolestes&quot;, &quot;Melanoplus&quot;, &quot;Metrioptera&quot;, 
&quot;Myrmica&quot;, &quot;Oulema&quot;, &quot;Phyciodes&quot;, &quot;Phyllopalpus&quot;, NA, &quot;Andropogon&quot;, 
&quot;Achillea&quot;, &quot;Solidago&quot;, &quot;Blarina&quot;, &quot;Microtus&quot;, &quot;Peromyscus&quot;, 
&quot;Zapus&quot;), d15N_mean = c(4.32, 5.235, 3.83, 5.068, 3.98666666666667, 
3.18, 5.22, 2.86, 4.39, 4.17, 4.84, 5.32, 3.44, 2.43, 2.31, 3.2, 
2.31, 2.46, 0.94, 4.16, 1.68, 1.32, -0.9, 7.22, 1.47, 0.26, 2.575, 
4.29, 0.32, 0.995, 2.435, 2.485, NA, 0.28, -1.54, 6.11, 4.30875, 
3.87666666666667, 4.13333333333333), d13C_mean = c(-25.07, -24.055, 
-24.92, -24.628, -23.9733333333333, -24.2, -24.8466666666667, 
-23.64, -25.28, -22.34, -24.21, -24.45, -24.285, -25.09, -19.95, 
-24.56, -27.94, -18.54, -26.18, -17.72, -24, -21.41, -24.75, 
-25.42, -25.81, -23.04, -27.265, -24.19, -21.935, -15.34, -19.865, 
-23.7664285714286, -11.915, -31.65, -30.55, -22.0733333333333, 
-22.98875, -22.81, -23.92), d15N_sd = c(NA, 1.46371103705615, 
NA, 0.939079336371534, 1.09367880720682, 0.74953318805774, 0.762692598626734, 
NA, NA, NA, NA, NA, 0.636396103067893, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 0.742462120245875, NA, 0.834386001800126, 
0.261629509039022, 0.912167747730646, 2.35327840060823, NA, NA, 
1.04651803615609, 1.25729869164014, 0.398405303320985, 1.60593690203984, 
0.849548899907082), d13C_sd = c(NA, 1.71826947828331, NA, 0.60363068179144, 
0.285014619508076, 0.127279220613578, 0.209841209807163, NA, 
NA, NA, NA, NA, 0.219203102167831, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 0.0212132034355972, NA, 3.34461507501237, 
3.62038671967512, 0.51618795026618, 5.32832143743781, 0.0919238815542517, 
NA, 0.183847763108501, 3.12616911464069, 1.69705995769154, 0.523736575006939, 
0.444409720865778)), class = c(&quot;grouped_df&quot;, &quot;tbl_df&quot;, &quot;tbl&quot;, 
&quot;data.frame&quot;), row.names = c(NA, -39L), groups = structure(list(
Class = c(&quot;Arachnida&quot;, &quot;Aves&quot;, &quot;Diplopoda&quot;, &quot;Gastropoda&quot;, 
&quot;Insecta&quot;, &quot;Liliopsida&quot;, &quot;Magnoliopsida&quot;, &quot;Mammalia&quot;), .rows = structure(list(
1:2, 3:14, 15L, 16L, 17:32, 33L, 34:35, 36:39), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;, 
&quot;vctrs_vctr&quot;, &quot;list&quot;))), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;
), row.names = c(NA, -8L), .drop = TRUE))
</details>
# 答案1
**得分**: 2
```p + geom_text(aes(label = Genus), data = subset(df, Class == "Aves"), vjust = 1.5, hjust = 1.5)```
<details>
<summary>英文:</summary>
```p + geom_text(aes(label = Genus), data = subset(df, Class == &quot;Aves&quot;), vjust = 1.5, hjust = 1.5)```
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/bQMl9.png
</details>

huangapple
  • 本文由 发表于 2023年6月19日 08:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定