希望這不是脫離主題。自定義條形圖 - 將條形圖與圖表下面的表格對齊
我嘗試從this偉大的答案定製代碼使用額外的數據:
df <- structure(list(year = 1998:2007, work = c(0L, 4L, 0L, 1L, 0L, 0L, 1L, 2L, 2L, 3L), confid = c(0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 3L, 0L), jrs = c(0L, 1L, 0L, 0L, 1L, 9L, 6L, 4L, 21L, 2L)), .Names = c("year", "work", "confid", "jrs"), class = "data.frame", row.names = c(NA, -10L))
library(ggplot2)
library(reshape)
md <- melt(df, id=(c("year")))
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))
我試圖在最後更改數字,但我再次收到相同的結果。表格比圖表小,圖表上的橫槓不在正確位置。
我怎樣才能最終定製數字?
在圖像1我檢查,因爲要在酒吧與1999年的同一行,現在看來2年。圖2中的2顯示錶格必須與情節具有相同的寬度,並且年份應該從每個對應的小節開始下降。
你發的問題,多米諾骨牌。這並不一定是壞事。您發佈另一個問題,而不是在一篇文章中不斷詢問/編輯您的問題的觀點是很好的做法。 +1,也是你的好問題。關於你的問題,嘗試改變'widths'。 – Masoud
你想要改變什麼(和eipi10的答案相比,你還有什麼改變,還有新的數據框)?如果我運行前例,這些酒吧似乎按照我的預期排列(?)。 – lukeA