Re: [問題] 折線圖+長條圖呈現

看板R_Language作者 (阿罵)時間8年前 (2017/06/13 12:16), 8年前編輯推噓4(4010)
留言14則, 4人參與, 最新討論串6/9 (看更多)
#更新 感謝版主c大各種code詳解 及G大還有Andrew大的幫忙!已經有解了 更新方法一於置底 借用這個標題 跟原po有一樣的問題 試了幾個幫法 但是都不成功......... 不知道版上有沒有人能有方法解決兩種方法使其更優......感謝了! [程式範例] #data: 用c大分享的 code library(dplyr) ngrp <- 2L numSamples <- 200L DF <- data.frame(V1 = sample(1L:3L, numSamples, TRUE), V2 = sample(1L:2L, numSamples, TRUE), V3 = rnorm(numSamples), V4 = rnorm(numSamples), V5 = rnorm(numSamples)) #sum statistics sum0<-DF %>% group_by(V1) %>% summarise (mean=mean(V2),n=n()) #方法一: bar chart 不在節點上,看起來怪怪的 par(mar=c(4,4.5,4,4)) barplot(sum0$n,ylim=c(0,max(sum0$n)+0.1*max(sum0$n)), yaxt="n",width=0.2,space=2) axis(side=4) mtext("Count", side=4, line=1.8) par(new=T) plot(sum0$V1,sum0$mean, xlab="cartegpory level", ylim=c(0,2), ylab="") mtext("Group mean",side=2, line=1.8) lines(sum0$V1,sum0$mean, type="o") #方法二 使用ggpltot, 但是無法讓兩圖在同一圖上... library(ggplot2) #frequency plot g.botton<-ggplot()+geom_bar(data=sum0,aes(x=V1, y=n), stat="identity")+ labs(y="Counts")+ theme_classic() #line plot g.top<-ggplot()+geom_line(data=sum0, aes(x=V1, y=mean, group=1))+geom_point(data=sum0,aes(x=V1,y=mean, group=1))+ scale_y_continuous(limits = c(0,2),position="right")+ labs(y="Group mean")+ theme_classic() print(g.bottom) #vp ? print(g.top) #vp? ※ 引述《samex4x4 (Same)》之銘言: : [問題類型]: : : 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) : : [軟體熟悉度]: : 入門(寫過其他程式,只是對語法不熟悉) : [問題敘述]: : 想詢問是否有辦法同時畫出長條圖和摺線圖在一張圖表上? : 兩者是不同的數值(取不同欄位) : 大概是長這樣 : http://imgur.com/b5XELZF.jpg
: 謝謝QQ : [程式範例]: : : : [環境敘述]: : : 3.3.2 : : [關鍵字]: : : 選擇性,也許未來有用 : -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 72.195.237.4 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1497327383.A.763.html

06/13 13:12, , 1F
方法二可以試試看gridExtra這個package
06/13 13:12, 1F

06/13 13:13, , 2F
grid.arrange(g.bottom, g.top, ncol = 1)
06/13 13:13, 2F
感謝G大分享此方法 試過之後 貌似grid.arrange只會將 圖合併成上下圖...

06/14 01:13, , 3F
barplot()的回傳值會直接告訴你每個柱的x軸位置
06/14 01:13, 3F

06/14 01:13, , 4F
你看看 y <- barplot(...) 之後 y 是什麼?
06/14 01:13, 4F

06/14 01:13, , 5F
就可以解決沒有對齊的問題了。
06/14 01:13, 5F
感謝andrew大提醒! 更新修改方法於下: #更新:加入xlim可解 par(mar=c(4,4.5,4,4)) Y<-barplot(sum0$n,sum0$V1,ylim=c(0,max(sum0$n)+0.1*max(sum0$n)), yaxt="n",width=0.2,space=2,xlim=c(min(Y)-0.1*max(Y),max(Y)+0.1*max(Y))) axis(side=4) mtext("Count", side=4, line=1.8) par(new=T) plot(Y,sum0$mean, xlab="cartegpory level", ylim=c(0,2), ylab="",xlim=c(min(Y)-0.1*max(Y),max(Y)+0.1*max(Y)),xaxt="n") mtext("Group mean",side=2, line=1.8) lines(Y,sum0$mean, type="o",ylab="",xlim=c(min(Y)-0.1*max(Y),max(Y)+0.1*max(Y)),xaxt="n") 圖範例: http://imgur.com/XTNEEVL
再次感謝大家的時間與幫忙! 謝謝! ※ 編輯: sacidoO (72.195.237.4), 06/14/2017 12:58:07

06/14 14:13, , 6F
哈 搞懂你的問題了 原來不是要把兩個圖擺在一起
06/14 14:13, 6F

06/14 14:16, , 7F
hadley在10年時在stackoverflow說ggplot2沒辦法做這種圖
06/14 14:16, 7F

06/14 19:13, , 8F
怎麼可能ggplot2不行,跟lattice一樣是grid-based,
06/14 19:13, 8F

06/14 19:13, , 9F
只是沒人做而已
06/14 19:13, 9F

06/14 20:01, , 10F
ggplot2已經可以這樣做了,我更新在我回覆的文章中
06/14 20:01, 10F

06/14 20:02, , 11F
應該是說當時hadley說這種圖不符合ggplot2的設計哲學
06/14 20:02, 11F

06/14 20:02, , 12F
所以才沒有特別支援 https://goo.gl/JqLsi2
06/14 20:02, 12F

06/14 20:05, , 13F
此一時彼一時阿(攤手
06/14 20:05, 13F

06/14 20:07, , 14F
不過之前就有人hack出來了 https://goo.gl/0JHbq9
06/14 20:07, 14F
文章代碼(AID): #1PFsSNTZ (R_Language)
討論串 (同標題文章)
文章代碼(AID): #1PFsSNTZ (R_Language)