ggplot2를 사용하여 막대 글 머리 기호가있는 막대 그래프를 만듭니다.

Jérémz

ggplot2를 사용하여 데이터 집합 (Y의 $ proteinN 및 X의 $ 메서드)에서 SDM을 사용하여 막대 그래프를 만들고 범례의 다른 데이터 집합 ($ 특정)에 표시기가있는 동일한 막대 그래프에 포함하고 싶습니다 (겹침) 글 머리 기호 막대 차트 모양으로. 이와 약간 비슷합니다 (그러나 첫 번째 데이터 세트의 경우 세로 막대와 SDM)


(출처 : yaksis.com )

내 코드와 데이터는 다음과 같습니다.

    library(ggplot2) 
    data <- textConnection("proteinN, supp, method, specific
    293, protnumb, insol, 46
    259, protnumb, insol, 46
    274, protnumb, insol, 46
    359, protnumb, fasp, 49
    373, protnumb, fasp, 49
    388, protnumb, fasp, 49
    373, protnumb, efasp, 62
    384, protnumb, efasp, 62
    382, protnumb, efasp, 62
    ")

    data <- read.csv(data, h=T)

# create functions to get the lower and upper bounds of the error bars
stderr <- function(x){sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))}
lowsd <- function(x){return(mean(x)-stderr(x))}
highsd <- function(x){return(mean(x)+stderr(x))}

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", 
               "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

# create a ggplot
ggplot(data=data,aes(x=method, y=proteinN, fill=method))+
  #Change _hue by _manualand remove c=45, l=80 if not desire#
  scale_fill_manual(values=cbPalette)+
  scale_fill_hue(c=45, l=80)+

  # first layer is barplot with means
  stat_summary(fun.y=mean, geom="bar", position="dodge", colour='black')+
  # second layer overlays the error bars using the functions defined above
  stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, 
              geom="errorbar", position="dodge",color = 'black', size=.5)

몇 가지 시도했지만 아무것도 작동하지 않으며 두 번째 데이터 세트를 추가하려고 할 때 항상 다음 오류 출력이 나타납니다.

Error : Mapping a variable to y and also using stat="bin". With stat="bin", it will attempt to set the y value to the count of cases in each group. This can result in unexpected behavior and will not be allowed in a future version of ggplot2. If you want y to represent counts of cases, use stat="bin" and don't map a variable to y. If you want y to represent values in the data, use stat="identity". See ?geom_bar for examples. (Defunct; last used in version 0.9.2)

Error : Mapping a variable to y and also using stat="bin". With stat="bin", it will attempt to set the y value to the count of cases in each group. This can result in unexpected behavior and will not be allowed in a future version of ggplot2. If you want y to represent counts of cases, use stat="bin" and don't map a variable to y. If you want y to represent values in the data, use stat="identity". See ?geom_bar for examples. (Defunct; last used in version 0.9.2)

Here is my try:

# create functions to get the lower and upper bounds of the error bars
stderr <- function(x){sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))}
lowsd <- function(x){return(mean(x)-stderr(x))}
highsd <- function(x){return(mean(x)+stderr(x))}

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", 
               "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# create a ggplot
ggplot(data=data,aes(x=method, y=proteinN, fill=method, witdh=1))+
  #Change _hue by _manualand remove c=45, l=80 if not desire#
  scale_fill_manual(values=cbPalette)+
  scale_fill_hue(c=45, l=80)+

  #Second set of data#
  geom_bar(aes(x=method, y=specific, fill="light green"), width=.4) +

  # first layer is barplot with means
  stat_summary(fun.y=mean, geom="bar", position="dodge", colour='black')+

  # second layer overlays the error bars using the functions defined above
  stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, 
      geom="errorbar", position="dodge",color = 'black', size=.5)
joran

Maybe try something like this?

ggplot(data=data,aes(x=method, y=proteinN, fill=method, width=1))+
  scale_fill_hue(c=45, l=80) +
  stat_summary(fun.y=mean, geom="bar", position="dodge", colour='black')+
  stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, 
               geom="errorbar", position="dodge",color = 'black', size=.5) + 
  geom_bar(data = unique(data[,c('method','specific')]),
           aes(x = method,y = specific),
           stat = "identity",
           fill = "light green",
           width = 0.5)

A couple of notes.

You misspelled "width".

Your two scale_fill lines are pointless. ggplot will only take one fill scale, whichever one appears last. You can't "modify" the fill scale like that. You ought to have received a warning about it that explicitly said:

Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.

The error message you got said:

Mapping a variable to y and also using stat="bin"

i.e. you specified y = proteinN while also using stat = "bin" in geom_bar (the default). It went on to explain:

With stat="bin", it will attempt to set the y value to the count of cases in each group.

i.e. rather than plot the values in y, it will try to count the number of instances of, say, insol, and plot that. (Three, in this case.) A cursory examination of the examples in ?geom_bar immediately reveals that most of the examples only specify an x variable. Until you get to this example in the help:

# When the data contains y values in a column, use stat="identity"
library(plyr)
# Calculate the mean mpg for each level of cyl
mm <- ddply(mtcars, "cyl", summarise, mmpg = mean(mpg))
ggplot(mm, aes(x = factor(cyl), y = mmpg)) + geom_bar(stat = "identity")

where it demonstrates that when you specify the precise y values you want, you have to also say stat = "identity". Conveniently, the error message also said this:

If you want y to represent values in the data, use stat="identity".

The final piece was knowing that since the overlaid bars only have one value per x value, we should really collapse that piece down to the minimum information needed via:

unique(data[,c('method','specific')]

or just split it off into it's own data frame ahead of time.

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

ggplot2를 사용하여 막대 글 머리 기호가있는 막대 그래프를 만듭니다.

분류에서Dev

ggplot2를 사용하여 막대 차트 만들기

분류에서Dev

ggplot2를 사용하여 보조 축에 역 막대 그래프로 여러 시계열을 그리는 방법은 무엇입니까?

분류에서Dev

ggplot2를 사용하여 그룹화 된 막대 차트에 대한 오차 막대를 그리는 방법은 무엇입니까?

분류에서Dev

ggplot2를 사용한 다중 막대 그래프

분류에서Dev

ggplot2를 사용하여 누적 막대 그림에서 상위 3 개 항목 만 표시하고 나머지는 "기타"로 표시합니다.

분류에서Dev

ggplot을 사용하여 막대 그래프를 그리는 방법

분류에서Dev

세로 막대 그래프 용 시리즈를 만드는 Highcharts

분류에서Dev

ggplot2를 사용하는 R의 누적 막대 그래프

분류에서Dev

ggplot을 사용하여 레이블이있는 다른 색상의 막대 그래프 그리기

분류에서Dev

ggplot2 패키지를 사용하여 두 데이터 세트를 비교하기 위해 막대 차트를 그리시겠습니까?

분류에서Dev

geom_density는 막대 모양의 그래프를 만듭니다.

분류에서Dev

ggplot2를 사용하여 R에서 범례 제목과 키 순서 및 색상을 다중 스택 막대 그래프로 변경하는 방법

분류에서Dev

ggplot2에 오류 막대가있는 막대 그래프

분류에서Dev

R ggplot2를 사용하여 숫자가 아닌 데이터에서 누적 막대 차트 만들기

분류에서Dev

막대 글 머리 기호 차트를 기둥 차트로 변환

분류에서Dev

ggplot2를 사용하여 누적 막대 그림에 대한 데이터 프레임 순서 관리

분류에서Dev

MatPlotLib를 사용하여 차트에 여러 막대 그리기

분류에서Dev

빈 데이터 시리즈를 보여주는 다중 시리즈가있는 jqPlot 막대 그래프

분류에서Dev

막대 그래프로 최소 / 최대 막대를 그리는 방법

분류에서Dev

Pandas 데이터 프레임에서 백분율 분포를 사용하여 수평 막대 그래프를 그리는 방법은 무엇입니까?

분류에서Dev

여러 열을 그룹화하고 막대 그래프를 그리는 방법은 무엇입니까?

분류에서Dev

사전을 사용하여 Seaborn 백분율 막대 그래프를 어떻게 그릴 수 있습니까?

분류에서Dev

Bokeh를 사용하여 막대 차트 그리기

분류에서Dev

flutter의 막대만으로 막대 그래프를 만들 수 있습니까?

분류에서Dev

GGplot2에서 막대에 백분율 기호를 추가하는 방법

분류에서Dev

matplot lib를 사용하여 막대의 3D 막대 그래프 너비를 조정하는 방법

분류에서Dev

다음 막대 그래프를 그리는 방법은 무엇입니까?

분류에서Dev

R Shiny-dateRangeInput 및 selectInput 함수를 사용하여 반응 형 막대 그래프를 만드는 방법

Related 관련 기사

  1. 1

    ggplot2를 사용하여 막대 글 머리 기호가있는 막대 그래프를 만듭니다.

  2. 2

    ggplot2를 사용하여 막대 차트 만들기

  3. 3

    ggplot2를 사용하여 보조 축에 역 막대 그래프로 여러 시계열을 그리는 방법은 무엇입니까?

  4. 4

    ggplot2를 사용하여 그룹화 된 막대 차트에 대한 오차 막대를 그리는 방법은 무엇입니까?

  5. 5

    ggplot2를 사용한 다중 막대 그래프

  6. 6

    ggplot2를 사용하여 누적 막대 그림에서 상위 3 개 항목 만 표시하고 나머지는 "기타"로 표시합니다.

  7. 7

    ggplot을 사용하여 막대 그래프를 그리는 방법

  8. 8

    세로 막대 그래프 용 시리즈를 만드는 Highcharts

  9. 9

    ggplot2를 사용하는 R의 누적 막대 그래프

  10. 10

    ggplot을 사용하여 레이블이있는 다른 색상의 막대 그래프 그리기

  11. 11

    ggplot2 패키지를 사용하여 두 데이터 세트를 비교하기 위해 막대 차트를 그리시겠습니까?

  12. 12

    geom_density는 막대 모양의 그래프를 만듭니다.

  13. 13

    ggplot2를 사용하여 R에서 범례 제목과 키 순서 및 색상을 다중 스택 막대 그래프로 변경하는 방법

  14. 14

    ggplot2에 오류 막대가있는 막대 그래프

  15. 15

    R ggplot2를 사용하여 숫자가 아닌 데이터에서 누적 막대 차트 만들기

  16. 16

    막대 글 머리 기호 차트를 기둥 차트로 변환

  17. 17

    ggplot2를 사용하여 누적 막대 그림에 대한 데이터 프레임 순서 관리

  18. 18

    MatPlotLib를 사용하여 차트에 여러 막대 그리기

  19. 19

    빈 데이터 시리즈를 보여주는 다중 시리즈가있는 jqPlot 막대 그래프

  20. 20

    막대 그래프로 최소 / 최대 막대를 그리는 방법

  21. 21

    Pandas 데이터 프레임에서 백분율 분포를 사용하여 수평 막대 그래프를 그리는 방법은 무엇입니까?

  22. 22

    여러 열을 그룹화하고 막대 그래프를 그리는 방법은 무엇입니까?

  23. 23

    사전을 사용하여 Seaborn 백분율 막대 그래프를 어떻게 그릴 수 있습니까?

  24. 24

    Bokeh를 사용하여 막대 차트 그리기

  25. 25

    flutter의 막대만으로 막대 그래프를 만들 수 있습니까?

  26. 26

    GGplot2에서 막대에 백분율 기호를 추가하는 방법

  27. 27

    matplot lib를 사용하여 막대의 3D 막대 그래프 너비를 조정하는 방법

  28. 28

    다음 막대 그래프를 그리는 방법은 무엇입니까?

  29. 29

    R Shiny-dateRangeInput 및 selectInput 함수를 사용하여 반응 형 막대 그래프를 만드는 방법

뜨겁다태그

보관