2014-11-07 2 views
0

ggplot을 사용하여 6 개의 막대 그래프를 플롯하려고합니다. 각 막대 그래프는 하나의 제품 (예 : Modis 2000, Modis 2005 등)에 해당합니다. x 축은 토지 이용 (농업, 건설 등)이고 y 축은 커미션 오류 (_CE)와 누락 오류 (_OE)를 모두 포함하는 백분율 오류입니다. 이 두 오류의 막대는 각각의 토지 이용 (줄거리 첨부 참조에 인접한다. 여러 변수가있는 하나의 창에서 ggplot을 사용하여 여러 막대 그래프를 그립니다.

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)) 

내가 달성하고자하는 것입니다하지만,이 히스토그램은 하나 개의 제품을 위해 만들어졌다. 나는 같은 종류를 만들려면 각 제품과 모든 히스토그램에 대한 히스토그램은 팝업해야 하나 개의 창에서 누군가가 그렇게 나를 도와 드릴까요 감사를 도와
enter image description here

답변

0
당신은 당신이 설명한 것을 달성하기 위해 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)에 넣고 을 사용하여 Long Format 데이터로 변환하여 ggplotfacet_wrap과 함께 사용합니다.

enter image description here

편집 : 다음과 같습니다

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) 

을 : : 또는 당신이 facet_wrapnrow=1을 추가하여 단일 행에있는 모든 그래프를 가질 수

다음은 위의 코드의 출력은

enter image description here

이 다음과 같은 이미지를 사용하여이 이미지를 내 보냈습니다. x 축 라벨이 겹치지 않도록하려면 : 2000 * 1500.

다른 내보내기 크기와 글꼴 크기로 재생하면 원하는 것을 찾을 수 있습니다.

관련 문제