【发布时间】:2016-06-20 19:00:17
【问题描述】:
我正在使用ggplot2、plotly 和ggthemes 创建数据摘要条形图。这是我的代码:
# data
dput(dat)
structure(list(Source = structure(1:11, .Label = c("GTEx", "Target",
"RNASeq", "Microarray", "CNA", "Mutation", "HI51", "IH250", "IH251",
"NB88", "OBER649"), class = "factor"), Type = c("External", "External",
"Cell Line", "Cell Line", "Cell Line", "Cell Line", "Patient Sample",
"Patient Sample", "Patient Sample", "Patient Sample", "Patient Sample"
), Samples = c(1641, 162, 41, 29, 34, 29, 51, 250, 251, 88, 649
), Genes = c(52576, 25818, 26770, 19822, 21376, 12716, 21118,
17768, 17768, 21118, 19858)), .Names = c("Source", "Type", "Samples",
"Genes"), class = "data.frame", row.names = c(NA, -11L))
library(ggplot2)
library(ggthemes)
library(plotly)
p <- ggplot(data = dat, aes(x = Source, y = Genes)) +
geom_bar(stat="identity",aes(color = Type)) +
theme_bw() +
geom_text(aes(label = Samples, color = Type), position=position_dodge(width=0.9), vjust=-0.25, size=5) +
theme_hc(bgcolor = "darkunica") +
scale_colour_hc("darkunica") +
theme(axis.title = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
p <- plotly_build(p)
p$layout$plot_bgcolor <- p$layout$paper_bgcolor
p$layout$annotations[[1]]$text <- ""
这是创建的情节:
条上的标签显示样本数。当我将鼠标悬停在上面时 - 它会显示 Source、Genes 和 Type。即使我在geom_text 中使用position 和vjust 参数,它也不起作用。如何调整条形上方的标签,使其可见,即完全在条形上方或下方?
【问题讨论】:
-
你可以看看here
-
产生
Error: Aesthetics must be either length 1 or the same as the data (11): label, colour, position, vjust, x, y -
@dww 将
geom_text()中的内容替换为此geom_text(aes(label = Samples, color = Type), position=position_dodge(width=0.9), vjust=-0.25, size=5) -
请注意:这里的数据墨水比不好。
-
@zx8754 你能详细说明一下吗?谢谢