ggplot 有两个 x 轴文本堆栈。

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

ggplot having two stacks of x axis text

问题

以下是您要翻译的部分:

"So my current plot looks like this:
ggplot 有两个 x 轴文本堆栈。"

"But I want to add one or two other series of text to the plot to look something like this:
ggplot 有两个 x 轴文本堆栈。"

Is that even possible to do such a thing using ggplot or should I go after another package?

Update:
Here is the code for my current plot:

ggplot() + geom_col(data = s, aes(x = V1, y = V3, fill = V3), show.legend = F) +
coord_flip() +
labs(x = "", y = "") +
scale_fill_gradient(low = "yellow", high="darkgreen") +
theme(axis.text.y = element_text(vjust = 0, hjust = 0), plot.margin = margin(1, 0.3, 0.1, 0.3, "cm"), panel.background = element_rect(fill = 'white', colour = 'white'), axis.ticks.y = element_blank()) + ylim(0, 6)

and here is some fake data:

1 a b 6 7
2 c d 8 9
3 g f 5 4
4 f n 3 2

and here is the output of the real data:

structure(list(V1 = c("regulation of locomotion", "regulation of cell migration", 
"skin development", "negative regulation of biological process", 
"cell adhesion", "response to oxygen-containing compound"), V2 = c(7.74e-05, 
0.000143, 0.000165, 0.000176, 0.00019, 0.00019), V3 = c(4.111259039, 
3.844663963, 3.782516056, 3.754487332, 3.721246399, 3.721246399
)), row.names = c(NA, 6L), class = "data.frame")
英文:

So my current plot looks like this:
ggplot 有两个 x 轴文本堆栈。

But I want to add one or two other series of text to the plot to look something like this:

ggplot 有两个 x 轴文本堆栈。

Is that even possible to do such a thing using ggplot or should I go after another package?

Update:
Here is the code for my current plot:

ggplot() + geom_col(data = s, aes(x = V1, y = V3, fill = V3), show.legend = F) + 
coord_flip() + 
labs(x = "", y = "") + 
scale_fill_gradient(low = "yellow", high="darkgreen") + 
theme(axis.text.y = element_text(vjust = 0, hjust = 0), plot.margin = margin(1, 0.3, 0.1, 0.3, "cm"), panel.background = element_rect(fill = 'white', colour = 'white'), axis.ticks.y = element_blank()) + ylim(0, 6)

and here is some fake data:

1  a  b  6  7
2  c  d  8  9
3  g  f  5  4
4  f  n  3  2

and here is the output of the real data:

structure(list(V1 = c("regulation of locomotion", "regulation of cell migration", 
"skin development", "negative regulation of biological process", 
"cell adhesion", "response to oxygen-containing compound"), V2 = c(7.74e-05, 
0.000143, 0.000165, 0.000176, 0.00019, 0.00019), V3 = c(4.111259039, 
3.844663963, 3.782516056, 3.754487332, 3.721246399, 3.721246399
)), row.names = c(NA, 6L), class = "data.frame")

答案1

得分: 1

在只有一列的情况下,添加第二列作为轴文本的一个简单选项是通过 geom_text 图层,如下所示。您只需确保设置正确的大小和颜色,就像轴文本一样:

library(ggplot2)

ggplot(data = s) +
  geom_col(aes(x = V3, y = V1, fill = V3), show.legend = F) +
  geom_text(aes(x = -1, y = V1, label = scales::label_scientific()(V2)),
    hjust = 0, size = .8 * 11 / .pt, vjust = 0, color = "grey30"
  ) +
  labs(x = "", y = "") +
  scale_fill_gradient(low = "yellow", high = "darkgreen") +
  theme(
    axis.text.y = element_text(vjust = 0, hjust = 0),
    plot.margin = margin(1, 0.3, 0.1, 0.3, "cm"),
    panel.background = element_rect(fill = "white", colour = "white"),
    axis.ticks.y = element_blank()
  ) +
  xlim(-1, 6)

ggplot 有两个 x 轴文本堆栈。

英文:

In case of just one column one of the easiest options would be to add the second column for your axis text via a geom_text layer like so. You only have to make sure to set the right size and color as for the axis text:

library(ggplot2)

ggplot(data = s) +
  geom_col(aes(x = V3, y = V1, fill = V3), show.legend = F) +
  geom_text(aes(x = -1, y = V1, label = scales::label_scientific()(V2)),
    hjust = 0, size = .8 * 11 / .pt, vjust = 0, color = "grey30"
  ) +
  labs(x = "", y = "") +
  scale_fill_gradient(low = "yellow", high = "darkgreen") +
  theme(
    axis.text.y = element_text(vjust = 0, hjust = 0),
    plot.margin = margin(1, 0.3, 0.1, 0.3, "cm"),
    panel.background = element_rect(fill = "white", colour = "white"),
    axis.ticks.y = element_blank()
  ) +
  xlim(-1, 6)

ggplot 有两个 x 轴文本堆栈。<!-- -->

huangapple
  • 本文由 发表于 2023年2月10日 03:09:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403333.html
匿名

发表评论

匿名网友

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

确定