ggplot2和tibble绘图geom_point未检测到日期/因子

时间:2019-04-03 11:50:53

标签: r ggplot2

我坚持这种奇怪的行为,也许我没有注意到明显的事情。数据争吵之后,我最终得到了一个长格式的数据集,非常类似于socviz书中的数据集:

enter image description here

但是在尝试生成条形或geom_points时,ggplot无法将年份识别为单独的因素。请考虑以下MWE:

dep <- function(x) {
  if(!all(x %in% installed.packages()))
    install.packages(x[x %in% installed.packages()[,"Package"] == F])
  lapply(x, require, character.only = T)
}
dep(c("dplyr", "tibble", "ggplot2"))

a<-tribble(
  ~region,  ~icd_group1,                                              ~year, ~visits,
  "Midwest", "F00-F09 Organic, Including Symptomatic, Mental Dis.",     2008,  2.59,
  "Midwest", "F10-F19 Mental And Behav. Dis. Due To Substance Use" ,    2008,  3.18, 
  "South", "F20-F29 Schizophrenia, Schizotypal And Delusional Dis.",  2009, 13.1,  
  "Southeast", "F30-F39 Mood [Affective] Dis.",                           2009, 26.7,  
  "Southeast", "F40-F48 Neurotic, Stress Related And Somatoform Dis.",    2010,  7.77, 
  "North", "F50-F59 Behav. Synd. Assoc. w/ Physical Factors",         2010,  0.409
)
ggplot(a) + geom_point(size = 3)+ aes(x = "year", y ="visits", color = icd_group1)

但是我得到了:

enter image description here

我希望这是沿着x轴的三年。

  • 我在做什么错了?

1 个答案:

答案 0 :(得分:0)

尝试一下:

library(tidyverse)
a %>% ggplot(aes(year, visits))+ geom_point(aes(color=icd_group1))

enter image description here