R_Language 板


LINE

我Update一下ggplot2的狀態 ggplot2 2.2.0已經准許你這樣做了 成果:http://imgur.com/7YIrEB8 程式好讀版:https://pastebin.com/u6fCQKuy 程式碼: library(ggplot2) library(dplyr) numSamples <- 200L DF <- data.frame(V1 = sample(1L:4L, numSamples, TRUE), V2 = sample(1L:2L, numSamples, TRUE)) sum0 <- DF %>% group_by(V1) %>% summarise (mean = mean(V2), n = n()) ggplotColours <- function(n = 6, h = c(0, 360) + 15){ if ((diff(h) %% 360) < 1) h[2] <- h[2] - 360/n hcl(h = (seq(h[1], h[2], length = n)), c = 100, l = 65) } transDblAxisForBar <- function(x, y) { return((y - min(y)) / (max(y) - min(y)) * extendrange(x)[2]) } cols <- ggplotColours(2) ggplot(sum0, aes(V1, n)) + geom_bar(position = "stack", stat = "identity", fill = cols[1]) + geom_line(aes(x = V1, y = transDblAxisForBar(n, mean)), col = cols[2], lwd = 1.5) + scale_y_continuous("n", sec.axis = sec_axis(~ . / extendrange(sum0$n)[2] * diff(range(sum0$mean))+ min(sum0$mean), name = "mean")) ※ 引述《sacidoO (阿罵)》之銘言: : #更新 感謝版主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), 來自: 36.235.47.89
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1497441620.A.6FA.html ※ 編輯: celestialgod (36.235.47.89), 06/14/2017 20:03:02







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Tech_Job站內搜尋

TOP