堆叠密度图上的标签

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

labels on a stacked density plot

问题

以下是您要翻译的内容:

I'm generating a stacked density plot:

ggplot(data=tydy_rawdata, aes(x=timepoint, y=tpm, group=fct_inorder(names), 
    fill=fct_inorder(names))) +
         geom_density(position="fill",
                      stat="identity") +
         scale_fill_manual(values = rev(mycolors))

plot :
堆叠密度图上的标签

I would like to add label on each curve (or at least the top 3 or 4) basing on the "names" displayed on the right.

I'm trying adding geom_text but the result is this :

gplot(data=tydy_rawdata, aes(x=timepoint, y=tpm, group=fct_inorder(names), 
    fill=fct_inorder(names))) +
         geom_density(position="fill",
                      stat="identity") +
         geom_text(aes(label=names)) +
         scale_fill_manual(values = rev(mycolors))

plot :

堆叠密度图上的标签

Are there some way to do it?

英文:

I'm generating a stacked density plot:

ggplot(data=tydy_rawdata, aes(x=timepoint, y=tpm, group=fct_inorder(names), 
    fill=fct_inorder(names))) +
         geom_density(position="fill",
                      stat="identity") +
         scale_fill_manual(values = rev(mycolors))

plot :
堆叠密度图上的标签

I would like to add label on each curve (or at least the top 3 or 4) basing on the "names" displayed on the right.

I'm trying adding geom_text but the result is this :

gplot(data=tydy_rawdata, aes(x=timepoint, y=tpm, group=fct_inorder(names), 
    fill=fct_inorder(names))) +
         geom_density(position="fill",
                      stat="identity") +
         geom_text(aes(label=names)) +
         scale_fill_manual(values = rev(mycolors))

plot :

堆叠密度图上的标签

Are there some way to do it?

答案1

得分: 0

首先,你的图表是一个堆叠面积图,即geom_densitystat="identity"相当于geom_area。其次,当使用geom_text添加标签时,你需要考虑position参数。因为你在密度/面积图中使用了position="fill",所以在geom_text中也需要使用相同的设置。

由于你没有提供示例数据,我创建了自己的示例数据以使你的问题可以复现:

library(ggplot2)
library(forcats)

set.seed(123)

tydy_rawdata <- data.frame(
  names = rep(LETTERS[1:10], each = 6),
  timepoint = factor(seq(6)),
  tpm = runif(6 * 10, 0, 80)
)

ggplot(data = tydy_rawdata, aes(
  x = timepoint, y = tpm,
  group = fct_inorder(names), fill = fct_inorder(names)
)) +
  geom_area(
    position = "fill",
    color = "black"
  ) +
  geom_text(aes(label = names), position = "fill")

堆叠密度图上的标签

英文:

First, your chart is a stacked area chart, i.e. geom_density with stat=&quot;identity&quot; is equal to geom_area. Second, when adding labels via geom_text you have to take account of the position argument. As you use position=&quot;fill&quot; for your density/area chart you also have to do the same for geom_text.

As you provided no example data I created my own to make your issue reproducible:

library(ggplot2)
library(forcats)

set.seed(123)

tydy_rawdata &lt;- data.frame(
  names = rep(LETTERS[1:10], each = 6),
  timepoint = factor(seq(6)),
  tpm = runif(6 * 10, 0, 80)
)

ggplot(data = tydy_rawdata, aes(
  x = timepoint, y = tpm,
  group = fct_inorder(names), fill = fct_inorder(names)
)) +
  geom_area(
    position = &quot;fill&quot;,
    color = &quot;black&quot;
  ) +
  geom_text(aes(label = names), position = &quot;fill&quot;)

堆叠密度图上的标签<!-- -->

huangapple
  • 本文由 发表于 2023年2月8日 19:24:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385096.html
匿名

发表评论

匿名网友

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

确定