Ggplot2和tibble绘图geom_point没有检测日期/因子[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我坚持这种奇怪的行为,我也许没有注意到一些明显的东西。在数据争论之后,我最终获得了长格式的数据集,就像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轴的三年。

  • 我究竟做错了什么?
r ggplot2
1个回答
0
投票

试试这个:

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.