2017-09-21 3 views
2

나는 R에서 피라미드 모양의 "플롯"을 만들려고 노력하고 있으며 가까이 있다고 생각합니다. 플롯 릭스의 pyramid.plot과 같은 기능이 있다는 것을 알고 있지만, 내가하고 싶은 것은 진짜 피라미드 플롯이 아닙니다. 피라미드 플롯에는 왼쪽과 오른쪽에 막대가있는 가운데 아래쪽에 텍스트 레이블이 있습니다. 그 대신에, 내가하고 싶은 것은 바가 그들로부터 떨어져 나오는 텍스트의 두 칼럼이 있다는 것입니다.반대 방향으로 수평 막대 그림 만들기

저는 ggplot을 사용하고 있습니다. (필자는 그럴 필요가 없습니다) 그리고 multiplot 기능을 사용하고 있습니다.

mtcars$`car name` <- rownames(mtcars) 
obj_a <- ggplot (mtcars, aes (x=`car name`, y=mpg)) 
obj_a <- obj_a + geom_bar (position = position_dodge(), stat="identity") 
obj_a <- obj_a + coord_flip() 
obj_a <- obj_a + xlab ("") 

USArrests$`states` <- rownames(USArrests) 
obj_b <- ggplot (USArrests, aes (x=`states`, y=UrbanPop)) 
obj_b <- obj_b + geom_bar (position = position_dodge(), stat="identity") 
obj_b <- obj_b + coord_flip() 
obj_b <- obj_b + xlab ("") 

multiplot (obj_a, obj_b, cols=2) 

다음과 같습니다 : 최소한의 예는 다음과 같이 될 것이다

Example graph

내가 원하는 것을 각 행을 가질 수 있도록 단지 (왼쪽 절반을 뒤집을 것 같다 왼쪽에서 오른쪽으로) : 왼쪽 막대, 자동차 모델, 주 이름, 오른쪽 막대. (그래프는 양쪽 모두에서 같은 수의 행을 가지므로 너무 비좁지는 않습니다.) 그러나 요점은 텍스트의 두 열이 아니라 하나의 열이 있다는 것입니다.

물론이 두 반쪽은 서로 독립적이므로 내 진짜 문제는 왼쪽 절반을 만드는 방법을 모르겠다는 것입니다. (막대가 반대 방향으로가는 막대 그림) 그러나 나는 또한 내가하려고하는 것을 설명 할 줄 알았는데 ...

미리 감사드립니다!

답변

3

당신이 차 이름이 반대편에 축 obj_a 부정, & 위치에 mpg의 값을 설정할 수 있습니다

ggplot (mtcars, aes (x=`car name`, y=-mpg)) +   # y takes on negative values 
    geom_bar (position = position_dodge(), stat = "identity") + 
    coord_flip() + 
    scale_x_discrete(name = "", position = "top") +  # x axis (before coord_flip) on opposite side 
    scale_y_continuous(name = "mpg", 
        breaks = seq(0, -30, by = -10), # y axis values (before coord_flip) 
        labels = seq(0, 30, by = 10)) # show non-negative values 

obj_a

+0

가 감사를! 나는 당신이 ggplots로 그 일을 할 수 있다는 것을 몰랐다. 즉, 음수 값을 사용하여 막대를 뒤집는다. 나는 coord_flip()과 같은 것을 생각하고 있었지만 한 번 (90도) 뒤집었다. 나는 다시 뒤집을 수 없다. ... – Ray

1

그 pyramid.plot 이미 당신이 필요하지 보인다. 자신의 예를 사용 :

xy.pop<-c(3.2,3.5,3.6,3.6,3.5,3.5,3.9,3.7,3.9,3.5,3.2,2.8,2.2,1.8, 
      1.5,1.3,0.7,0.4) 
xx.pop<-c(3.2,3.4,3.5,3.5,3.5,3.7,4,3.8,3.9,3.6,3.2,2.5,2,1.7,1.5, 
      1.3,1,0.8) 
agelabels<-c("0-4","5-9","10-14","15-19","20-24","25-29","30-34", 
      "35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74", 
      "75-79","80-44","85+") 
mcol<-color.gradient(c(0,0,0.5,1),c(0,0,0.5,1),c(1,1,0.5,1),18) 
fcol<-color.gradient(c(1,1,0.5,1),c(0.5,0.5,0.5,1),c(0.5,0.5,0.5,1),18) 
par(mar=pyramid.plot(xy.pop,xx.pop,labels=agelabels, 
        main="Australian population pyramid 2002",lxcol=mcol,rxcol=fcol, 
        gap=0.5,show.values=TRUE)) 
# three column matrices 
avtemp<-c(seq(11,2,by=-1),rep(2:6,each=2),seq(11,2,by=-1)) 
malecook<-matrix(avtemp+sample(-2:2,30,TRUE),ncol=3) 
femalecook<-matrix(avtemp+sample(-2:2,30,TRUE),ncol=3) 



# *** Make agegrps a two column data frame with the labels *** 

# group by age 
agegrps<-data.frame(c("0","11","21","31","41","51", 
      "61-70","71-80","81-90","91+"), 
      c("10","20","30","40","50","60", 
      "70","80","90","91")) 


oldmar<-pyramid.plot(malecook,femalecook,labels=agegrps, 
        unit="Bowls per month",lxcol=c("#ff0000","#eeee88","#0000ff"), 
        rxcol=c("#ff0000","#eeee88","#0000ff"),laxlab=c(0,10,20,30), 
        raxlab=c(0,10,20,30),top.labels=c("Males","Age","Females"),gap=4, 
        do.first="plot_bg(\"#eedd55\")") 
# put a box around it 
box() 
# give it a title 
mtext("Porridge temperature by age and sex of bear",3,2,cex=1.5) 
# stick in a legend 
legend(par("usr")[1],11,c("Too hot","Just right","Too cold"), 
     fill=c("#ff0000","#eeee88","#0000ff")) 
# don't forget to restore the margins and background 
par(mar=oldmar,bg="transparent") 

결과 : enter image description here

+0

당신의 솔루션과 Z Lin이 모두 좋다; 나는 하나만 받아 들일 수있어서 유감이다. 그러나 귀하의 답변이 다른 사람들을 도울 수 있고 그들이 선택할 수 있다고 확신합니다. 필자는 pyramid.plot 예제를 살펴보고 두 개의 텍스트 열을 만드는 방법을 보지 못했습니다. 그것을 제안 해 주셔서 감사합니다! – Ray

+0

문제는 없지만 필자는 피라미드 음모를 발견하게 된 것을 기쁘게 생각합니다. –