我正在尝试将百分比添加到每个条形图:
# 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
提前,谢谢!
答案 0 :(得分:2)
为了说明“代码样式”注释:
# 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
)
所以^^仍然坏了,但是 kinda 可读(它仍然是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
的默认stat
为identity
。 geom_bar
的默认值为count
。即使您已解决此问题,如何ValueG
总结这三个组?而且,如果这是正确的图形输出,那么您想在哪里贴标签?上衣?居中?
我强烈建议您重组源代码,然后在ggplot2之外计算必要的值和组,并使用geom_col
和geom_bar
。