在条形图下显示值表

时间:2017-06-04 19:54:19

标签: r ggplot2

我问这个没有找到尝试的东西,因为我没有找到相同的东西。我为此道歉。

来自this条形图:

df <- structure(list(year = 2002:2005, work = c(1L, 2L, 3L, 2L), confid = c(8L, 
5L, 0L, 6L), jrs = c(0L, 3L, 4L, 5L)), .Names = c("year", "work", 
"confid", "jrs"), class = "data.frame", row.names = c(NA, -4L
))

library(ggplot2)
library(reshape)
md <- melt(df, id=(c("year")))
temp.plot <- ggplot(data=md, aes(x=year, y=value, fill=variable) ) + 
    geom_bar(stat="identity")+ 
    theme(axis.text.x=element_text(angle=90))+ 
    ggtitle("Score Distribtion")

temp.plot

我想问一下,是否有任何简单的方法使用ggplot2来获得每年的值,因为它在每个变量的barplot中。 这里有一个虚拟示例输出: enter image description here

1 个答案:

答案 0 :(得分:10)

在每个条形图中绘制计数可能更好。例如:

tableGrob

the documentation

如果您仍然想要一个表格下方的表格,我不知道一个简单的方法,但您可以为表格创建一个单独的library(grid) library(gridExtra) # Function to extract legend # https://stackoverflow.com/a/13650878/496488 g_legend <- function(a.gplot){ tmp <- ggplot_gtable(ggplot_build(a.gplot)) leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") legend <- tmp$grobs[[leg]] return(legend)} p = ggplot(data=md, aes(x=year, y=value, fill=variable) ) + geom_bar(stat="identity")+ #theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=0.5))+ ggtitle("Score Distribution") + labs(fill="") # Extract the legend as a separate grob leg = g_legend(p) # Create a table grob tab = t(df) tab = tableGrob(tab, rows=NULL) tab$widths <- unit(rep(1/ncol(tab), ncol(tab)), "npc") # Lay out plot, legend, and table grob grid.arrange(arrangeGrob(nullGrob(), p + guides(fill=FALSE) + theme(axis.text.x=element_blank(), axis.title.x=element_blank(), axis.ticks.x=element_blank()), widths=c(1,8)), arrangeGrob(arrangeGrob(nullGrob(),leg,heights=c(1,10)), tab, nullGrob(), widths=c(6,20,1)), heights=c(4,1)) ,将图例作为单独的grob(图形对象)提取,然后分别布置每个部分。布置各个部分需要手动调整,虽然比我更了解网格图形的人可能能够自动化。这是一个例子:

import re

special_chars = ["?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+"]
uri = "photo's url.jpeg"

#use str.replace
for i in special_chars:
    uri = uri.replace(i, "")

#or re.sub
#uri = re.sub("\?|\[|\]|/|\\|\=|<|>|:|;|,|'|\"|\&|\$|#|\*|\(|\)|~|`|!|\{|\}|%|\+", "", uri)

uri = re.sub("\s+", "-", uri)
print(uri)

enter image description here