Why does stacked bar plot change when add facet in r ggplot2

jessi

I have data that are in several groups, and I want to display them in a faceted stacked bar chart. The data show responses to a survey question. When I look at them in the dataframe, they make sense, and when I plot them (without faceting), they make sense.

However the data appear to change when they are faceted. I have never had this problem before. I was able to re-create a change (not the exact same change) with some dummy data.

myDF <- data.frame(rep(c('aa','ab','ac'), each = 9),
               rep(c('x','y','z'),times = 9),
               rep(c("yes", "no", "maybe"), each=3, times=3),
               sample(50:200, 27, replace=FALSE))
colnames(myDF) <- c('place','program','response','number')
library(dplyr)
myDF2 <- myDF %>%
    group_by(place,program) %>%
    mutate(pct=(100*number)/sum(number))

The data in myDF are basically a count of responses to a question. The myDF2 only creates a percent of respondents with any particular response within each place and program.

library(ggplot2)
my.plot <-ggplot(myDF2, 
             aes(x=place, y=pct)) +
    geom_bar(aes(fill=myDF$response),stat="identity")

my.plot.facet <-ggplot(myDF2, 
                   aes(x=place, y=pct)) +
    geom_bar(aes(fill=myDF$response),stat="identity")+
    facet_wrap(~program)

각 "프로그램"과 "장소"내에서 각 "응답"에 대한 적절한 "pct"를 보여주는 플롯을보고 싶습니다. 그러나 my.plot.facet장소 당 하나의 "응답"만 표시합니다.

my.plot 및 my.plot.facet의 예

데이터는 그렇지 않습니다. 예를 들어 head(myDF2)는 'x'위치의 프로그램 'aa'에 yes와 no가 모두 있음을 보여줍니다.

> head(myDF2)
Source: local data frame [6 x 5]
Groups: place, program

  place program response number      pct
1    aa       x      yes     69 18.35106
2    aa       y      yes     95 25.81522
3    aa       z      yes    192 41.64859
4    aa       x       no    129 34.30851
5    aa       y       no    188 51.08696
6    aa       z       no    162 35.14100
제시

여기서 주문이 중요하다는 것이 밝혀졌습니다. myDF2는 더 이상 데이터 프레임이 아닙니다. dplyr 객체입니다. 이는 ggplot2가 실제로 어려움을 겪고 있음을 의미합니다.

데이터가 프로그램에 의해 패싯되어야하는 경우 '프로그램'을 먼저 호출해야합니다. group_by()

역 플롯 패싯을 살펴보면 이것이 사실입니다.

my.plot.facet2 <-ggplot(myDF2, 
                       aes(x=program, y=pct)) +
   geom_bar(aes(fill=myDF2$response),stat="identity")+
   facet_wrap(~place)

생성 :

my.plot.facet2

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

R에서 ggplot2의 ggforce facet_zoom 오류

분류에서Dev

How to change section color of a stacked bar chart in Google Charts API?

분류에서Dev

Add multiple titles/text above brackets in facet_wrap ggplot in R

분류에서Dev

Change facet labels for a ggplot created by ggfortify::autoplot

분류에서Dev

R // ggplot2 : facet_wrap 및 for 루프 결합시 동적 제목

분류에서Dev

다른 facet_grid 플롯, ggplot2 R에서 동일한 패널 높이

분류에서Dev

연속 형 및 범주 형 변수에 ggplot2 및 facet_grid 사용 (R)

분류에서Dev

facet_grid를 재정렬하고 ggplot2의 geom_bar를 인수합니다.

분류에서Dev

R ggplot How to plot a bar chart with different colours inside depicting 3 differents columns from the dataframe?

분류에서Dev

Why i'm getting "titlebarViewController not supported for this window style" exception when trying to add title bar accessory view

분류에서Dev

interaction plot using ggplot2

분류에서Dev

R ggplot2 heatmap, force discrete scale with custom range, add grid to map

분류에서Dev

ggplot2의 facet_grid 오류

분류에서Dev

R ggplot2 및 ggforce에서 facet_wrap_paginate를 사용하는 x 축 레이블 누락

분류에서Dev

R ggplot2 facet_grid와 유사한 결과이지만 독립적 인 열

분류에서Dev

R ggplot2 : 막대 내부의 레이블, 쌓인 geom_bar 없음

분류에서Dev

df.groupby ( 'MONTH') [ 'TASKTYPE']. value_counts (). unstack (). loc [month_order] .plot.bar (stacked = True)-KeyError

분류에서Dev

R ggplot : Can't change y-axis scale range with facetted plot

분류에서Dev

add the same density curve to a plot in R

분류에서Dev

Why does all Word.Range object change when changing one of them?

분류에서Dev

Why does Facebook change the "FB.ui - picture" when the share is resharing on facebook itself?

분류에서Dev

Flow duration curve using facet_wrap of ggplot in R?

분류에서Dev

R : facet_grid (ggplot)의 t.test

분류에서Dev

Why does the time zone attribute get dropped when I add a new series to a zoo object

분류에서Dev

Why does Entity make a duplicate of related item when trying to add readonly object?

분류에서Dev

plot two columns as a bar graph and third column as line graph ggplot

분류에서Dev

Plot ggplot line graph having 2 seperate columns value in R

분류에서Dev

Why does NVL2 returns NULL when it's not expected?

분류에서Dev

R / ggplot2를 사용하여 geom_bar () 내부의 개별 %에 레이블 추가

Related 관련 기사

  1. 1

    R에서 ggplot2의 ggforce facet_zoom 오류

  2. 2

    How to change section color of a stacked bar chart in Google Charts API?

  3. 3

    Add multiple titles/text above brackets in facet_wrap ggplot in R

  4. 4

    Change facet labels for a ggplot created by ggfortify::autoplot

  5. 5

    R // ggplot2 : facet_wrap 및 for 루프 결합시 동적 제목

  6. 6

    다른 facet_grid 플롯, ggplot2 R에서 동일한 패널 높이

  7. 7

    연속 형 및 범주 형 변수에 ggplot2 및 facet_grid 사용 (R)

  8. 8

    facet_grid를 재정렬하고 ggplot2의 geom_bar를 인수합니다.

  9. 9

    R ggplot How to plot a bar chart with different colours inside depicting 3 differents columns from the dataframe?

  10. 10

    Why i'm getting "titlebarViewController not supported for this window style" exception when trying to add title bar accessory view

  11. 11

    interaction plot using ggplot2

  12. 12

    R ggplot2 heatmap, force discrete scale with custom range, add grid to map

  13. 13

    ggplot2의 facet_grid 오류

  14. 14

    R ggplot2 및 ggforce에서 facet_wrap_paginate를 사용하는 x 축 레이블 누락

  15. 15

    R ggplot2 facet_grid와 유사한 결과이지만 독립적 인 열

  16. 16

    R ggplot2 : 막대 내부의 레이블, 쌓인 geom_bar 없음

  17. 17

    df.groupby ( 'MONTH') [ 'TASKTYPE']. value_counts (). unstack (). loc [month_order] .plot.bar (stacked = True)-KeyError

  18. 18

    R ggplot : Can't change y-axis scale range with facetted plot

  19. 19

    add the same density curve to a plot in R

  20. 20

    Why does all Word.Range object change when changing one of them?

  21. 21

    Why does Facebook change the "FB.ui - picture" when the share is resharing on facebook itself?

  22. 22

    Flow duration curve using facet_wrap of ggplot in R?

  23. 23

    R : facet_grid (ggplot)의 t.test

  24. 24

    Why does the time zone attribute get dropped when I add a new series to a zoo object

  25. 25

    Why does Entity make a duplicate of related item when trying to add readonly object?

  26. 26

    plot two columns as a bar graph and third column as line graph ggplot

  27. 27

    Plot ggplot line graph having 2 seperate columns value in R

  28. 28

    Why does NVL2 returns NULL when it's not expected?

  29. 29

    R / ggplot2를 사용하여 geom_bar () 내부의 개별 %에 레이블 추가

뜨겁다태그

보관