DAUTONSA 数据集 geom_boxplot 上的标签

labels on geom_boxplot for DAUTONSA dataset

我正在尝试为我的箱线图添加正确的标签 (2011-2017):

monthly_sales <- read.csv("https://raw.githubusercontent.com/robintux/Datasets4WhosebugQuestions/master/Monthly_Sales_2011-2017.csv"
                          ,header = TRUE, stringsAsFactors = FALSE) 


# BoxPlot 
ggplot(monthly_sales, aes( x = year, y = DAUTONSA, color = as.factor(year))) + 
  geom_boxplot() +
  scale_x_discrete(breaks = 2011:2017, labels = as.character(2011:2017))

结果是

每个箱线图的标签应该是年份 (2011-2017)

我认为这应该可以解决您的问题:当您调用 ggplot(aes):

时将变量设置为 'year' 字符
monthly_sales <- read.csv("https://raw.githubusercontent.com/robintux/Datasets4WhosebugQuestions/master/Monthly_Sales_2011-2017.csv"
                          ,header = TRUE, stringsAsFactors = FALSE) 


# BoxPlot 
ggplot(monthly_sales, aes(x = as.character(year), y = DAUTONSA, color = as.factor(year))) + 
    geom_boxplot() +
    scale_x_discrete(labels = as.character(2011:2017))