ggplot2--實操(圖例的修改:以頻數散佈圖為例)_ggplot2

來源:互聯網
上載者:User
ggplot2–實操(圖例的修改:以頻數散佈圖為例) ggplot2 直接實現

畫圖代碼:
資料為兩列,第二列為因子型。

> head(plotdat)   SOC   SOC過低警示1   0      normal2   2      error3   0      normal4   1      error5   0      normal###大小和高度都代表頻率###g <- g + geom_point(aes(x=plotdat[,1], y=..density.., colour=plotdat[,2], shape=plotdat[,2]),                    stat="bin",binwidth=0.1, size=3, alpha=0.6)  ##用stat轉化(ggplot2 1.1有介紹),binwidth為頻寬,size是點的大小,alpha是透明度g <- g + labs(x = "Soc", y = "count", title="Soc 頻數散佈圖")##設定名字

問題

關於圖例有三個問題:
1.如何除去圖例。
2.圖例是雙映射出來的,如何進行修改。
3.如何自己添加圖例(利用圖層屬性) 1 除去圖例

如何除去圖例,利用映射屬性標度,添加參數guide=”none”。 2 修改圖例

如單映射一樣進行修改,雙映射則對雙映射進行修改。

g <- ggplot(plotdat)g <- g + geom_point(aes(x=plotdat[,1], size=..density.., colour=plotdat[,2], shape=plotdat[,2]),                    stat="bin",binwidth=0.1, size=3, alpha=0.6) g <- g + labs(x = "Soc", y = "count", title="Soc 頻數散佈圖")g <- g + scale_colour_hue(name="SOC過低警示")g <- g + scale_shape_manual(values=c(17,19), name="SOC過低警示")g

3 自己添加圖例

由途中可以看到直接利用ggplot2進行畫頻數散佈圖,底部會將頻寬畫出來。為解決這個問題將對資料進行處理。下列代碼中注意colour的位置,在mapping內時會產生圖例。

plot_normal <- plotdat[which(plotdat[,2]=="normal"),]plot_error <- plotdat[which(plotdat[,2]=="error"),]plot_nor <- as.data.frame(table(plot_normal[,i]))plot_err <- as.data.frame(table(plot_error[,i]))plot_nor[,1] <- as.numeric(plot_nor[,1])plot_err[,1] <- as.numeric(plot_err[,1])g <- ggplot()g <- g + geom_point(aes(x=plot_nor[,1], y=plot_nor[,2], colour="blue"), size=3, data=plot_nor, alpha = 0.6)g <- g + geom_point(aes(x=plot_err[,1], y=plot_err[,2], colour="red"), size=3, data=plot_err, alpha = 0.6)g <- g + labs(x = names(plotdat)[i], y = "count", title="Soc 頻數散佈圖")g <- g + scale_colour_hue( name = "SOC過低警示", labels = c("normal", "error"))print(g)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.