簡體   English   中英

繪制分組的連續變量與二進制變量

[英]Plotting grouped continuous variable vs. binary variable

我有一個連續響應變量和一個二進制預測變量。 但是,該二元預測變量也有兩種形式(兩種不同的年份)。 我想創建一個兩年分開的箱形圖,但在同一x變量列中。

這是一個像我一樣的假設數據框設置

    Wingspan     Infected     Year
    15.3         1            2015
    14.9         1            2015
    15.9         0            2016  
    15.0         1            2016
    13.8         0            2015
    16.1         0            2016
    14.2         1            2015
    15.9         1            2015 
    13.7         0            2016
    16.4         0            2016
    13.9         0            2016
    14.0         1            2015

我很容易通過做得到輸出

    Model <- Wingspan ~ Infected
    plot(Model)

但是,我希望Infected列每列有2個框,一個用於2015年,一個用於2016年。我嘗試了各種函數來拆分數據,例如split()和各種綁定函數,但是我似乎無法分區以任何方式獲取此數據並獲得輸出。 任何想法,將不勝感激。

這是您想要的:

require(read.so) #Awesome package by @Alistaire47
dat <- read.so()
require(ggplot2)

ggplot(dat, aes(as.character(Infected), Wingspan, color = as.character(Year))) + 
geom_point()
#I have used as.character in order to prevent R reading the numbers as , 
#... well... , numbers

在此處輸入圖片說明

編輯1對於geom_point()圖,只需將geom_point()更改為geom_boxplot() ……就這樣:)

在基本R中編輯2以使用不同的顏色,將以下內容添加到@thelatemail的代碼中:

boxplot(Wingspan ~ Infected + Year, data=dat, boxfill = dat$Year)
#again, try ggplot. Very rewarding, in terms of getting nice graphs.

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM