2016-03-05 61 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") 
相关问题