2016-03-05 38 views
0

內。如果我有列yearID的數據幀DF和工資
顯示中的R多個盒形圖的特定範圍

boxplot(df$payroll ~ df$yearID, ylab="Payroll", xlab="Year")
顯示每年的箱線圖。有沒有辦法指定顯示的年份範圍?謝謝

+2

看看上一個''boxplot'示例 – rawr

回答

0

讓你的代碼擁有數據是有幫助的。您可以閱讀更多關於如何創建示例here

正如rawr在評論中指出的那樣,您可以使用boxplot的子集參數來縮小呈現年份的範圍。

boxplot(df$payroll ~ df$yearID, ylab="Payroll", xlab="Year", subset = yearID > 2013)

就個人而言,我更喜歡爲了利用從dplyr數據管理工具,保持我的代碼一致的,無論我使用的功能。在這種情況下,您可以使用filter來只選擇您想要的年份。 dplyr在使用pipes時變得更有用,但我會保持此示例簡單。

library(tidyverse) # Includes dplyr and other useful packages 

# Generate dummy data 
yearID <- sample(1995:2016, size = 1000, replace = TRUE) 
payroll <- round(rnorm(1000, mean = 50000, sd = 20000)) 
df <- tibble(yearID, payroll) 

# Filter the data to include only the years you want 
df_plot <- filter(df, yearID > 2013) 

# Generate your boxplot 
boxplot(df_plot$payroll ~ df_plot$yearID, ylab="Payroll", xlab="Year") 
相關問題