[英]Plot several histograms with ggplot in one window with several variables
我想使用ggplot繪制6個直方圖。 每個直方圖對應一個產品(例如Modis 2000,Modis 2005等)。 x軸為土地利用(農業,建築業等),y軸為百分比誤差,其中包括委托誤差(_CE)和遺漏誤差(_OE)。 對於每種土地用途,這兩個誤差的小節應該相鄰(請參見附表)。
structure(list(X = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L), .Label = c("Forest_CE", "Forest_OE"), class = "factor"),
Product = structure(c(5L, 5L, 6L, 6L, 3L, 3L, 4L, 4L, 1L,
1L, 2L, 2L), .Label = c("CCI 2000", "CCI 2005", "GLC-SHARE2000",
"GLC-SHARE2005", "Modis 2000", "Modis 2005"), class = "factor"),
Agriculture = c(45.42827657, 36.98156682, 48.19181349, 55.41838134,
41.6579589, 29.74910394, 42.88911495, 7.112253642, 38.86168911,
86.76103247, 44.08410549, 88.54166667), Built.up = c(0.990712074,
0.115207373, 0.702079746, 0.137174211, 0.104493208, 0, 0.996948118,
0, 1.591187271, 0, 1.069137562, 0), Mining = c(0.557275542,
0, 0.132467877, 0, 0.870776733, 0, 0.22380468, 0, 1.407588739,
0, 0.249465431, 0), Other = c(52.73477812, 51.38248848, 50.73519671,
44.17009602, 56.94879833, 70.25089606, 55.50356053, 77.97772065,
57.71113831, 11.07410491, 54.16963649, 7.899305556), Water = c(0.288957688,
11.52073733, 0.238442178, 0.274348422, 0.417972832, 0, 0.386571719,
14.91002571, 0.428396573, 2.164862614, 0.427655025, 3.559027778
)), .Names = c("X", "Product", "Agriculture", "Built.up",
"Mining", "Other", "Water"), class = "data.frame", row.names = c(NA,
-12L))
這是我想要實現的,但是此直方圖是針對一種產品制作的。 我想為每種產品創建相同類型的直方圖,所有直方圖應在一個窗口中彈出。 有人可以幫我嗎? 謝謝你的幫助。
您可以使用facet_wrap
實現您所描述的內容:
yellowmellow <- structure(list(X = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L), .Label = c("Forest_CE", "Forest_OE"), class = "factor"),
Product = structure(c(5L, 5L, 6L, 6L, 3L, 3L, 4L, 4L, 1L,
1L, 2L, 2L), .Label = c("CCI 2000", "CCI 2005", "GLC-SHARE2000",
"GLC-SHARE2005", "Modis 2000", "Modis 2005"), class = "factor"),
Agriculture = c(45.42827657, 36.98156682, 48.19181349, 55.41838134,
41.6579589, 29.74910394, 42.88911495, 7.112253642, 38.86168911,
86.76103247, 44.08410549, 88.54166667), Built.up = c(0.990712074,
0.115207373, 0.702079746, 0.137174211, 0.104493208, 0, 0.996948118,
0, 1.591187271, 0, 1.069137562, 0), Mining = c(0.557275542,
0, 0.132467877, 0, 0.870776733, 0, 0.22380468, 0, 1.407588739,
0, 0.249465431, 0), Other = c(52.73477812, 51.38248848, 50.73519671,
44.17009602, 56.94879833, 70.25089606, 55.50356053, 77.97772065,
57.71113831, 11.07410491, 54.16963649, 7.899305556), Water = c(0.288957688,
11.52073733, 0.238442178, 0.274348422, 0.417972832, 0, 0.386571719,
14.91002571, 0.428396573, 2.164862614, 0.427655025, 3.559027778
)), .Names = c("X", "Product", "Agriculture", "Built.up",
"Mining", "Other", "Water"), class = "data.frame", row.names = c(NA,
-12L))
# using reshape 2, we change the data frame to long format
mean.long = melt(yellowmellow, measure.vars = 3:7, variable.name = "Land", value.name = "Percentage")
ggplot(mean.long, aes(x=Land, y=Percentage, fill=factor(X))) + theme_bw() + facet_wrap(~Product)+
geom_bar(position=position_dodge(.9)) + # this line is not needed. Only included as a fix for legend/key random line
geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = FALSE) +
scale_fill_grey(start=.4)
我猜這就是您要尋找的。 我基本上將您的數據放入一個數據框(yellowmellow)中,並使用melt
將其轉換為Long Format數據,以便與ggplot
和facet_wrap
一起使用。 這是上面代碼的輸出:
編輯:另外,您可以通過增加對單個行的所有圖表nrow=1
至facet_wrap
:
ggplot(mean.long, aes(x=Land, y=Percentage, fill=factor(X))) + theme_bw() + facet_wrap(~Product, nrow=1)+
geom_bar(position=position_dodge(.9)) + # this line is not needed. Only included as a fix for legend/key random line
geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = FALSE) +
scale_fill_grey(start=.4)
看起來像這樣:
我使用以下尺寸導出此圖像,以避免x軸標簽重疊:2000 * 1500。
您可以使用不同的導出大小和字體大小來查找所需內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.