2016-09-21 5 views
0

그래서 @Axeman의 도움으로 누적 막대 그래프를 만들었습니다. 지금까지는 너무 좋아. 이제 나는 Blue 음모를 Red 음모로 바꾸고 싶다. 나는 문자 그대로 ggplot에서 order 함수를 사용하여 모든 것을 시도해 보았고 올바르게 할 수 없다. 다음은Rstudio reorder stacked ggplot geom_bar

Stacked barplot

, 당신은 (첫 번째 행 머리글을 containts)이 음모에 사용 된 dataframe의 추출물을 찾을 수 있습니다 여기에 플롯이다. 기본적으로 저축 (반복적 MDF) 및 마지막 열 두 가지 유형의 다음의 분할 이름 모두 (NewRepetitive + Anrechenbare_MDF)의 합을 여기

"DivisionName", "NewRepetitive", "Anrechenbare_MDF", "Total_CR" 
    "Electrification Products" 0 0 0 
    "Electrification Products" 39.762 0 39.762 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 1641.348852 0 1641.348852 
    "Electrification Products" 6367.725252 0 6367.725252 
    "Electrification Products" 24299.24741 0 24299.24741 
    "Electrification Products" 32910.87486 0 32910.87486 
    "Electrification Products" 8637.313704 0 8637.313704 
    "Electrification Products" 9220.489704 0 9220.489704 
    "Electrification Products" -1038.609948 0 -1038.609948 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 6346.518852 0 6346.518852 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 4675.189452 0 4675.189452 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 0 0 0 
    "Electrification Products" 8495.893524 0 8495.893524 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 2439.849336 0 2439.849336 
    "Power Grids" 0 0 0 
    "Power Grids" -3909.837222 0 -3909.837222 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" -974.473842 0 -974.473842 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" -448.740678 0 -448.740678 
    "Power Grids" -886.997442 0 -886.997442 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 263.767854 0 263.767854 
    "Power Grids" -28.4961 0 -28.4961 
    "Power Grids" 122.00307 0 122.00307 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 3910.778256 0 3910.778256 
    "Power Grids" 0 0 0 
    "Power Grids" -1246.87005 0 -1246.87005 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" -61.047924 0 -61.047924 
    "Power Grids" -61.047924 0 -61.047924 
    "Power Grids" 0 0 0 
    "Power Grids" -6.348666 0 -6.348666 
    "Power Grids" -12.697332 0 -12.697332 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" -240.798672 0 -240.798672 
    "Power Grids" 0 0 0 
    "Power Grids" -65.395236 0 -65.395236 
    "Power Grids" 24.678948 0 24.678948 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 0 0 0 
    "Power Grids" 215.669088 0 215.669088 
    "Power Grids" -61.047924 0 -61.047924 
    "Power Grids" 14.605908 0 14.605908 
    "Power Grids" -42.61161 0 -42.61161 
    "Power Grids" -553.540056 0 -553.540056 
    "Power Grids" 0 0 0 
    "Power Grids" 113.507256 0 113.507256 
    "Power Grids" -21.365448 0 -21.365448 
    "Power Grids" 0 0 0 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 603.25581 0 603.25581 
    "Power Grids" -61.047924 0 -61.047924 
    "Power Grids" 0 0 0 
    "Power Grids" -224.429982 0 -224.429982 
    "Power Grids" 0 0 0 
    "Power Grids" 44.904552 0 44.904552 
    "Power Grids" -240.798672 0 -240.798672 
    "Power Grids" 0 0 0 
    "Power Grids" -361.198008 0 -361.198008 
    "Power Grids" 0 0 0 
    "Power Grids" -240.798672 0 -240.798672 
    "Power Grids" -115.349562 0 -115.349562 
    "Power Grids" 0 0 0 
    "Power Grids" 263.767854 0 263.767854 
    "Power Grids" -1948.93443 0 -1948.93443 
    "Power Grids" 16.36869 0 16.36869 
    "Power Grids" 111.280584 0 111.280584 
    "Power Grids" 924.254436 0 924.254436 
    "Power Grids" -1122.136656 0 -1122.136656 
    "Power Grids" 0 0 0 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 5.977554 0 5.977554 
    "Power Grids" 0 0 0 
    "Electrification Products" 5542.133592 -559.362777 4982.770815 
    "Electrification Products" 3038.638548 -260.690669 2777.947879 
    "Electrification Products" 2507.378466 -279.681388 2227.697078 
    "Electrification Products" -2241.940608 -334.661491 -2576.602099 
    "Electrification Products" -4535.744118 -931.275287 -5467.019405 
    "Electrification Products" 808.003602 -156.175362 651.82824 
    "Electrification Products" 958.873884 -209.163422 749.710462 
    "Electrification Products" -830.588418 -144.621569 -975.209987 
    "Electrification Products" -999.404616 -184.794228 -1184.198844 
    "Electrification Products" 342.112248 -225.099694 117.012554 
    "Electrification Products" -237.193584 -92.629517 -329.823101 
    "Electrification Products" 238.572 -236.653473 1.918527 
    "Electrification Products" 145.674714 -71.925657 73.749057 
    "Electrification Products" 0 -40.637464 -40.637464 
    "Electrification Products" -437.382 -111.553826 -548.935826 
    "Electrification Products" 1852.074198 -643.426551 1208.647647 
    "Electrification Products" 1318.680222 -290.106352 1028.57387 
    "Electrification Products" 395.89698 -81.009327 314.887653 
    "Electrification Products" 314.106546 -118.725138 195.381408 
    "Electrification Products" -711.580752 -277.88855 -989.469302 
    "Electrification Products" -897.22953 -196.215213 -1093.444743 
    "Electrification Products" 228.074832 -150.066454 78.008378 
    "Electrification Products" -657.517686 -188.844696 -846.362382 
    "Electrification Products" -986.269902 -283.267038 -1269.53694 
    "Electrification Products" -474.638994 -99.203216 -573.84221 
    "Electrification Products" -711.951864 -148.804831 -860.756695 
    "Electrification Products" 286.577988 -66.863233 219.714755 

수정을 사용하는 것을 제외하고 R-코드 포함 (tidyr 패키지를 사용하여) 올바른 방식으로 데이터 프레임을 만들었으므로 gplot에 특정 형식의 데이터가 필요하기 때문에 누적 막대 그래프를 만들 수 있습니다.
library(ggplot2) 
    library(tidyr) 

    pd <- gather(stack, 'key', 'value', -DivisionName) 
    pd <- pd[pd$key != 'Total_CR', ] 

그리고 마지막으로 ggplot 코드

:

ggplot(pd, aes(DivisionName, value, fill = key)) + 
    geom_bar(stat = "summary", fun.y = "sum") + 
    scale_fill_discrete(name="Saving-Typen",labels=c("MDF-Savings","Repetitive Savings")) + 
    ylab("Total Savings") + scale_y_continuous(breaks=c(seq(0,15000000,1000000)), expand = c(0,0)) 

barplot 내가 원하는 방법을 정확하게 보이는 그것에 순서를 제외하고 .. 내가 반복 - 저축의 위의 MDF 저축이 필요합니다. 나는 5 시간 동안 여기에 앉아 있고 올바른 해결책을 얻을 수 없다. 나는 "주문"옵션으로 모든 것을 시도했다.

나를 도와 줄 사람이 있습니까?

+1

R과 같이 데이터를 공유하려면'dput()'을 사용하십시오. 재사용이 쉽고 컬럼 클래스와 같은 메타 데이터가 포함되어 있습니다. –

답변

0

문제는 플롯 코드가 아니라 데이터 프레임 pd에 있습니다. 요소 대신 문자 벡터로 pd$key이 있습니다. 이를 올바른 순서로 팩터로 변환하면 ggplot이 나머지 작업을 대신 수행합니다.

pd <- gather(stack, 'key', 'value', -DivisionName) 
pd <- pd[pd$key != 'Total_CR', ] 

한 후 수행

pd$key <- factor(pd$key, 
       levels = c("NewRepetitive", 
          "Anrechenbare_MDF")) 

하는 것은 올바른 값과 일치하도록 다시 레이블을 변경하는 것을 잊지 마십시오.

ggplot(pd, aes(DivisionName, abs(value), fill = key)) + 
    geom_bar(stat = "summary", fun.y = "sum") + 
    scale_fill_discrete(name="Saving-Typen",labels=c("Repetitive Savings","MDF_Savings")) + 
    ylab("Total Savings") + scale_y_continuous(breaks=c(seq(0,15000000,1000000)), expand = c(0,0)) 

내가 인해 사기 예를 들어 데이터 세트에 abs(value)을 사용했다

enter image description here

참고를 제공합니다. 앞으로 질문을하는 방법에 대한 아이디어를 얻으려면 How to make a great R reproducible example?을보십시오.

+0

정말 고마워요! 너는 나의 하루를 구했다! – Pixelements

0

시도 수동으로 원하는 색상을 선택합니다 :

+ scale_fill_manual(values=c("#999999", "#E69F00"), 
         name="Legend title", 
         breaks=c("Electrical", "Power grids"), 
         labels=c("Electrical", "Power grids")) 

테스트하지

http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/에 발견.

+1

바를 교환하고 싶습니다. 뿐만 아니라 색깔 :/ – Pixelements

+0

그것은 명확하지 않았다. 또한 dput()을 사용하여 다른 사람들이 실제로 질문에 답변 할 수 있도록하십시오. 직접 주문을 지정할 수있는 주문한 요인을 사용하여 제안 할 것입니다. http : //www.dummies.com/programming/r/how-to-work-with-r- – MichaelVE

+0

예제에서 이것을 수행하는 방법이 확실하지 않습니다. DivisionName은 사실 첫 번째 데이터 프레임의 요소이기 때문에. – Pixelements