簡體   English   中英

如何使用ggplot2制作條形圖

[英]How to make a bar plot like this with ggplot2

如何制作如下圖的條形圖?

圖。1

我已經嘗試過了,但是只能做到這一點:

圖2

這是我的代碼:

ggplot(N.Balance, aes(x = factor(Period), y = value)) + 
geom_bar(stat = "identity", aes( group = Type, fill = variable), osition = "stack", width = 0.6) +
facet_wrap(~ Type,ncol = 1) +
coord_flip() +
scale_fill_grey() + 
theme_bw(base_size = 30, base_family = "serif") + 
labs(y = expression(paste("kg", " ", "N", " ", ha^{-1}))) + 
theme(legend.key.height = unit(0.5, "in"))

數據如下所示:

structure(list(Period = c("2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W", 
"2009R", "2009/2010W"), 
variable = c("Denitrification", "Denitrification", "Denitrification", "Denitrification",
"Denitrification", "Denitrification", "Runoff", "Runoff", "Runoff", "Runoff", "Runoff", 
"Runoff", "Leaching", "Leaching", "Leaching", "Leaching", "Leaching", "Leaching", "NH3Vol", 
"NH3Vol", "NH3Vol", "NH3Vol", "NH3Vol", "NH3Vol", "Harvest", 
"Harvest", "Harvest", "Harvest", "Harvest", "Harvest", "Fertilizer", 
"Fertilizer", "Fertilizer", "Fertilizer", "Fertilizer", "Fertilizer", 
"Fix", "Fix", "Fix", "Fix", "Fix", "Fix", "Irrigation", "Irrigation", 
"Irrigation", "Irrigation", "Irrigation", "Irrigation", "Seeds", 
"Seeds", "Seeds", "Seeds", "Seeds", "Seeds", "Deposition", "Deposition", 
"Deposition", "Deposition", "Deposition", "Deposition"), 
value = c(-89.4, -34.4, -61.5, -82.5, -87.2, -34.7, -21.8, -33.4, -2.65, -42.8, 
-19.2, -58.7, -8.22, -1.44, -9.76, -4.76, -4.97, -19, -71.6, 
-50.8, -97.1, -10.9, -60.6, -19.6, -187, -116, -167, -96, -177, 
-127, 300, 200, 300, 200, 300, 200, 45, 15, 45, 15, 45, 15, 12.5, 
0, 11.6, 0, 11.3, 0, 0.9, 3, 0.9, 3, 0.9, 3, 8.41, 13.74, 4.01, 
13.34, 16.31, 9.81), Type = c("O", "O", "O", "O", "O", "O", "O", 
"O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", 
"O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "I", "I", "I", 
"I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", 
"I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I")), 
.Names = c("Period", "variable", "value", "Type"), class = "data.frame", 
row.names = c(NA,  -60L))

注意,類型“ O”表示“輸出”,“ I”表示“輸入”

進行我在注釋中要求的dput()可能會更有幫助,但我將print()輸出替換為其他可能想要貢獻的人。

至少有兩種其他方法(除了下面的一種)可以接近該圖表。 這依賴於一種新興的geom_ ,它使您可以使用不帶coord_flip() -這意味着您可以使用刻面比例尺:

library(ggplot2)
library(ggstance) # devtools::install_github("lionel-/ggstance")
library(dplyr)

N.Balance <- read.csv("~/Data/so.csv", stringsAsFactors=FALSE)
N.Balance$Period_f <- factor(N.Balance$Period)
N.Balance$Type_f <- factor(N.Balance$Type, 
                         levels=c("O", "I"),
                         labels=c("Output", "Input"))

gg <- ggplot(N.Balance, aes(x=value, y=Period_f))
gg <- gg + geom_barh(stat = "identity", 
                    aes(group = Type, fill = variable), 
                    position = "stack", width = 0.6)
gg <- gg + geom_text(data=data.frame(Period=unique(N.Balance$Period)),
                     aes(x=5, y=Period, label=Period),
                     color="white", hjust=0, size=3)
gg <- gg + scale_x_continuous(expand=c(0,-0.001))
gg <- gg + scale_fill_grey(name="")
gg <- gg + facet_wrap(~ Type_f, ncol=2, scales="free_x")
gg <- gg + guides(fill=guide_legend(keywidth = 2, keyheight = 1))
gg <- gg + labs(y=NULL, x = expression(paste("kg", " ", "N", " ", ha^{-1})))
gg <- gg + theme_bw()
gg <- gg + theme(legend.key.height = unit(0.5, "in"))
gg <- gg + theme(panel.background=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(panel.margin=margin(l=0, r=0))
gg <- gg + theme(legend.position="top")
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(legend.key=element_blank())
gg <- gg + theme(legend.text=element_text(size=8))
gg <- gg + theme()
gg

在此處輸入圖片說明

這個新的geom_barh()位於一個不在CRAN上的程序包中(因此),因此,如果有問題,至少有兩種方法可以解決。

另一種方法是為您提供完全刷新的條,並且可以僅通過geom_bar()來完成,但需要一些數據處理和軸文本標簽處理。 您必須在這里手工制作什么方面的標簽可免費獲得您(如果需要輸入/輸出標簽)。

如果需要將圖例分開(以及進行一些其他調整以使條形平齊),那么最直接的方法是制作兩個單獨的圖,編輯grid.arrange()邊距並使用grid.arrange() 如果在圖標題中執行此方法,則還可以使輸入/輸出標簽對齊。

兩者都是“工作”。

如果您需要圖案填充,其他人將不得不幫助您。 那是可行的,但很乏味。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM