【问题标题】:Add % to the barplot ggplot2将 % 添加到条形图 ggplot2
【发布时间】:2018-12-16 12:01:21
【问题描述】:

我正在尝试将百分比添加到每个条形:

# Set up the work directory in which all data is gonna be extracted
H1517 = read.csv("HiBAPMapGraph.csv") #Change name of the file
library(ggplot2)

# Histogram on a Categorical variable
p <- ggplot(H1517, aes(Chromosome)) + geom_bar(aes(fill=Genome), width = 
0.5) + scale_fill_manual("Genome", values = c("A" = "chartreuse3", "B" = 
"darkorange1 ", "D" = "gold1"))  
theme(axis.text.x = element_text(angle=65, vjust=0.6, face="bold", size=12), 
axis.text.y = element_text(face="bold", size=10)) 
p.labs <- p + labs(x = "Chromosome", y = "# markers")
red.bold.italic.text <- element_text(face = "bold", size = 10)
p.labs + theme(title = red.bold.italic.text, axis.title = 
red.bold.italic.text) + scale_x_continuous(breaks=seq(1,7,1)) +  
scale_y_continuous(breaks=seq(0,1800,200) + geom_text(aes(label = 
paste0(ValueG*100,"%")), position = position_stack(vjust = 0.5), size = 2)                                                     

但是下一条消息来了:

List of 2
 $ axis.text.x:List of 11
  ..$ family       : NULL
  ..$ face         : chr "bold"
  ..$ colour       : NULL
  ..$ size         : num 12
  ..$ hjust        : NULL
  ..$ vjust        : num 0.6
  ..$ angle        : num 65
  ..$ lineheight   : NULL
  ..$ margin       : NULL
  ..$ debug        : NULL
  ..$ inherit.blank: logi FALSE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ axis.text.y:List of 11
  ..$ family       : NULL
  ..$ face         : chr "bold"
  ..$ colour       : NULL
  ..$ size         : num 10
  ..$ hjust        : NULL
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : NULL
  ..$ debug        : NULL
  ..$ inherit.blank: logi FALSE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 - attr(*, "class")= chr [1:2] "theme" "gg"
 - attr(*, "complete")= logi FALSE
 - attr(*, "validate")= logi TRUE

我没有得到我的图表! :( 知道我做错了什么吗?

这是一些数据:

    Chromosome  Genome  ValueG  ValueChr
AX-94493709 1   A   0.047264487 0.179561886
AX-94913549 1   A   0.047264487 0.179561886
AX-94856564 1   A   0.047264487 0.179561886
AX-95182909 1   B   0.098197907 0.179561886
AX-94667633 1   B   0.098197907 0.179561886
AX-94944833 1   B   0.098197907 0.179561886
AX-94793453 1   D   0.034099493 0.179561886
AX-95079458 1   D   0.034099493 0.179561886
AX-95072382 1   D   0.034099493 0.179561886

提前,谢谢!

【问题讨论】:

  • 是由1s 组成的染色体列还是包含整个 AX-something?
  • 1 是数据的一部分(另一列)
  • 所以请编辑问题,使您的数据有 5 列
  • 您缺乏代码样式在很大程度上导致了您的困惑,并且 - 除其他外 - 您缺少)
  • 还有一些。如果问题与定位文本标签无关,则应该(并且可能仍然应该)将其作为“错字”关闭,尤其是因为它在当前形式下不可重现。

标签: r ggplot2 bar-chart


【解决方案1】:

为了说明“代码样式”注释:

# perhaps have all library() calls up front
library(ggplot2)

# perhaps keep consistent assignment operators
H1517 <- read.table(text = "Chromosome  Genome  ValueG  ValueChr
AX-94493709 1   A   0.047264487 0.179561886
AX-94913549 1   A   0.047264487 0.179561886
AX-94856564 1   A   0.047264487 0.179561886
AX-95182909 1   B   0.098197907 0.179561886
AX-94667633 1   B   0.098197907 0.179561886
AX-94944833 1   B   0.098197907 0.179561886
AX-94793453 1   D   0.034099493 0.179561886
AX-95079458 1   D   0.034099493 0.179561886
AX-95072382 1   D   0.034099493 0.179561886
")

# Some concept of formatting would have made the original block readable
p <- ggplot(H1517, aes(Chromosome)) +
  geom_bar(aes(fill = Genome), width = 0.5) +
  scale_fill_manual(
    name = "Genome",
    values = c(
      "A" = "chartreuse3", "B" = "darkorange1 ", "D" = "gold1"
    )
  )

# why is this dangling?
theme(
  axis.text.x = element_text(angle = 65, vjust = 0.6, face = "bold", size = 12),
  axis.text.y = element_text(face = "bold", size = 10)
)

p.labs <- p + labs(x = "Chromosome", y = "# markers")

red.bold.italic.text <- element_text(face = "bold", size = 10)

# Formatting this in some basic way wld have likely enabled you to discover the missing `)`
p.labs + theme(
  title = red.bold.italic.text,
  axis.title = red.bold.italic.text
) +
  scale_x_continuous(breaks = seq(1, 7, 1)) +
  scale_y_continuous(breaks = seq(0, 1800, 200)) +
  geom_text(
    aes(label = paste0(ValueG * 100, "%")),
    position = position_stack(vjust = 0.5), size = 2
  )

所以 ^^ 仍然损坏,但 有点 可读(它仍然是 copypasta 的大杂烩)。

让我们改变它:

library(ggplot2)

H1517 <- read.table(text = "Chromosome  Genome  ValueG  ValueChr
AX-94493709 1   A   0.047264487 0.179561886
AX-94913549 1   A   0.047264487 0.179561886
AX-94856564 1   A   0.047264487 0.179561886
AX-95182909 1   B   0.098197907 0.179561886
AX-94667633 1   B   0.098197907 0.179561886
AX-94944833 1   B   0.098197907 0.179561886
AX-94793453 1   D   0.034099493 0.179561886
AX-95079458 1   D   0.034099493 0.179561886
AX-95072382 1   D   0.034099493 0.179561886
")

red.bold.italic.text <- element_text(face = "bold", size = 10)

ggplot(H1517, aes(x=Chromosome)) +
  geom_bar(aes(fill = Genome), width = 0.5) +
  # geom_text(
  #   aes(label = paste0(ValueG * 100, "%")),
  #   position = position_stack(vjust = 0.5), size = 2
  # ) +
  scale_x_continuous(breaks = seq(1, 7, 1)) +
  scale_y_continuous(breaks = seq(0, 1800, 200)) +
  scale_fill_manual(
    name = "Genome",
    values = c(
      "A" = "chartreuse3", "B" = "darkorange1 ", "D" = "gold1"
    )
  ) +
  labs(x = "Chromosome", y = "# markers") +
  theme(
    axis.text.x = element_text(angle = 65, vjust = 0.6, face = "bold", size = 12),
    axis.text.y = element_text(face = "bold", size = 10),
    title = red.bold.italic.text,
    axis.title = red.bold.italic.text
  )

注意:我们已将 geom_text() 注释掉,因为它已损坏。

那么,上面的图片是你要找的没有标签的吗?

如果是这样,ggplot2 应该如何知道如何处理ValueG?:

geom_text(
  aes(label = paste0(ValueG * 100, "%")),
  position = position_stack(vjust = 0.5), size = 2
)

geom_text 的默认 statidentitygeom_bar 的默认值为 count。即使您解决了这个问题,ValueG 应该如何为这三个组进行汇总?而且,如果这是正确的图形输出,你想要标签在哪里?最高额?居中?

我强烈建议您重构源代码,然后在 ggplot2 之外计算必要的值和组,并使用 geom_colgeom_bar

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2021-07-17
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多